1 | <?php |
||
28 | final class Router |
||
29 | { |
||
30 | const HTML = 1; |
||
31 | const JSON = 2; |
||
32 | const TEXT = 3; |
||
33 | const XML = 4; |
||
34 | const REDIRECT = 5; |
||
35 | const DOWNLOAD = 6; |
||
36 | const CUSTOM = 7; |
||
37 | |||
38 | const OPTIONS = 'OPTIONS'; |
||
39 | const HEAD = 'HEAD'; |
||
40 | const GET = 'GET'; |
||
41 | const POST = 'POST'; |
||
42 | const PUT = 'PUT'; |
||
43 | const DELETE = 'DELETE'; |
||
44 | const PATCH = 'PATCH'; |
||
45 | |||
46 | /** |
||
47 | * Routes array to be registered. |
||
48 | * Some routes may have aliases to be used in templating system |
||
49 | * Route item can be defined using array key as an alias key. |
||
50 | * Each route item is an array has items respectively: Request Method, Request Uri, Controller/Action, Return Type. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | private $routes = []; |
||
55 | |||
56 | /** |
||
57 | * Aliases array to be registered. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | private $aliases = []; |
||
62 | |||
63 | /** |
||
64 | * HTTP request Method |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | private $method; |
||
69 | |||
70 | /** |
||
71 | * Request Uri |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | private $requestedPath; |
||
76 | |||
77 | /** |
||
78 | * Default return type if not noted in the $routes |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | private $defaultReturnType; |
||
83 | |||
84 | /** |
||
85 | * @var null|string |
||
86 | */ |
||
87 | private $cachedFile; |
||
88 | |||
89 | /** |
||
90 | * @var array |
||
91 | */ |
||
92 | private $routerClosures = []; |
||
93 | |||
94 | /** |
||
95 | * Valid Request Methods array. |
||
96 | * Make sures about requested methods. |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | private static $validRequestMethods = [ |
||
101 | 'GET', |
||
102 | 'OPTIONS', |
||
103 | 'HEAD', |
||
104 | 'POST', |
||
105 | 'PUT', |
||
106 | 'DELETE', |
||
107 | 'PATCH' |
||
108 | ]; |
||
109 | |||
110 | /** |
||
111 | * Router constructor. |
||
112 | * Create new router. |
||
113 | * |
||
114 | * @param int $defaultReturnType |
||
115 | * @param string $method |
||
116 | * @param string $requestedPath |
||
117 | * @param string $folder |
||
118 | * @param string $cachedFile |
||
119 | * @throws UnexpectedValueException |
||
120 | */ |
||
121 | public function __construct( |
||
137 | |||
138 | /** |
||
139 | * Remove sub folder from requestedPath if defined |
||
140 | * |
||
141 | * @param string $requestPath |
||
142 | * @param string $folder |
||
143 | * @return string |
||
144 | */ |
||
145 | private function extractFolder(string $requestPath, string $folder) : string |
||
155 | |||
156 | /** |
||
157 | * Add route to routes list |
||
158 | * |
||
159 | * @param string|array requestMethods |
||
160 | * @param string $route |
||
161 | * @param string $action |
||
162 | * @param int $returnType |
||
163 | * @param string $alias |
||
164 | * @throws InvalidArgumentException |
||
165 | * @throws UnexpectedValueException |
||
166 | */ |
||
167 | public function add( |
||
186 | |||
187 | /** |
||
188 | * @param string $method |
||
189 | * @param array $args |
||
190 | * @throws UnexpectedValueException |
||
191 | * @throws InvalidArgumentException |
||
192 | */ |
||
193 | public function __call(string $method, array $args) : void |
||
205 | |||
206 | /** |
||
207 | * @param int|null $returnType |
||
208 | * @return int |
||
209 | */ |
||
210 | private function determineReturnType(?int $returnType) : int |
||
217 | |||
218 | /** |
||
219 | * @param string $requestMethod |
||
220 | * Checks if request method is valid |
||
221 | * @throws UnexpectedValueException; |
||
222 | */ |
||
223 | private function checkRequestMethodIsValid(string $requestMethod) : void |
||
230 | |||
231 | /** |
||
232 | * @param $requestMethod |
||
233 | * @throws InvalidArgumentException |
||
234 | */ |
||
235 | private function checkRequestMethodParameterType($requestMethod) : void |
||
246 | |||
247 | /** |
||
248 | * Dispatch against the provided HTTP method verb and URI. |
||
249 | * |
||
250 | * @return FastRoute\Dispatcher |
||
251 | * @throws RuntimeException; |
||
252 | */ |
||
253 | private function dispatcher() : FastRoute\Dispatcher |
||
270 | |||
271 | private function createCachedRoute($routeCollector) : void |
||
281 | |||
282 | /** |
||
283 | * @return FastRoute\Dispatcher\GroupCountBased |
||
284 | * @throws RuntimeException |
||
285 | */ |
||
286 | private function cachedDispatcher() : FastRoute\Dispatcher\GroupCountBased |
||
294 | |||
295 | /** |
||
296 | * Define Closures for all routes that returns controller info to be used. |
||
297 | * |
||
298 | * @param FastRoute\RouteCollector $route |
||
299 | */ |
||
300 | private function addRoutes(FastRoute\RouteCollector $route) : void |
||
324 | |||
325 | /** |
||
326 | * Get router data that includes route info and aliases |
||
327 | * |
||
328 | * @return array |
||
329 | * @throws RuntimeException |
||
330 | */ |
||
331 | public function getRoute() : array |
||
342 | |||
343 | /** |
||
344 | * Get route info for requested uri |
||
345 | * |
||
346 | * @param array $routeInfo |
||
347 | * @return array $routerData |
||
348 | */ |
||
349 | private function runDispatcher(array $routeInfo) : array |
||
365 | |||
366 | /** |
||
367 | * Get routeData according to dispatcher's results |
||
368 | * |
||
369 | * @param array $routeInfo |
||
370 | * @return array |
||
371 | */ |
||
372 | private function getRouteData(array $routeInfo) : array |
||
385 | } |
||
386 |
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.