/
jagepard
/
Rudra-Router
Обзор
Документация
Войти
/
jagepard
/
Rudra-Router
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
v2.0.0
src/Router.php
141 строка
4 KB
Jagepard
Update directCall
05 июн 2018, 17:59
05 июн 2018, 17:59
fac02a3
Код
Авторство
О чём код?
<?php declare(strict_types=1); /** * @author : Korotkov Danila <dankorot@gmail.com> * @copyright Copyright (c) 2018, Korotkov Danila * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3.0 */ namespace Rudra; use Rudra\Interfaces\RouterInterface; use Rudra\Interfaces\ContainerInterface; use Rudra\Traits\RouterMatchTrait; use Rudra\Traits\RouterMethodTrait; use Rudra\Traits\RouterAnnotationTrait; use Rudra\Exceptions\RouterException; /** * Class Router * @package Rudra */ class Router implements RouterInterface { use RouterMatchTrait; use RouterMethodTrait; use RouterAnnotationTrait; /** * @var ContainerInterface */ protected $container; /** * @var string */ protected $namespace; /** * Router constructor. * @param ContainerInterface $container * @param string $namespace */ public function __construct(ContainerInterface $container, string $namespace) { $this->container = $container; $this->namespace = $namespace; set_exception_handler([new RouterException($container), 'handler']); } /** * @param array $route * @throws Exceptions\RouterException */ public function set(array $route): void { $requestMethod = $this->container->getServer('REQUEST_METHOD'); if ($this->container->hasPost('_method') && $requestMethod === 'POST') { $this->setRequestMethod(); } if (($requestMethod === 'GET') || $requestMethod === 'POST') { $this->matchHttpMethod($route); } if (($requestMethod === 'PUT') || ($requestMethod === 'PATCH') || ($requestMethod === 'DELETE')) { $settersName = 'set' . ucfirst(strtolower($requestMethod)); parse_str(file_get_contents('php://input'), $data); $this->container->$settersName($data); $this->matchHttpMethod($route); } } // @codeCoverageIgnore /** * @param array $route * @param null $params * @throws Exceptions\RouterException * @throws RouterException */ public function directCall(array $route, $params = null): void { $controller = new $route['controller']($this->container); if (!method_exists($controller, $route['method'])) { throw new RouterException($this->container, '503'); } // Инициализуруем $controller->init($this->container, []); // Выполняем методы before до основного вызова $controller->before(); !isset($route['middleware']) ?: $this->handleMiddleware($route['middleware']); // Собственно вызываем экшн, в зависимости от наличия параметров isset($params) ? $controller->{$route['method']}($params) : $controller->{$route['method']}(); // Выполняем методы after !isset($route['after_middleware']) ?: $this->handleMiddleware($route['after_middleware']); $controller->after(); // after } protected function setRequestMethod(): void { switch ($this->container->getPost('_method')) { case 'PUT': $this->container->setServer('REQUEST_METHOD', 'PUT'); break; case 'PATCH': $this->container->setServer('REQUEST_METHOD', 'PATCH'); break; case 'DELETE': $this->container->setServer('REQUEST_METHOD', 'DELETE'); break; } } /** * @return mixed */ protected function controllersNamespace(): string { return $this->namespace . 'Controllers\\'; } /** * @return mixed */ protected function middlewareNamespace(): string { return $this->namespace . 'Middleware\\'; } /** * @param string $namespace */ public function setNamespace(string $namespace): void { $this->namespace = $namespace; } }