/
jagepard
/
Rudra-Router
Обзор
Документация
Войти
/
jagepard
/
Rudra-Router
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
2024
src/Traits/RouterAnnotationTrait.php
83 строки
3 KB
Jagepard
remove Facade
22 май 2023, 15:24
22 май 2023, 15:24
cc50f9a
Код
Авторство
О чём код?
<?php declare(strict_types=1); /** * @author : Jagepard <jagepard@yandex.ru"> * @license https://mit-license.org/ MIT */ namespace Rudra\Router\Traits; use Rudra\Annotation\Annotation; use Rudra\Container\Interfaces\RudraInterface; trait RouterAnnotationTrait { /** * @param array $controllers * @param boolean $getter * @param boolean $attributes * @return void */ public function annotationCollector(array $controllers, bool $getter = false, bool $attributes = false) { $annotations = []; foreach ($controllers as $controller) { $methods = get_class_methods($controller); foreach ($methods as $method) { $annotation = ($attributes) ? $this->rudra->get(Annotation::class)->getAttributes($controller, $method) : $this->rudra->get(Annotation::class)->getAnnotations($controller, $method); $middleware = []; if (isset($annotation["Middleware"])) { $middleware = array_merge($middleware, ['before' => $this->handleAnnotationMiddleware($annotation["Middleware"])]); } if (isset($annotation["AfterMiddleware"])) { $middleware = array_merge($middleware, ['after' => $this->handleAnnotationMiddleware($annotation["AfterMiddleware"])]); } if (isset($annotation["Routing"])) { foreach ($annotation["Routing"] as $route) { if ($getter) { $annotations[] = [[$route['url'], $route["method"] ?? "GET", [$controller, $method], $middleware]]; } else { $this->set([$route['url'], $route["method"] ?? "GET", [$controller, $method], $middleware]); } } } } } if ($getter) { return $annotations; } } /** * @param array $annotation * @return array */ protected function handleAnnotationMiddleware(array $annotation): array { $middleware = []; $count = count($annotation); for ($i = 0; $i < $count; $i++) { $middleware[$i][] = $annotation[$i]["name"]; if (isset($annotation[$i]["params"])) { $middleware[$i][] = $annotation[$i]["params"]; } } return $middleware; } abstract public function rudra(): RudraInterface; }