| + | Code Coverage |
+ |||||||||
| + | Lines |
+ Functions and Methods |
+ Classes and Traits |
+ |||||||
| Total | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ CRAP | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
| AfterMiddleware | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 2 | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
| __construct | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 2 | ++ | ||
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | /** |
| 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | * |
| 8 | * @author Korotkov Danila (Jagepard) <jagepard@yandex.ru> |
| 9 | * @license https://mozilla.org/MPL/2.0/ MPL-2.0 |
| 10 | */ |
| 11 | |
| 12 | #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] |
| 13 | class AfterMiddleware |
| 14 | { |
| 15 | public function __construct( |
| 16 | public string $name, |
| 17 | public string|null $params = null |
| 18 | ) {} |
| 19 | } |
| + | Code Coverage |
+ |||||||||
| + | Lines |
+ Functions and Methods |
+ Classes and Traits |
+ |||||||
| Total | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ CRAP | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
| Middleware | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 2 | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
| __construct | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 2 | ++ | ||
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | /** |
| 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | * |
| 8 | * @author Korotkov Danila (Jagepard) <jagepard@yandex.ru> |
| 9 | * @license https://mozilla.org/MPL/2.0/ MPL-2.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Rudra\Router\Attributes; |
| 13 | |
| 14 | #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] |
| 15 | class Middleware |
| 16 | { |
| 17 | public function __construct( |
| 18 | public string $name, |
| 19 | public string|null $params = null |
| 20 | ) {} |
| 21 | } |
| + | Code Coverage |
+ |||||||||
| + | Lines |
+ Functions and Methods |
+ Classes and Traits |
+ |||||||
| Total | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ CRAP | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
| Routing | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 2 | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
| __construct | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 2 | ++ | ||
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | /** |
| 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | * |
| 8 | * @author Korotkov Danila (Jagepard) <jagepard@yandex.ru> |
| 9 | * @license https://mozilla.org/MPL/2.0/ MPL-2.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Rudra\Router\Attributes; |
| 13 | |
| 14 | #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] |
| 15 | class Routing |
| 16 | { |
| 17 | public function __construct( |
| 18 | public string $url = '', |
| 19 | public string|array $method = 'GET' |
| 20 | ) {} |
| 21 | } |
Bubble size = executable lines. Hover for details, click to navigate.
+Sorted by descending CRAP (Change Risk Anti-Patterns) index.
+| Class | +CRAP | +Coverage | +
|---|---|---|
| AfterMiddleware | 2 | 0.0% |
| Rudra\Router\Attributes\Middleware | 2 | 0.0% |
| Rudra\Router\Attributes\Routing | 2 | 0.0% |
Bubble size = executable lines. Hover for details, click to navigate.
+Sorted by descending CRAP (Change Risk Anti-Patterns) index.
+| Method | +CRAP | +Coverage | +
|---|---|---|
| AfterMiddleware::__construct | 2 | 0.0% |
| Rudra\Router\Attributes\Middleware::__construct | 2 | 0.0% |
| Rudra\Router\Attributes\Routing::__construct | 2 | 0.0% |
| + | Code Coverage |
+ ||||||||
| + | Lines |
+ Functions and Methods |
+ Classes and Traits |
+ ||||||
| Total | +
+
+
+ |
+ 0.00% |
+ 0 / 3 |
+
+
+
+ |
+ 0.00% |
+ 0 / 3 |
+
+
+
+ |
+ 0.00% |
+ 0 / 3 |
+
|
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ |
|
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ |
|
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ |
| + | Code Coverage |
+ |||||||||
| + | Lines |
+ Functions and Methods |
+ Classes and Traits |
+ |||||||
| Total | +
+
+
+ |
+ 0.00% |
+ 0 / 117 |
+
+
+
+ |
+ 0.00% |
+ 0 / 9 |
+ CRAP | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
| Router | +
+
+
+ |
+ 0.00% |
+ 0 / 117 |
+
+
+
+ |
+ 0.00% |
+ 0 / 9 |
+ 2550 | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
| set | +
+
+
+ |
+ 0.00% |
+ 0 / 6 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 12 | ++ | ||
| handleRequestUri | +
+
+
+ |
+ 0.00% |
+ 0 / 12 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 30 | ++ | ||
| handleRequestMethod | +
+
+
+ |
+ 0.00% |
+ 0 / 11 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 30 | ++ | ||
| handlePattern | +
+
+
+ |
+ 0.00% |
+ 0 / 20 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 56 | ++ | ||
| setCallable | +
+
+
+ |
+ 0.00% |
+ 0 / 8 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 20 | ++ | ||
| directCall | +
+
+
+ |
+ 0.00% |
+ 0 / 18 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 42 | ++ | ||
| callActionThroughReflection | +
+
+
+ |
+ 0.00% |
+ 0 / 10 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 20 | ++ | ||
| callActionThroughException | +
+
+
+ |
+ 0.00% |
+ 0 / 11 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 42 | ++ | ||
| handleMiddleware | +
+
+
+ |
+ 0.00% |
+ 0 / 21 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 110 | ++ | ||
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | /** |
| 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | * |
| 8 | * @author Korotkov Danila (Jagepard) <jagepard@yandex.ru> |
| 9 | * @license https://mozilla.org/MPL/2.0/ MPL-2.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Rudra\Router; |
| 13 | |
| 14 | use ReflectionException; |
| 15 | use Rudra\Container\Traits\SetRudraContainersTrait; |
| 16 | use Rudra\Exceptions\MiddlewareException; |
| 17 | use Rudra\Exceptions\RouterException; |
| 18 | use Rudra\Router\Traits\RouterAnnotationTrait; |
| 19 | use Rudra\Router\Traits\RouterRequestMethodTrait; |
| 20 | |
| 21 | class Router implements RouterInterface |
| 22 | { |
| 23 | use RouterAnnotationTrait; |
| 24 | use SetRudraContainersTrait; |
| 25 | use RouterRequestMethodTrait; |
| 26 | |
| 27 | private array $reflectionCache = []; |
| 28 | |
| 29 | /** |
| 30 | * Sets the route, parsing HTTP methods (if multiple are specified via |). |
| 31 | * Registers a route handler for each method. |
| 32 | */ |
| 33 | public function set(array $route): void |
| 34 | { |
| 35 | $httpMethods = str_contains($route['method'], '|') |
| 36 | ? explode('|', $route['method']) |
| 37 | : [$route['method']]; |
| 38 | |
| 39 | foreach ($httpMethods as $httpMethod) { |
| 40 | $route['method'] = $httpMethod; |
| 41 | $this->handleRequestUri($route); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Processes the incoming URI request and checks if it matches the current route. |
| 47 | */ |
| 48 | private function handleRequestUri(array $route): void |
| 49 | { |
| 50 | $this->handleRequestMethod(); |
| 51 | |
| 52 | $request = $this->rudra->request(); |
| 53 | $server = $request->server(); |
| 54 | |
| 55 | if ($route['method'] !== $server->get('REQUEST_METHOD')) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | $uriRaw = $server->get('REQUEST_URI'); |
| 60 | $parsed = parse_url($uriRaw); |
| 61 | $requestPath = $parsed && isset($parsed['path']) ? ltrim($parsed['path'], '/') : ''; |
| 62 | $uriSegments = explode('/', $requestPath); |
| 63 | [$uri, $params] = $this->handlePattern($route, $uriSegments); |
| 64 | |
| 65 | if ($uri === $uriSegments) { |
| 66 | $this->setCallable($route, $params); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Processes the HTTP request method, including spoofing via _method (for PUT/PATCH/DELETE) |
| 72 | */ |
| 73 | private function handleRequestMethod(): void |
| 74 | { |
| 75 | $request = $this->rudra->request(); |
| 76 | $requestMethod = $request->server()->get('REQUEST_METHOD'); |
| 77 | |
| 78 | // Spoofing the method via _method parameter in POST requests |
| 79 | if ($requestMethod === 'POST' && $request->post()->has('_method')) { |
| 80 | $spoofedMethod = strtoupper($request->post()->get('_method')); |
| 81 | if (in_array($spoofedMethod, ['PUT', 'PATCH', 'DELETE'])) { |
| 82 | $requestMethod = $spoofedMethod; |
| 83 | $request->server()->set(['REQUEST_METHOD' => $spoofedMethod]); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Handle PUT, PATCH, and DELETE requests by parsing raw input data |
| 88 | if (in_array($requestMethod, ['PUT', 'PATCH', 'DELETE'])) { |
| 89 | $rawInput = file_get_contents('php://input'); |
| 90 | parse_str($rawInput, $data); |
| 91 | $request->{strtolower($requestMethod)}()->set($data); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Matches the URI from the route with the actual request, processing parameters of the form :param and :regexp. |
| 97 | * This method is used to extract dynamic segments from a URI pattern |
| 98 | */ |
| 99 | private function handlePattern(array $route, array $request): array |
| 100 | { |
| 101 | $uri = []; |
| 102 | $params = null; |
| 103 | $subject = explode('/', ltrim($route['url'], '/')); |
| 104 | $count = count($subject); |
| 105 | |
| 106 | for ($i = 0; $i < $count; $i++) { |
| 107 | if (preg_match("/^:[a-zA-Z0-9_-]+$/", $subject[$i]) > 0 && array_key_exists($i, $request)) { |
| 108 | $value = $request[$i]; |
| 109 | $uri[] = $value; |
| 110 | $params[] = $value; |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | if (preg_match("/^:([\\[\\]\\\\:a-zA-Z0-9_\\-{,}]+)$/", $subject[$i], $matches)) { |
| 115 | if (array_key_exists($i, $request)) { |
| 116 | $pattern = $matches[1]; |
| 117 | if (preg_match("/^$pattern$/", $request[$i])) { |
| 118 | $uri[] = $request[$i]; |
| 119 | $params[] = $request[$i]; |
| 120 | } else { |
| 121 | $uri[] = '!@#$%^&*'; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | $uri[] = $subject[$i]; |
| 129 | } |
| 130 | |
| 131 | return [$uri, $params]; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Calls the controller associated with the route — either a Closure or a controller method. |
| 136 | */ |
| 137 | private function setCallable(array $route, ?array $params): void |
| 138 | { |
| 139 | if ($route['controller'] instanceof \Closure) { |
| 140 | if (is_array($params)) { |
| 141 | $route['controller'](...$params); |
| 142 | } else { |
| 143 | $route['controller']($params); |
| 144 | } |
| 145 | |
| 146 | if ($this->rudra->config()->get('environment') !== 'test') { |
| 147 | exit(); |
| 148 | } |
| 149 | |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | $this->directCall($route, $params); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Calls the controller and its method directly, performing the full lifecycle: |
| 158 | * This method is used to fully dispatch a route after matching it with the current request. |
| 159 | * |
| 160 | * @throws RouterException |
| 161 | */ |
| 162 | public function directCall(array $route, ?array $params = null): void |
| 163 | { |
| 164 | $controller = $this->rudra->get($route['controller']); |
| 165 | $action = $route['action']; |
| 166 | |
| 167 | if (!method_exists($controller, $action)) { |
| 168 | throw new RouterException("Service Unavailable", 503); |
| 169 | } |
| 170 | |
| 171 | $controller->shipInit(); |
| 172 | $controller->containerInit(); |
| 173 | $controller->init(); |
| 174 | |
| 175 | $controller->before(); |
| 176 | |
| 177 | if (isset($route['middleware']['before'])) { |
| 178 | $this->handleMiddleware($route['middleware']['before']); |
| 179 | } |
| 180 | |
| 181 | ((int) ini_get('zend.exception_ignore_args') === 1) |
| 182 | ? $this->callActionThroughReflection($params, $action, $controller) |
| 183 | : $this->callActionThroughException($params, $action, $controller); |
| 184 | |
| 185 | if (isset($route['middleware']['after'])) { |
| 186 | $this->handleMiddleware($route['middleware']['after']); |
| 187 | } |
| 188 | |
| 189 | $controller->after(); |
| 190 | |
| 191 | if ($this->rudra->config()->get('environment') !== 'test') { |
| 192 | exit(); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Calls the controller method using Reflection, performing automatic parameter injection based on type hints. |
| 198 | * |
| 199 | * This method is typically used when the zend.exception_ignore_args setting is enabled, |
| 200 | * allowing for more flexible and type-safe dependency resolution. |
| 201 | * |
| 202 | * @throws RouterException |
| 203 | */ |
| 204 | private function callActionThroughReflection(?array $params, string $action, object $controller): void |
| 205 | { |
| 206 | if ($params && in_array('', $params, true)) { |
| 207 | throw new RouterException("Not Found", 404); |
| 208 | } |
| 209 | |
| 210 | $cacheKey = get_class($controller) . "::$action"; |
| 211 | |
| 212 | if (!isset($this->reflectionCache[$cacheKey])) { |
| 213 | $this->reflectionCache[$cacheKey] = [ |
| 214 | 'method' => new \ReflectionMethod($controller, $action), |
| 215 | ]; |
| 216 | } |
| 217 | |
| 218 | $method = $this->reflectionCache[$cacheKey]['method']; |
| 219 | $arguments = $this->rudra()->getParamsIoC($method, $params); |
| 220 | $method->invokeArgs($controller, $arguments); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Calls the specified controller method directly. |
| 225 | * |
| 226 | * If the argument type or number does not match — tries to automatically inject required dependencies. |
| 227 | * This is a fallback mechanism for cases where Reflection-based injection is disabled or unavailable. |
| 228 | * |
| 229 | * Handles two types of errors during invocation: |
| 230 | * - \ArgumentCountError — thrown when the number of arguments doesn't match the method signature. |
| 231 | * - \TypeError — thrown when an argument is not compatible with the expected type. |
| 232 | * |
| 233 | * In both cases, Rudra's autowire system attempts to resolve and inject the correct dependencies. |
| 234 | * |
| 235 | * @throws RouterException |
| 236 | * @throws \TypeError |
| 237 | * @throws \ArgumentCountError |
| 238 | */ |
| 239 | private function callActionThroughException(?array $params, string $action, object $controller): void |
| 240 | { |
| 241 | if (isset($params) && in_array('', $params)) { |
| 242 | throw new RouterException("Not Found", 404); |
| 243 | } |
| 244 | |
| 245 | try { |
| 246 | if (empty($params)) { |
| 247 | $controller->$action(); |
| 248 | } else { |
| 249 | $controller->$action(...$params); |
| 250 | } |
| 251 | } catch (\ArgumentCountError $e) { |
| 252 | $trace = $e->getTrace()[0]; |
| 253 | $this->rudra()->autowire($this->rudra()->get($trace['class']), $trace['function']); |
| 254 | } catch (\TypeError $e) { |
| 255 | $trace = $e->getTrace()[0]; |
| 256 | $this->rudra()->autowire($this->rudra()->new($trace['class']), $trace['function'], $trace['args']); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Executes a chain of middleware, recursively calling each element. |
| 262 | * |
| 263 | * Middleware can be specified in one of the supported formats: |
| 264 | * - 'MiddlewareClass' (string) — a simple class name to call without parameters. |
| 265 | * - ['MiddlewareClass'] (array with class name) — same as above, allows for future extensions. |
| 266 | * - ['MiddlewareClass', $parameter] (array with class and parameter) — passes the parameter to the middleware. |
| 267 | * |
| 268 | * Each middleware must implement the __invoke() method to be callable. |
| 269 | * |
| 270 | * @throws \Rudra\Router\Exceptions\MiddlewareException |
| 271 | */ |
| 272 | public function handleMiddleware(array $chain): void |
| 273 | { |
| 274 | if (!$chain) { |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | $current = array_shift($chain); |
| 279 | |
| 280 | try { |
| 281 | if (is_array($current) && count($current) === 2 && is_string($current[0])) { |
| 282 | $middleware = new $current[0](); |
| 283 | $middleware($chain, ...$current[1]); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | if (is_array($current) && is_string($current[0])) { |
| 288 | $middleware = new $current[0](); |
| 289 | $middleware($chain); |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | if (is_string($current)) { |
| 294 | $middleware = new $current(); |
| 295 | $middleware($chain); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | if (is_callable($current)) { |
| 300 | $current($chain); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | throw new MiddlewareException('Invalid middleware format'); |
| 305 | } catch (\Throwable $e) { |
| 306 | throw new MiddlewareException("Failed to process middleware: " . json_encode($current), 0, $e); |
| 307 | } |
| 308 | } |
| 309 | } |
| + | Code Coverage |
+ |||||||||
| + | Lines |
+ Functions and Methods |
+ Classes and Traits |
+ |||||||
| Total | ++ | n/a |
+ 0 / 0 |
+ + | n/a |
+ 0 / 0 |
+ CRAP | ++ | n/a |
+ 0 / 0 |
+
| RouterFacade | ++ | n/a |
+ 0 / 0 |
+ + | n/a |
+ 0 / 0 |
+ 0 | ++ | n/a |
+ 0 / 0 |
+
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | /** |
| 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | * |
| 8 | * @author Korotkov Danila (Jagepard) <jagepard@yandex.ru> |
| 9 | * @license https://mozilla.org/MPL/2.0/ MPL-2.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Rudra\Router; |
| 13 | |
| 14 | use Rudra\Container\Traits\FacadeTrait; |
| 15 | |
| 16 | /** |
| 17 | * @method static void set(array $route) |
| 18 | * @method static void directCall(array $classAndMethod, $params = null) |
| 19 | * @method static void get(string $pattern, $target, array $middleware = []) |
| 20 | * @method static void post(string $pattern, $target, array $middleware = []) |
| 21 | * @method static void put(string $pattern, $target, array $middleware = []) |
| 22 | * @method static void patch(string $pattern, $target, array $middleware = []) |
| 23 | * @method static void delete(string $pattern, $target, array $middleware = []) |
| 24 | * @method static void any(string $pattern, $target, array $middleware = []) |
| 25 | * @method static void resource(string $pattern, string $controller, array $actions = []) |
| 26 | * @method static annotationCollector(array $controllers) |
| 27 | * @method static handleMiddleware(array $middleware) |
| 28 | * |
| 29 | * @see Router |
| 30 | */ |
| 31 | final class RouterFacade |
| 32 | { |
| 33 | use FacadeTrait; |
| 34 | } |
| + | Code Coverage |
+ |||||||||
| + | Lines |
+ Functions and Methods |
+ Classes and Traits |
+ |||||||
| Total | ++ | n/a |
+ 0 / 0 |
+ + | n/a |
+ 0 / 0 |
+ CRAP | ++ | n/a |
+ 0 / 0 |
+
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | * |
| 8 | * @author Korotkov Danila (Jagepard) <jagepard@yandex.ru> |
| 9 | * @license https://mozilla.org/MPL/2.0/ MPL-2.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Rudra\Router; |
| 13 | |
| 14 | use Rudra\Exceptions\RouterException; |
| 15 | |
| 16 | interface RouterInterface |
| 17 | { |
| 18 | public function set(array $route): void; |
| 19 | public function directCall(array $route, ?array $params = null): void; |
| 20 | } |
| + | Code Coverage |
+ |||||||||
| + | Lines |
+ Functions and Methods |
+ Classes and Traits |
+ |||||||
| Total | +
+
+
+ |
+ 0.00% |
+ 0 / 35 |
+
+
+
+ |
+ 0.00% |
+ 0 / 2 |
+ CRAP | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
| RouterAnnotationTrait | +
+
+
+ |
+ 0.00% |
+ 0 / 35 |
+
+
+
+ |
+ 0.00% |
+ 0 / 2 |
+ 210 | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
| annotationCollector | +
+
+
+ |
+ 0.00% |
+ 0 / 29 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 132 | ++ | ||
| handleAnnotationMiddleware | +
+
+
+ |
+ 0.00% |
+ 0 / 6 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 12 | ++ | ||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @author Korotkov Danila (Jagejord) <jagepard@yandex.ru> |
| 11 | * @license https://mozilla.org/MPL/2.0/ MPL-2.0 |
| 12 | */ |
| 13 | |
| 14 | namespace Rudra\Router\Traits; |
| 15 | |
| 16 | use ReflectionClass; |
| 17 | use ReflectionMethod; |
| 18 | use RuntimeException; |
| 19 | use Rudra\Annotation\Annotation; |
| 20 | use Rudra\Container\Interfaces\RudraInterface; |
| 21 | |
| 22 | /** |
| 23 | * @property RudraInterface $rudra |
| 24 | */ |
| 25 | trait RouterAnnotationTrait |
| 26 | { |
| 27 | /** |
| 28 | * Collects and processes annotations/attributes from the specified controllers. |
| 29 | * |
| 30 | * This method scans each controller class for Routing and Middleware annotations/attributes, |
| 31 | * builds route definitions based on them, and either: |
| 32 | * - Registers them directly via `set()` (if $getter = false), or |
| 33 | * - Returns them as an array (if $getter = true). |
| 34 | * |
| 35 | * @param array<class-string> $controllers |
| 36 | * @param bool $getter If true, returns the collected routes instead of registering them. |
| 37 | * @param bool $attributes If true, uses PHP 8 Attributes instead of Doctrine Annotations. |
| 38 | * @return array<int, array<string, mixed>>|null |
| 39 | * @throws RuntimeException If a specified controller class does not exist. |
| 40 | */ |
| 41 | public function annotationCollector(array $controllers, bool $getter = false, bool $attributes = false): ?array |
| 42 | { |
| 43 | $annotations = []; |
| 44 | $annotationService = $this->rudra->get(Annotation::class); |
| 45 | |
| 46 | foreach ($controllers as $controller) { |
| 47 | if (!class_exists($controller)) { |
| 48 | throw new RuntimeException("Remove the {$controller} controller from the routes.php file."); |
| 49 | } |
| 50 | |
| 51 | $reflection = new ReflectionClass($controller); |
| 52 | $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC); |
| 53 | |
| 54 | foreach ($methods as $method) { |
| 55 | $action = $method->getName(); |
| 56 | $annotation = $attributes |
| 57 | ? $annotationService->getAttributes($controller, $action) |
| 58 | : $annotationService->getAnnotations($controller, $action); |
| 59 | |
| 60 | $middleware = []; |
| 61 | |
| 62 | if (isset($annotation['Middleware'])) { |
| 63 | $middleware['before'] = $this->handleAnnotationMiddleware($annotation['Middleware']); |
| 64 | } |
| 65 | |
| 66 | if (isset($annotation['AfterMiddleware'])) { |
| 67 | $middleware['after'] = $this->handleAnnotationMiddleware($annotation['AfterMiddleware']); |
| 68 | } |
| 69 | |
| 70 | if (isset($annotation['Routing'])) { |
| 71 | foreach ($annotation['Routing'] as $route) { |
| 72 | $route += [ |
| 73 | 'controller' => $controller, |
| 74 | 'action' => $action, |
| 75 | 'middleware' => $middleware, |
| 76 | 'method' => 'GET', |
| 77 | ]; |
| 78 | |
| 79 | if ($getter) { |
| 80 | $annotations[] = $route; |
| 81 | } else { |
| 82 | $this->set($route); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return $getter ? $annotations : null; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Processes middleware annotations into a valid middleware format. |
| 94 | * |
| 95 | * Example: |
| 96 | * ```#[Middleware(name: "Auth", params: "admin")]``` |
| 97 | * converts to: |
| 98 | * ```['Auth', 'admin']``` |
| 99 | * |
| 100 | * @param array<int, array{name: string, params?: mixed}> $annotation |
| 101 | * @return array<int, string|array{0: string, 1: mixed}> |
| 102 | */ |
| 103 | protected function handleAnnotationMiddleware(array $annotation): array |
| 104 | { |
| 105 | $output = []; |
| 106 | |
| 107 | foreach ($annotation as $middleware) { |
| 108 | if (!isset($middleware['params'])) { |
| 109 | $output[] = $middleware['name']; |
| 110 | } else { |
| 111 | $output[] = [$middleware['name'], $middleware['params']]; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return $output; |
| 116 | } |
| 117 | } |
| + | Code Coverage |
+ |||||||||
| + | Lines |
+ Functions and Methods |
+ Classes and Traits |
+ |||||||
| Total | +
+
+
+ |
+ 0.00% |
+ 0 / 47 |
+
+
+
+ |
+ 0.00% |
+ 0 / 8 |
+ CRAP | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
| RouterRequestMethodTrait | +
+
+
+ |
+ 0.00% |
+ 0 / 47 |
+
+
+
+ |
+ 0.00% |
+ 0 / 8 |
+ 462 | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
| get | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 2 | ++ | ||
| post | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 2 | ++ | ||
| put | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 2 | ++ | ||
| patch | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 2 | ++ | ||
| delete | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 2 | ++ | ||
| any | +
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 2 | ++ | ||
| resource | +
+
+
+ |
+ 0.00% |
+ 0 / 28 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 90 | ++ | ||
| setRoute | +
+
+
+ |
+ 0.00% |
+ 0 / 13 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ 42 | ++ | ||
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | /** |
| 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | * |
| 8 | * @author Korotkov Danila (Jagepard) <jagepard@yandex.ru> |
| 9 | * @license https://mozilla.org/MPL/2.0/ MPL-2.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Rudra\Router\Traits; |
| 13 | |
| 14 | trait RouterRequestMethodTrait |
| 15 | { |
| 16 | /** |
| 17 | * Registers a route with the GET HTTP method. |
| 18 | */ |
| 19 | public function get(string $pattern, array|callable $target, array $middleware = []): void |
| 20 | { |
| 21 | $this->setRoute($pattern, $target, 'GET', $middleware); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Registers a route with the POST HTTP method. |
| 26 | */ |
| 27 | public function post(string $pattern, array|callable $target, array $middleware = []): void |
| 28 | { |
| 29 | $this->setRoute($pattern, $target, 'POST', $middleware); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Registers a route with the PUT HTTP method. |
| 34 | */ |
| 35 | public function put(string $pattern, array|callable $target, array $middleware = []): void |
| 36 | { |
| 37 | $this->setRoute($pattern, $target, 'PUT', $middleware); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Registers a route with the PATCH HTTP method. |
| 42 | */ |
| 43 | public function patch(string $pattern, array|callable $target, array $middleware = []): void |
| 44 | { |
| 45 | $this->setRoute($pattern, $target, 'PATCH', $middleware); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Registers a route with the DELETE HTTP method. |
| 50 | */ |
| 51 | public function delete(string $pattern, array|callable $target, array $middleware = []): void |
| 52 | { |
| 53 | $this->setRoute($pattern, $target, 'DELETE', $middleware); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Registers a route that supports all HTTP methods. |
| 58 | * |
| 59 | * Sets the method to a pipe-separated string ('GET|POST|PUT|PATCH|DELETE'), |
| 60 | * allowing the same route to handle multiple request types. |
| 61 | */ |
| 62 | public function any(string $pattern, array|callable $target, array $middleware = []): void |
| 63 | { |
| 64 | $this->setRoute($pattern, $target, 'GET|POST|PUT|PATCH|DELETE', $middleware); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Registers a resource route, mapping standard actions to controller methods. |
| 69 | * |
| 70 | * Supports common CRUD operations by default: |
| 71 | * - GET => read |
| 72 | * - POST => create |
| 73 | * - PUT => update |
| 74 | * - DELETE => delete |
| 75 | * |
| 76 | * Can be customized with an optional $actions array. |
| 77 | */ |
| 78 | public function resource(string $pattern, string $controller, array $actions = ['read', 'create', 'update', 'delete']): void |
| 79 | { |
| 80 | $request = $this->rudra->request(); |
| 81 | $server = $request->server(); |
| 82 | $post = $request->post(); |
| 83 | |
| 84 | $requestMethod = $server->get('REQUEST_METHOD'); |
| 85 | $httpMethod = $requestMethod === 'POST' && $post->has('_method') |
| 86 | ? strtoupper($post->get('_method')) |
| 87 | : $requestMethod; |
| 88 | |
| 89 | switch ($httpMethod) { |
| 90 | case 'GET': |
| 91 | $route['method'] = 'GET'; |
| 92 | $route['action'] = $actions[0]; // read |
| 93 | break; |
| 94 | case 'POST': |
| 95 | $route['method'] = 'POST'; |
| 96 | $route['action'] = $actions[1]; // create |
| 97 | break; |
| 98 | case 'PUT': |
| 99 | case 'PATCH': |
| 100 | $route['method'] = $httpMethod; |
| 101 | $route['action'] = $actions[2]; // update |
| 102 | break; |
| 103 | case 'DELETE': |
| 104 | $route['method'] = 'DELETE'; |
| 105 | $route['action'] = $actions[3]; // delete |
| 106 | break; |
| 107 | default: |
| 108 | return; // Unknown method — ignore |
| 109 | } |
| 110 | |
| 111 | $route['url'] = $pattern; |
| 112 | $route['controller'] = $controller; |
| 113 | |
| 114 | $this->set($route); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * The method constructs a route definition and passes it to the `set()` method for registration. |
| 119 | */ |
| 120 | protected function setRoute(string $pattern, $target, string $httpMethod, array $middleware = []): void |
| 121 | { |
| 122 | $route['method'] = $httpMethod; |
| 123 | $route['url'] = $pattern; |
| 124 | |
| 125 | if (count($middleware)) { |
| 126 | if (array_key_exists('before', $middleware)) { |
| 127 | $route['middleware']['before'] = $middleware['before']; |
| 128 | } |
| 129 | |
| 130 | if (array_key_exists('after', $middleware)) { |
| 131 | $route['middleware']['after'] = $middleware['after']; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (is_callable($target)) { |
| 136 | $route['controller'] = $target; |
| 137 | } elseif (is_array($target)) { |
| 138 | $route['controller'] = $target[0]; |
| 139 | $route['action'] = $target[1]; |
| 140 | } |
| 141 | |
| 142 | $this->set($route); |
| 143 | } |
| 144 | } |
Bubble size = executable lines. Hover for details, click to navigate.
+Sorted by descending CRAP (Change Risk Anti-Patterns) index.
+| Class | +CRAP | +Coverage | +
|---|---|---|
| Rudra\Router\Traits\RouterRequestMethodTrait | 462 | 0.0% |
| Rudra\Router\Traits\RouterAnnotationTrait | 210 | 0.0% |
Bubble size = executable lines. Hover for details, click to navigate.
+Sorted by descending CRAP (Change Risk Anti-Patterns) index.
+| + | Code Coverage |
+ ||||||||
| + | Lines |
+ Functions and Methods |
+ Classes and Traits |
+ ||||||
| Total | +
+
+
+ |
+ 0.00% |
+ 0 / 82 |
+
+
+
+ |
+ 0.00% |
+ 0 / 10 |
+
+
+
+ |
+ 0.00% |
+ 0 / 2 |
+
|
+
+
+ |
+ 0.00% |
+ 0 / 35 |
+
+
+
+ |
+ 0.00% |
+ 0 / 2 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ |
|
+
+
+ |
+ 0.00% |
+ 0 / 47 |
+
+
+
+ |
+ 0.00% |
+ 0 / 8 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ |
Bubble size = executable lines. Hover for details, click to navigate.
+Sorted by descending CRAP (Change Risk Anti-Patterns) index.
+| Class | +CRAP | +Coverage | +
|---|---|---|
| Rudra\Router\Router | 2550 | 0.0% |
| Rudra\Router\Traits\RouterRequestMethodTrait | 462 | 0.0% |
| Rudra\Router\Traits\RouterAnnotationTrait | 210 | 0.0% |
| AfterMiddleware | 2 | 0.0% |
| Rudra\Router\Attributes\Middleware | 2 | 0.0% |
| Rudra\Router\Attributes\Routing | 2 | 0.0% |
| Rudra\Router\RouterFacade | 0 | 100.0% |
Bubble size = executable lines. Hover for details, click to navigate.
+Sorted by descending CRAP (Change Risk Anti-Patterns) index.
+| + | Code Coverage |
+ ||||||||
| + | Lines |
+ Functions and Methods |
+ Classes and Traits |
+ ||||||
| Total | +
+
+
+ |
+ 0.00% |
+ 0 / 202 |
+
+
+
+ |
+ 0.00% |
+ 0 / 22 |
+
+
+
+ |
+ 0.00% |
+ 0 / 6 |
+
|
+
+
+ |
+ 0.00% |
+ 0 / 3 |
+
+
+
+ |
+ 0.00% |
+ 0 / 3 |
+
+
+
+ |
+ 0.00% |
+ 0 / 3 |
+ |
|
+
+
+ |
+ 0.00% |
+ 0 / 82 |
+
+
+
+ |
+ 0.00% |
+ 0 / 10 |
+
+
+
+ |
+ 0.00% |
+ 0 / 2 |
+ |
|
+
+
+ |
+ 0.00% |
+ 0 / 117 |
+
+
+
+ |
+ 0.00% |
+ 0 / 9 |
+
+
+
+ |
+ 0.00% |
+ 0 / 1 |
+ |
| + | n/a |
+ 0 / 0 |
+ + | n/a |
+ 0 / 0 |
+ + | n/a |
+ 0 / 0 |
+ |
| + | n/a |
+ 0 / 0 |
+ + | n/a |
+ 0 / 0 |
+ + | n/a |
+ 0 / 0 |
+ |