-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.php
More file actions
442 lines (377 loc) · 15.7 KB
/
Router.php
File metadata and controls
442 lines (377 loc) · 15.7 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php declare(strict_types=1);
/**
* @package Triangle Router Component
* @link https://github.com/Triangle-org/Router
*
* @author Ivan Zorin <creator@localzet.com>
* @copyright Copyright (c) 2023-2025 Triangle Framework Team
* @license https://www.gnu.org/licenses/agpl-3.0 GNU Affero General Public License v3.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* For any questions, please contact <triangle@localzet.com>
*/
namespace Triangle;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ReflectionAttribute;
use ReflectionClass;
use Triangle\Annotation\DisableDefaultRoute;
use Triangle\Http\App;
use Triangle\Router\BadRouteException;
use Triangle\Router\DataGenerator;
use Triangle\Router\Dispatcher;
use Triangle\Router\RouteObject;
use Triangle\Router\RouteParser;
class Router
{
protected static ?DataGenerator $dataGenerator = null;
protected ?Dispatcher $dispatcher = null;
protected static ?RouteParser $routeParser = null;
protected array $children = [];
protected array $routes = [];
protected string $currentGroupPrefix = '';
protected static ?Router $instance = null;
protected static array $disableDefaultRoute = [];
protected static array $nameList = [];
protected static array $fallback = [];
protected static array $fallbackRoutes = [];
protected static array $disabledDefaultRoutes = [];
protected static array $disabledDefaultRouteControllers = [];
protected static array $disabledDefaultRouteActions = [];
/**
* Adds a route to the collection.
*
* The syntax used in the $route string depends on the used route parser.
*
* @param string|string[] $httpMethod
* @param string $path
* @param callable|mixed $callback
* @return RouteObject
*/
public function addRoute(array|string $httpMethod, string $path, mixed $callback): RouteObject
{
$path = $this->currentGroupPrefix . $path;
if (is_string($callback) && strpos($callback, '@')) {
$callback = explode('@', $callback, 2);
}
if (!is_array($callback)) {
if (!is_callable($callback)) {
$callStr = is_scalar($callback) ? $callback : 'Closure';
throw new BadRouteException("Router $path $callStr is not callable\n");
}
} else {
$callback = array_values($callback);
if (!isset($callback[1]) || !class_exists($callback[0]) || !method_exists($callback[0], $callback[1])) {
throw new BadRouteException("Router $path " . json_encode($callback) . " is not callable\n");
}
}
$object = new RouteObject($httpMethod, $path, $callback);
$routeDatas = static::routeParser()->parse($path);
foreach ((array)$httpMethod as $method) {
foreach ($routeDatas as $data) {
static::dataGenerator()->addRoute($method, $path, $data, $callback, $object);
}
}
$this->routes[] = $object;
return $object;
}
public static function route(array|string $methods, string $path, mixed $callback): RouteObject
{
return static::instance()?->addRoute($methods, $path, $callback);
}
public function addGroup(string $path, callable $callback): static
{
$previousGroupPrefix = $this->currentGroupPrefix;
$this->currentGroupPrefix = $previousGroupPrefix . $path;
$callback($this);
$this->currentGroupPrefix = $previousGroupPrefix;
return $this;
}
public static function group(string $path, callable $callback): static
{
$prevInstance = static::$instance;
$nextInstance = static::$instance = new static;
static::$instance?->addGroup(($prevInstance?->currentGroupPrefix ?? '') . $path, $callback);
$prevInstance?->addChild($nextInstance);
static::$instance = $prevInstance;
return $nextInstance;
}
public function addChild(Router $route): void
{
$this->children[] = $route;
}
public static function child(string $path, callable $callback = null): static
{
$prevInstance = static::$instance;
$nextInstance = static::$instance = new static;
$callback(static::$instance);
$prevInstance?->addChild($nextInstance);
static::$instance = $prevInstance;
return $nextInstance;
}
public function getData()
{
return static::dataGenerator()->getData();
}
public static function data(): array
{
return static::instance()?->getData();
}
public function getRoutes()
{
return static::dataGenerator()->getRoutes();
}
public static function routes(): array
{
return static::instance()?->getRoutes();
}
public static function disableDefaultRoute(?string $plugin = '', string $app = null): bool
{
// Is [controller action]
if (is_array($plugin)) {
$controllerAction = $plugin;
if (!isset($controllerAction[0]) || !is_string($controllerAction[0]) ||
!isset($controllerAction[1]) || !is_string($controllerAction[1])) {
return false;
}
$controller = $controllerAction[0];
$action = $controllerAction[1];
static::$disabledDefaultRouteActions[$controller][$action] = $action;
return true;
}
// Is plugin
if (is_string($plugin) && (preg_match('/^[a-zA-Z0-9_]+$/', $plugin) || $plugin === '')) {
if (!isset(static::$disabledDefaultRoutes[$plugin])) {
static::$disabledDefaultRoutes[$plugin] = [];
}
$app = $app ?? '*';
static::$disabledDefaultRoutes[$plugin][$app] = $app;
return true;
}
// Is controller
if (is_string($plugin) && class_exists($plugin)) {
static::$disabledDefaultRouteControllers[$plugin] = $plugin;
return true;
}
return false;
}
public static function isDefaultRouteDisabled($plugin = '', string $app = null): bool
{
// Is [controller action]
if (is_array($plugin)) {
if (!isset($plugin[0]) || !is_string($plugin[0]) ||
!isset($plugin[1]) || !is_string($plugin[1])) {
return false;
}
return isset(static::$disabledDefaultRouteActions[$plugin[0]][$plugin[1]]) || static::isDefaultRouteDisabledByAnnotation($plugin[0], $plugin[1]);
}
// Is plugin
if (is_string($plugin) && (preg_match('/^[a-zA-Z0-9_]+$/', $plugin) || $plugin === '')) {
$app = $app ?? '*';
return isset(static::$disabledDefaultRoutes[$plugin]['*']) || isset(static::$disabledDefaultRoutes[$plugin][$app]);
}
// Is controller
if (is_string($plugin) && class_exists($plugin)) {
return isset(static::$disabledDefaultRouteControllers[$plugin]);
}
return false;
}
/**
* @param string $controller
* @param string|null $action
* @return bool
*/
protected static function isDefaultRouteDisabledByAnnotation(string $controller, ?string $action = null): bool
{
if (class_exists($controller)) {
$reflectionClass = new ReflectionClass($controller);
if ($reflectionClass->getAttributes(DisableDefaultRoute::class, ReflectionAttribute::IS_INSTANCEOF)) {
return true;
}
if ($action && $reflectionClass->hasMethod($action)) {
$reflectionMethod = $reflectionClass->getMethod($action);
if ($reflectionMethod->getAttributes(DisableDefaultRoute::class, ReflectionAttribute::IS_INSTANCEOF)) {
return true;
}
}
}
return false;
}
public static function setByName(string $name, RouteObject $instance): void
{
static::$nameList[$name] = $instance;
}
public static function getByName(string $name): ?RouteObject
{
return static::$nameList[$name] ?? null;
}
public static function fallback(callable $callback, ?string $plugin = ''): RouteObject
{
$route = new RouteObject([], '', $callback);
static::$fallbackRoutes[$plugin ?? ''] = $route;
return $route;
}
public static function getFallback(?string $plugin = '', int $status = 404): ?callable
{
if (!isset(static::$fallback[$plugin])) {
$route = static::$fallbackRoutes[$plugin] ?? null;
static::$fallback[$plugin] = $route ? App::getCallback($plugin, 'NOT_FOUND', $route->getCallback(), ['status' => $status], false, $route) : null;
}
return static::$fallback[$plugin];
}
public static function get(string $path, mixed $callback): RouteObject
{
return static::instance()->addRoute('GET', $path, $callback);
}
public static function post(string $path, mixed $callback): RouteObject
{
return static::instance()->addRoute('POST', $path, $callback);
}
public static function put(string $path, mixed $callback): RouteObject
{
return static::instance()->addRoute('PUT', $path, $callback);
}
public static function patch(string $path, mixed $callback): RouteObject
{
return static::instance()->addRoute('PATCH', $path, $callback);
}
public static function delete(string $path, mixed $callback): RouteObject
{
return static::instance()->addRoute('DELETE', $path, $callback);
}
public static function head(string $path, mixed $callback): RouteObject
{
return static::instance()->addRoute('HEAD', $path, $callback);
}
public static function options(string $path, mixed $callback): RouteObject
{
return static::instance()->addRoute('OPTIONS', $path, $callback);
}
public static function any(string $path, mixed $callback): RouteObject
{
return static::instance()->addRoute('*', $path, $callback);
}
public static function resource(string $name, string $controller, array $actions = null, string $prefix = ''): void
{
$name = trim($name, '/');
$selected = !empty($actions);
if ($selected) {
$diffOptions = array_diff($actions, ['index', 'create', 'store', 'update', 'show', 'edit', 'destroy', 'patch']);
if (!empty($diffOptions)) {
foreach ($diffOptions as $action) {
static::any("/$name/{$action}[/{id}]", [$controller, $action])->name("$prefix$name.$action");
}
}
}
// Отображение списка ресурсов
if (
($selected && in_array('index', $actions))
|| (!$selected && method_exists($controller, 'index'))
) static::get("/$name", [$controller, 'index'])->name("$prefix$name.index");
// Создание нового ресурса
if (
($selected && in_array('store', $actions))
|| (!$selected && method_exists($controller, 'store'))
) static::post("/$name", [$controller, 'store'])->name("$prefix$name.store");
// Отображение формы для создания нового ресурса
if (
($selected && in_array('create', $actions))
|| (!$selected && method_exists($controller, 'create'))
) static::get("/$name/create", [$controller, 'create'])->name("$prefix$name.create");
// Отображение формы для обновления существующего ресурса
if (
($selected && in_array('edit', $actions))
|| (!$selected && method_exists($controller, 'edit'))
) static::get("/$name/{id}/edit", [$controller, 'edit'])->name("$prefix$name.edit");
// Отображение конкретного ресурса
if (
($selected && in_array('show', $actions))
|| (!$selected && method_exists($controller, 'show'))
) static::get("/$name/{id}", [$controller, 'show'])->name("$prefix$name.show");
// Обновление существующего ресурса
if (
($selected && in_array('update', $actions))
|| (!$selected && method_exists($controller, 'update'))
) static::put("/$name/{id}", [$controller, 'update'])->name("$prefix$name.update");
// Изменение существующего ресурса
if (
($selected && in_array('patch', $actions))
|| (!$selected && method_exists($controller, 'patch'))
) static::patch("/$name/{id}", [$controller, 'patch'])->name("$name.patch");
// Удаление существующего ресурса
if (
($selected && in_array('destroy', $actions))
|| (!$selected && method_exists($controller, 'destroy'))
) static::delete("/$name/{id}", [$controller, 'destroy'])->name("$prefix$name.destroy");
}
public static function dispatch(string $method, string $path): array
{
return static::instance()->dispatcher->dispatch($method, $path);
}
public static function routeParser()
{
return static::$routeParser ??= new Router\RouteParser\Std();
}
public static function dataGenerator()
{
return static::$dataGenerator ??= new Router\DataGenerator\GroupCountBased();
}
public static function instance()
{
return static::$instance ??= new static;
}
public static function collect(array $paths): void
{
foreach ($paths as $configPath) {
$routeConfigFile = $configPath . '/route.php';
if (is_file($routeConfigFile)) {
require_once $routeConfigFile;
}
if (!is_dir($pluginConfigPath = $configPath . '/plugin')) {
continue;
}
$dirIterator = new RecursiveDirectoryIterator($pluginConfigPath, FilesystemIterator::FOLLOW_SYMLINKS);
$iterator = new RecursiveIteratorIterator($dirIterator);
foreach ($iterator as $file) {
if ($file->getBaseName('.php') !== 'route') {
continue;
}
$appConfigFile = pathinfo($file, PATHINFO_DIRNAME) . '/app.php';
if (!is_file($appConfigFile)) {
continue;
}
$appConfig = include $appConfigFile;
if (empty($appConfig['enable'])) {
continue;
}
require_once $file;
}
}
static::instance()->dispatcher = new Router\Dispatcher\GroupCountBased(
static::dataGenerator()->getData()
);
}
public function middleware($middleware): Router
{
foreach ($this->routes as $route) {
$route->middleware($middleware);
}
foreach ($this->children as $child) {
$child->middleware($middleware);
}
return $this;
}
}