-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathPackagesController.php
More file actions
144 lines (124 loc) · 4.36 KB
/
PackagesController.php
File metadata and controls
144 lines (124 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
declare(strict_types=1);
namespace App\Controller;
use Cake\Core\Configure;
use Cake\ORM\Query\SelectQuery;
/**
* Packages Controller
*
* @property \App\Model\Table\PackagesTable $Packages
*/
class PackagesController extends AppController
{
/**
* @return void
*/
public function initialize(): void
{
parent::initialize();
$this->Authentication->allowUnauthenticated(['index']);
}
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
// Add default sort if no sort is provided
$queryParams = $this->request->getQueryParams();
if (empty($queryParams['sort'])) {
$this->request = $this->request->withQueryParams(array_merge(
$queryParams,
['sort' => 'latest_stable_release_date', 'direction' => 'desc'],
));
}
$featuredPackages = [];
$activeFilterKeys = ['search', 'cakephp_slugs', 'php_slugs'];
$hasActiveFilters = false;
foreach ($activeFilterKeys as $key) {
if ($this->hasActiveFilterValue($queryParams[$key] ?? null)) {
$hasActiveFilters = true;
break;
}
}
$currentPage = max(1, (int)($queryParams['page'] ?? 1));
$showFeaturedPackages = !$hasActiveFilters && $currentPage === 1;
$featuredPackageNames = [];
if ($showFeaturedPackages) {
$featuredPackageNames = array_values(array_filter((array)Configure::read('Packages.featured', [])));
if ($featuredPackageNames !== []) {
shuffle($featuredPackageNames);
}
$featuredPackages = $this->Packages
->find()
->contain(['Tags' => function (SelectQuery $q) {
return $q->orderByDesc('Tags.label');
}])
->where(['Packages.package IN' => $featuredPackageNames])
->all()
->indexBy('package')
->toArray();
$featuredPackages = array_values(array_filter(
array_map(
static fn(string $packageName) => $featuredPackages[$packageName] ?? null,
$featuredPackageNames,
),
));
}
$query = $this->Packages
->find('search', search: $this->request->getQueryParams())
->contain(['Tags' => function (SelectQuery $q) {
return $q->orderByDesc('Tags.label');
}]);
if ($featuredPackageNames !== []) {
$query->where(['Packages.package NOT IN' => $featuredPackageNames]);
}
$packages = $this->paginate($query, ['limit' => 21]);
$cakephpTags = $this->Packages->Tags->find('list', keyField: 'slug')
->where(['slug LIKE' => 'cakephp-%'])
->toArray();
$cakephpTags = $this->sortVersionTags($cakephpTags, 'CakePHP');
$phpTags = $this->Packages->Tags->find('list', keyField: 'slug')
->where(['slug LIKE' => 'php-%'])
->toArray();
$phpTags = $this->sortVersionTags($phpTags, 'PHP');
$this->set(compact('featuredPackages', 'packages', 'cakephpTags', 'phpTags'));
}
/**
* @param mixed $value
* @return bool
*/
protected function hasActiveFilterValue(mixed $value): bool
{
if (is_array($value)) {
foreach ($value as $item) {
if ($this->hasActiveFilterValue($item)) {
return true;
}
}
return false;
}
if ($value === null) {
return false;
}
if (is_string($value)) {
return trim($value) !== '';
}
return (bool)$value;
}
/**
* @param array<string, string> $tags
* @return array<string, string>
*/
protected function sortVersionTags(array $tags, string $prefix): array
{
$pattern = '/^' . preg_quote($prefix, '/') . ':\s*/';
uasort($tags, static function (string $left, string $right) use ($pattern): int {
$leftVersion = preg_replace($pattern, '', $left) ?: $left;
$rightVersion = preg_replace($pattern, '', $right) ?: $right;
return version_compare($rightVersion, $leftVersion);
});
return $tags;
}
}