-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathPackagesController.php
More file actions
192 lines (166 loc) · 5.9 KB
/
PackagesController.php
File metadata and controls
192 lines (166 loc) · 5.9 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
declare(strict_types=1);
namespace App\Controller;
use Cake\Core\Configure;
use Cake\Http\Response;
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', 'autocomplete']);
}
/**
* 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'));
}
/**
* Autocomplete endpoint for package search.
*
* @return \Cake\Http\Response
*/
public function autocomplete(): Response
{
$q = trim((string)$this->request->getQuery('q'));
if (mb_strlen($q) < 2) {
return $this->response
->withType('application/json')
->withStringBody(json_encode([], JSON_THROW_ON_ERROR));
}
$packages = $this->Packages
->find('autocomplete', search: $q)
->all();
$results = [];
foreach ($packages as $package) {
$cakeVersions = [];
foreach ($package->cake_php_tag_groups as $major => $tags) {
$cakeVersions[] = $major . '.x';
}
$phpVersions = [];
foreach ($package->php_tag_groups as $major => $tags) {
$phpVersions[] = $major . '.x';
}
$results[] = [
'package' => $package->package,
'description' => $package->description,
'repo_url' => $package->repo_url,
'downloads' => $package->downloads,
'stars' => $package->stars,
'latest_version' => $package->latest_stable_version,
'cakephp_versions' => $cakeVersions,
'php_versions' => $phpVersions,
];
}
return $this->response
->withType('application/json')
->withStringBody(json_encode($results, JSON_THROW_ON_ERROR));
}
/**
* @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;
}
}