1 | <?php |
||
27 | final class Router |
||
28 | { |
||
29 | const HTML = 1; |
||
30 | const JSON = 2; |
||
31 | const TEXT = 3; |
||
32 | const XML = 4; |
||
33 | const REDIRECT = 5; |
||
34 | const DOWNLOAD = 6; |
||
35 | const CUSTOM = 7; |
||
36 | |||
37 | const OPTIONS = 'OPTIONS'; |
||
38 | const HEAD = 'HEAD'; |
||
39 | const GET = 'GET'; |
||
40 | const POST = 'POST'; |
||
41 | const PUT = 'PUT'; |
||
42 | const DELETE = 'DELETE'; |
||
43 | const PATCH = 'PATCH'; |
||
44 | |||
45 | /** |
||
46 | * Routes array to be registered. |
||
47 | * Some routes may have aliases to be used in templating system |
||
48 | * Route item can be defined using array key as an alias key. |
||
49 | * Each route item is an array has items respectively: Request Method, Request Uri, Controller/Action, Return Type. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | private $routes = []; |
||
54 | |||
55 | /** |
||
56 | * Aliases array to be registered. |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | private $aliases = []; |
||
61 | |||
62 | /** |
||
63 | * HTTP request Method |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | private $method; |
||
68 | |||
69 | /** |
||
70 | * Request Uri |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | private $requestedPath; |
||
75 | |||
76 | /** |
||
77 | * Default return type if not noted in the $routes |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | private $defaultReturnType; |
||
82 | |||
83 | /** |
||
84 | * @var null|string |
||
85 | */ |
||
86 | private $cachedFile; |
||
87 | |||
88 | /** |
||
89 | * @var array |
||
90 | */ |
||
91 | private $routerClosures = []; |
||
92 | |||
93 | /** |
||
94 | * Valid Request Methods array. |
||
95 | * Make sures about requested methods. |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | private static $validRequestMethods = [ |
||
100 | 'GET', |
||
101 | 'OPTIONS', |
||
102 | 'HEAD', |
||
103 | 'POST', |
||
104 | 'PUT', |
||
105 | 'DELETE', |
||
106 | 'PATCH' |
||
107 | ]; |
||
108 | |||
109 | /** |
||
110 | * Router constructor. |
||
111 | * Create new router. |
||
112 | * |
||
113 | * @param int $defaultReturnType |
||
114 | * @param string $method |
||
115 | * @param string $requestedPath |
||
116 | * @param string $folder |
||
117 | * @param string $cachedFile |
||
118 | * @throws UnexpectedValueException |
||
119 | */ |
||
120 | public function __construct( |
||
136 | |||
137 | /** |
||
138 | * Remove sub folder from requestedPath if defined |
||
139 | * |
||
140 | * @param string $requestPath |
||
141 | * @param string $folder |
||
142 | * @return string |
||
143 | */ |
||
144 | private function extractFolder(string $requestPath, string $folder) : string |
||
154 | |||
155 | /** |
||
156 | * Add route to routes list |
||
157 | * |
||
158 | * @param string|array requestMethods |
||
159 | * @param string $route |
||
160 | * @param string $action |
||
161 | * @param int $returnType |
||
162 | * @param string $alias |
||
163 | * @throws InvalidArgumentException |
||
164 | * @throws UnexpectedValueException |
||
165 | */ |
||
166 | public function add( |
||
185 | |||
186 | /** |
||
187 | * @param string $method |
||
188 | * @param array $args |
||
189 | * @throws UnexpectedValueException |
||
190 | * @throws InvalidArgumentException |
||
191 | */ |
||
192 | public function __call(string $method, array $args) : void |
||
204 | |||
205 | /** |
||
206 | * @param int|null $returnType |
||
207 | * @return int |
||
208 | */ |
||
209 | private function determineReturnType(?int $returnType) : int |
||
216 | |||
217 | /** |
||
218 | * @param string $requestMethod |
||
219 | * Checks if request method is valid |
||
220 | * @throws UnexpectedValueException; |
||
221 | */ |
||
222 | private function checkRequestMethodIsValid(string $requestMethod) : void |
||
229 | |||
230 | /** |
||
231 | * @param $requestMethod |
||
232 | * @throws InvalidArgumentException |
||
233 | */ |
||
234 | private function checkRequestMethodParameterType($requestMethod) : void |
||
245 | |||
246 | /** |
||
247 | * Dispatch against the provided HTTP method verb and URI. |
||
248 | * |
||
249 | * @return FastRoute\Dispatcher |
||
250 | */ |
||
251 | private function dispatcher() : FastRoute\Dispatcher |
||
260 | |||
261 | private function simpleDispatcher() : FastRoute\Dispatcher\GroupCountBased |
||
279 | |||
280 | private function cachedDispatcher() : FastRoute\Dispatcher\GroupCountBased |
||
309 | |||
310 | /** |
||
311 | * Define Closures for all routes that returns controller info to be used. |
||
312 | * |
||
313 | * @param FastRoute\RouteCollector $route |
||
314 | */ |
||
315 | private function addRoutes(FastRoute\RouteCollector $route) : void |
||
339 | |||
340 | /** |
||
341 | * Get router data that includes route info and aliases |
||
342 | * |
||
343 | * @return array |
||
344 | */ |
||
345 | public function getRoute() : array |
||
356 | |||
357 | /** |
||
358 | * Get route info for requested uri |
||
359 | * |
||
360 | * @param array $routeInfo |
||
361 | * @return array $routerData |
||
362 | */ |
||
363 | private function runDispatcher(array $routeInfo) : array |
||
379 | |||
380 | /** |
||
381 | * Get routeData according to dispatcher's results |
||
382 | * |
||
383 | * @param array $routeInfo |
||
384 | * @return array |
||
385 | */ |
||
386 | private function getRouteData(array $routeInfo) : array |
||
399 | } |
||
400 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.