From b5628bbd3708a8fcec743f413e63ab1a5828bf64 Mon Sep 17 00:00:00 2001 From: Jagepard Date: Tue, 27 May 2025 17:45:44 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B8=D0=BB=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs.md | 95 +++++++++++++++++++++++++++++ src/Router.php | 73 +++++----------------- src/RouterInterface.php | 9 --- src/Routing.php | 4 +- tests/RouterAnnotationTraitTest.php | 2 + 5 files changed, 115 insertions(+), 68 deletions(-) create mode 100644 docs.md diff --git a/docs.md b/docs.md new file mode 100644 index 00000000..50ecb63f --- /dev/null +++ b/docs.md @@ -0,0 +1,95 @@ +## Table of contents +- [Rudra\Router\MiddlewareInterface](#rudra_router_middlewareinterface) +- [Rudra\Router\Router](#rudra_router_router) +- [Rudra\Router\RouterFacade](#rudra_router_routerfacade) +- [Rudra\Router\RouterInterface](#rudra_router_routerinterface) +- [Rudra\Router\Routing](#rudra_router_routing) +- [Rudra\Router\Traits\RouterAnnotationTrait](#rudra_router_traits_routerannotationtrait) +- [Rudra\Router\Traits\RouterRequestMethodTrait](#rudra_router_traits_routerrequestmethodtrait) +
+ + + +### Class: Rudra\Router\MiddlewareInterface +| Visibility | Function | +|:-----------|:---------| +|abstract public|next( array $chainOfMiddlewares ): void
| + + + + +### Class: Rudra\Router\Router +##### implements [Rudra\Router\RouterInterface](#rudra_router_routerinterface) +| Visibility | Function | +|:-----------|:---------| +|public|set( array $route ): void
| +|private|handleRequestUri( array $route ): void
| +|private|handleRequestMethod(): void
| +|private|handlePattern( array $route array $request ): array
| +|private|setCallable( array $route $params ): void
| +|public|directCall( array $route $params ): void
| +|private|callActionThroughReflection( ?array $params string $action object $controller ): void
| +|private|callActionThroughException( $params $action $controller ): void
| +|public|handleMiddleware( array $chainOfMiddlewares ): void
| +|public|annotationCollector( array $controllers bool $getter bool $attributes ): ?array
| +|protected|handleAnnotationMiddleware( array $annotation ): array
| +|public|__construct( Rudra\Container\Interfaces\RudraInterface $rudra )
| +|public|rudra(): Rudra\Container\Interfaces\RudraInterface
| +|public|get( array $route ): void
| +|public|post( array $route ): void
| +|public|put( array $route ): void
| +|public|patch( array $route ): void
| +|public|delete( array $route ): void
| +|public|any( array $route ): void
| +|public|resource( array $route array $actions ): void
| + + + + +### Class: Rudra\Router\RouterFacade +| Visibility | Function | +|:-----------|:---------| +|public static|__callStatic( string $method array $parameters ): mixed
| + + + + +### Class: Rudra\Router\RouterInterface +| Visibility | Function | +|:-----------|:---------| +|abstract public|set( array $route ): void
| +|abstract public|directCall( array $route $params ): void
| + + + + +### Class: Rudra\Router\Routing +| Visibility | Function | +|:-----------|:---------| + + + + +### Class: Rudra\Router\Traits\RouterAnnotationTrait +| Visibility | Function | +|:-----------|:---------| +|public|annotationCollector( array $controllers bool $getter bool $attributes ): ?array
| +|protected|handleAnnotationMiddleware( array $annotation ): array
| + + + + +### Class: Rudra\Router\Traits\RouterRequestMethodTrait +| Visibility | Function | +|:-----------|:---------| +|abstract public|set( array $route ): void
| +|public|get( array $route ): void
| +|public|post( array $route ): void
| +|public|put( array $route ): void
| +|public|patch( array $route ): void
| +|public|delete( array $route ): void
| +|public|any( array $route ): void
| +|public|resource( array $route array $actions ): void
| +
+ +###### created with [Rudra-Documentation-Collector](#https://github.com/Jagepard/Rudra-Documentation-Collector) diff --git a/src/Router.php b/src/Router.php index 0c884001..3a6a1301 100755 --- a/src/Router.php +++ b/src/Router.php @@ -20,12 +20,8 @@ class Router implements RouterInterface use SetRudraContainersTrait; use RouterRequestMethodTrait; - protected array $reflectionCache = []; + private array $reflectionCache = []; - /** - * @param array $route - * @return void - */ public function set(array $route): void { $httpMethods = str_contains($route['method'], '|') @@ -38,50 +34,44 @@ public function set(array $route): void } } - /** - * @param array $route - * @return void - */ - protected function handleRequestUri(array $route): void + private function handleRequestUri(array $route): void { $this->handleRequestMethod(); $request = $this->rudra->request(); $server = $request->server(); - // Проверяем соответствие HTTP-метода if ($route['method'] !== $server->get('REQUEST_METHOD')) { return; } - $uriRaw = $server->get('REQUEST_URI'); - $parsed = parse_url($uriRaw); - $requestPath = $parsed && isset($parsed['path']) ? ltrim($parsed['path'], '/') : ''; - $uriSegments = explode('/', $requestPath); - + $uriRaw = $server->get('REQUEST_URI'); + $parsed = parse_url($uriRaw); + $requestPath = $parsed && isset($parsed['path']) ? ltrim($parsed['path'], '/') : ''; + $uriSegments = explode('/', $requestPath); [$uri, $params] = $this->handlePattern($route, $uriSegments); + if ($uri === $uriSegments) { $this->setCallable($route, $params); } } - protected function handleRequestMethod(): void + private function handleRequestMethod(): void { $request = $this->rudra->request(); $requestMethod = $request->server()->get('REQUEST_METHOD'); - // Spoofing метода через _method + // Spoofing the method via _method parameter in POST requests if ($requestMethod === 'POST' && $request->post()->has('_method')) { $spoofedMethod = strtoupper($request->post()->get('_method')); - if (in_array($spoofedMethod, ['PUT', 'PATCH', 'DELETE'])) { $requestMethod = $spoofedMethod; $request->server()->set(['REQUEST_METHOD' => $spoofedMethod]); } } - // Обработка PUT/PATCH/DELETE + // Handle PUT, PATCH, and DELETE requests by parsing raw input data if (in_array($requestMethod, ['PUT', 'PATCH', 'DELETE'])) { $rawInput = file_get_contents('php://input'); parse_str($rawInput, $data); @@ -89,12 +79,7 @@ protected function handleRequestMethod(): void } } - /** - * @param array $route - * @param array $request - * @return array - */ - protected function handlePattern(array $route, array $request): array + private function handlePattern(array $route, array $request): array { $uri = []; $params = null; @@ -113,7 +98,7 @@ protected function handlePattern(array $route, array $request): array if (array_key_exists($i, $request)) { $pattern = $matches[1]; if (preg_match("/^$pattern$/", $request[$i])) { - $uri[] = $request[$i]; + $uri[] = $request[$i]; $params[] = $request[$i]; } else { $uri[] = '!@#$%^&*'; @@ -129,12 +114,7 @@ protected function handlePattern(array $route, array $request): array return [$uri, $params]; } - /** - * @param array $route - * @param $params - * @throws RouterException|ReflectionException - */ - protected function setCallable(array $route, $params): void + private function setCallable(array $route, $params): void { if ($route['controller'] instanceof \Closure) { if (is_array($params)) { @@ -149,11 +129,6 @@ protected function setCallable(array $route, $params): void $this->directCall($route, $params); } - /** - * @param array $route - * @param $params - * @return void - */ public function directCall(array $route, $params = null): void { $controller = $this->rudra->get($route['controller']); @@ -163,7 +138,6 @@ public function directCall(array $route, $params = null): void throw new RouterException("503"); } - // Bootstrap controller $controller->shipInit(); $controller->containerInit(); $controller->init(); @@ -189,13 +163,7 @@ public function directCall(array $route, $params = null): void } } - /** - * @param $params - * @param $action - * @param $controller - * @return void - */ - protected function callActionThroughReflection(?array $params, string $action, object $controller): void + private function callActionThroughReflection(?array $params, string $action, object $controller): void { if ($params && in_array('', $params, true)) { throw new RouterException("404"); @@ -213,15 +181,7 @@ protected function callActionThroughReflection(?array $params, string $action, o $method->invokeArgs($controller, $arguments); } - /** - * @deprecated - * - * @param $params - * @param $action - * @param $controller - * @return void - */ - protected function callActionThroughException($params, $action, $controller): void + private function callActionThroughException($params, $action, $controller): void { if (isset($params) && in_array('', $params)) { throw new RouterException("404"); @@ -242,9 +202,6 @@ protected function callActionThroughException($params, $action, $controller): vo } } - /** - * @param array $chainOfMiddlewares - */ public function handleMiddleware(array $chainOfMiddlewares): void { if (!$chainOfMiddlewares) { diff --git a/src/RouterInterface.php b/src/RouterInterface.php index 0432b5c8..3769528d 100755 --- a/src/RouterInterface.php +++ b/src/RouterInterface.php @@ -13,15 +13,6 @@ interface RouterInterface { - /** - * @param array $route - */ public function set(array $route): void; - - /** - * @param array $route - * @param null $params - * @throws RouterException - */ public function directCall(array $route, $params = null): void; } diff --git a/src/Routing.php b/src/Routing.php index 0f198157..8241cc33 100755 --- a/src/Routing.php +++ b/src/Routing.php @@ -1,5 +1,7 @@ * @license https://mit-license.org/ MIT @@ -11,4 +13,4 @@ class Routing { -} \ No newline at end of file +} diff --git a/tests/RouterAnnotationTraitTest.php b/tests/RouterAnnotationTraitTest.php index 659e39c0..6ec7edc8 100755 --- a/tests/RouterAnnotationTraitTest.php +++ b/tests/RouterAnnotationTraitTest.php @@ -1,5 +1,7 @@ * @license https://mit-license.org/ MIT