/
jagepard
/
Rudra-Router
Обзор
Документация
Войти
/
jagepard
/
Rudra-Router
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/Traits/RouterAnnotationTrait.php
98 строк
3 KB
Jagepard
Refactor RouterAnnotationTrait for PHP 8.4 and add comprehensive tests
23 июн 2026, 16:53
23 июн 2026, 16:53
645346f
Код
Авторство
О чём код?
<?php declare(strict_types=1); /** * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. * * @author Korotkov Danila (Jagepard) <jagepard@yandex.ru> * @license https://mozilla.org/MPL/2.0/ MPL-2.0 */ namespace Rudra\Router\Traits; use Rudra\Annotation\Annotation; trait RouterAnnotationTrait { /** * Collects and processes annotations from the specified controllers. * * This method scans each controller class for Routing and Middleware annotations, * builds route definitions based on those annotations, and either: * - Registers them directly via `set()` (if $getter = false), or * - Returns them as an array (if $getter = true). * * @throws \Exception If a specified controller class does not exist. */ public function annotationCollector(array $controllers, bool $getter = false, bool $attributes = false): ?array { $annotations = []; $annotationService = $this->rudra->get(Annotation::class); foreach ($controllers as $controller) { if (!class_exists($controller)) { throw new \Exception("Remove the $controller controller from the routes.php file."); } $reflection = new \ReflectionClass($controller); $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); foreach ($methods as $method) { $action = $method->getName(); $annotation = $attributes ? $annotationService->getAttributes($controller, $action) : $annotationService->getAnnotations($controller, $action); $middleware = []; if (isset($annotation["Middleware"])) { $middleware['before'] = $this->handleAnnotationMiddleware($annotation["Middleware"]); } if (isset($annotation["AfterMiddleware"])) { $middleware['after'] = $this->handleAnnotationMiddleware($annotation["AfterMiddleware"]); } if (isset($annotation["Routing"])) { foreach ($annotation["Routing"] as $route) { $route += [ 'controller' => $controller, 'action' => $action, 'middleware' => $middleware, 'method' => 'GET', ]; $getter ? $annotations[] = [$route] : $this->set($route); } } } } return $getter ? $annotations : null; } /** * Processes middleware annotations into a valid middleware format. * * ```#[Middleware(name: "Auth", params: "admin")]``` * to: * ```['Auth', 'admin']``` */ protected function handleAnnotationMiddleware(array $annotation): array { $output = []; foreach ($annotation as $middleware) { if (!isset($middleware['params'])) { $output[] = $middleware['name']; } else { $output[] = [$middleware['name'], $middleware['params']]; } } return $output; } }