/
jagepard
/
Rudra-Router
Обзор
Документация
Войти
/
jagepard
/
Rudra-Router
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
v2.0.0
src/Traits/RouterMatchTrait.php
140 строк
4 KB
Jagepard
middleware update
30 июл 2018, 19:01
30 июл 2018, 19:01
69da92a
Код
Авторство
О чём код?
<?php declare(strict_types=1); /** * @author : Korotkov Danila <dankorot@gmail.com> * @copyright Copyright (c) 2016, Korotkov Danila * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3.0 */ namespace Rudra\Traits; use Rudra\Exceptions\RouterException; /** * Trait RouterMatchTrait * @package Rudra\Traits */ trait RouterMatchTrait { /** * @param array $route * @throws RouterException */ protected function matchHttpMethod(array $route): void { if (!strpos($route['http_method'], '|') !== false) { $this->matchRequest($route); } foreach (explode('|', $route['http_method']) as $httpItem) { $route['http_method'] = $httpItem; $this->matchRequest($route); } } /** * @param array $route * @throws RouterException */ protected function matchRequest(array $route): void { if ($route['http_method'] == $this->container->getServer('REQUEST_METHOD')) { $parsedRequest = parse_url(trim($this->container->getServer('REQUEST_URI'), '/'))['path']; list($patternsArray, $params) = $this->handlePattern($route, explode('/', $parsedRequest)); (implode('/', $patternsArray) !== $parsedRequest) ?: $this->setCallable($route, $params); } } /** * @param array $route * @param array $parsedRequest * @return array */ protected function handlePattern(array $route, array $parsedRequest): array { $i = 0; $params = null; $patternsArray = []; foreach (explode('/', ltrim($route['pattern'], '/')) as $patternItem) { // Ищем совпадение с шаблоном {...} if (preg_match('/{[a-zA-Z0-9]+}/', $patternItem, $matchesPattern) != 0) { // Убираем {} из названия будующего ключа массива параметров preg_match('/[a-zA-Z0-9]+/', $matchesPattern[0], $key); $params[$key[0]] = $parsedRequest[$i]; $patternsArray[] = $parsedRequest[$i]; } else { $patternsArray[] = $patternItem; } $i++; } return [$patternsArray, $params]; } /** * @param array $middleware * @throws RouterException */ public function handleMiddleware(array $middleware) { foreach ($middleware as $current) { $middlewareName = $this->setClassName($current[0], 'middlewareNamespace'); (new $middlewareName($this->container))(); } } /** * @param array $route * @param $params * @return mixed * @throws RouterException */ protected function setCallable(array $route, $params) { // Если $route['method'] является экземпляром ксласса Closure // возвращаем замыкание if ($route['method'] instanceof \Closure) { return $route['method'](); } $route['controller'] = $this->setClassName($route['controller'], 'controllersNamespace'); isset($params) ? $this->directCall($route, $params) : $this->directCall($route); } /** * @param string $className * @param string $type * @return string * @throws RouterException */ protected function setClassName(string $className, string $type): string { if (strpos($className, ':fq') !== false) { $classNameArray = explode(':', $className); if (!class_exists($classNameArray[0])) { throw new RouterException($this->container, '503'); } return $classNameArray[0]; } if (!class_exists($this->$type() . $className)) { throw new RouterException($this->container, '503'); } return $this->$type() . $className; } /** * @param array $classAndMethod * @param null $params */ abstract public function directCall(array $classAndMethod, $params = null): void; }