1 | <?php |
||
26 | final class Router |
||
27 | { |
||
28 | /** |
||
29 | * routes array to be registered. |
||
30 | * Some routes may have aliases to be used in templating system |
||
31 | * Route item can be defined using array key as an alias key. |
||
32 | * @var array |
||
33 | */ |
||
34 | private $routes = []; |
||
35 | |||
36 | /** |
||
37 | * aliases array to be registered. |
||
38 | * Each route item is an array has items respectively : Request Method, Request Uri, Controller/Action, Return Type. |
||
39 | * @var array |
||
40 | */ |
||
41 | private $aliases = []; |
||
42 | |||
43 | /** |
||
44 | * HTTP request Method |
||
45 | * @var string |
||
46 | */ |
||
47 | private $method; |
||
48 | |||
49 | /** |
||
50 | * Request Uri |
||
51 | * @var string |
||
52 | */ |
||
53 | private $requestedPath; |
||
54 | |||
55 | /** |
||
56 | * Default return type if not noted in the $routes |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | private $defaultReturnType; |
||
61 | |||
62 | /** |
||
63 | * @var null|string |
||
64 | */ |
||
65 | private $cachedFile; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | private $routerClosures = []; |
||
71 | |||
72 | /** |
||
73 | * Translation array. |
||
74 | * Make sures about return type. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | private static $translations = [ |
||
79 | 'h' => 'html', |
||
80 | 'html' => 'html', |
||
81 | 'r' => 'redirect', |
||
82 | 'redirect' => 'redirect', |
||
83 | 'j' => 'json', |
||
84 | 'json' => 'json', |
||
85 | 't' => 'text', |
||
86 | 'text' => 'text', |
||
87 | 'd' => 'download', |
||
88 | 'download' => 'download' |
||
89 | ]; |
||
90 | |||
91 | /** |
||
92 | * Valid Request Methods array. |
||
93 | * Make sures about requested methods. |
||
94 | * @var array |
||
95 | */ |
||
96 | private static $validRequestMethods = [ |
||
97 | 'GET', |
||
98 | 'OPTIONS', |
||
99 | 'HEAD', |
||
100 | 'POST', |
||
101 | 'PUT', |
||
102 | 'DELETE', |
||
103 | 'PATCH' |
||
104 | ]; |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Valid Request Methods array. |
||
109 | * Make sures about return type. |
||
110 | * Index 0 is also default value. |
||
111 | * @var array |
||
112 | */ |
||
113 | private static $validReturnTypes = [ |
||
114 | 'html', |
||
115 | 'json', |
||
116 | 'text', |
||
117 | 'redirect', |
||
118 | 'download' |
||
119 | ]; |
||
120 | |||
121 | /** |
||
122 | * Router constructor. |
||
123 | * Create new router. |
||
124 | * |
||
125 | * @param string $defaultReturnType |
||
126 | * @param string $method |
||
127 | * @param string $requestedPath |
||
128 | * @param string $folder |
||
129 | * @param string $cachedFile |
||
130 | * @throws UnexpectedValueException |
||
131 | */ |
||
132 | public function __construct( |
||
148 | |||
149 | /** |
||
150 | * Remove sub folder from requestedPath if defined |
||
151 | * @param string $requestPath |
||
152 | * @param string $folder |
||
153 | * @return string |
||
154 | */ |
||
155 | private function extractFolder(string $requestPath, string $folder) : string |
||
165 | |||
166 | /** |
||
167 | * add route to routes list |
||
168 | * @param string|array requestMethods |
||
169 | * @param string $route |
||
170 | * @param string $action |
||
171 | * @param string $returnType |
||
172 | * @param string $alias |
||
173 | * @throws InvalidArgumentException |
||
174 | * @throws UnexpectedValueException |
||
175 | */ |
||
176 | public function add($requestMethods, string $route, string $action, ?string $returnType = null, ?string $alias = null) |
||
189 | |||
190 | /** |
||
191 | * @param string $method |
||
192 | * @param array $args |
||
193 | * @throws UnexpectedValueException |
||
194 | * @throws InvalidArgumentException |
||
195 | */ |
||
196 | public function __call(string $method, array $args) : void |
||
209 | |||
210 | /** |
||
211 | * @param string|null $returnType |
||
212 | * @return string |
||
213 | */ |
||
214 | private function determineReturnType(?string $returnType) : string |
||
221 | |||
222 | /** |
||
223 | * @param string $requestMethod |
||
224 | * Checks if request method is valid |
||
225 | * @throws UnexpectedValueException; |
||
226 | */ |
||
227 | private function checkRequestMethodIsValid(string $requestMethod) : void |
||
234 | |||
235 | /** |
||
236 | * @param $requestMethod |
||
237 | * @throws InvalidArgumentException |
||
238 | */ |
||
239 | private function checkRequestMethodParameterType($requestMethod) : void |
||
250 | |||
251 | /** |
||
252 | * Dispatch against the provided HTTP method verb and URI. |
||
253 | * @return FastRoute\Dispatcher |
||
254 | */ |
||
255 | private function dispatcher() : FastRoute\Dispatcher |
||
262 | |||
263 | private function simpleDispatcher() |
||
279 | |||
280 | private function cachedDispatcher() |
||
307 | |||
308 | /** |
||
309 | * Define Closures for all routes that returns controller info to be used. |
||
310 | * @param FastRoute\RouteCollector $route |
||
311 | */ |
||
312 | private function addRoutes(FastRoute\RouteCollector $route) : void |
||
327 | |||
328 | |||
329 | |||
330 | /** |
||
331 | * Get router data that includes route info and aliases |
||
332 | * @return array |
||
333 | */ |
||
334 | public function getRoute() : array |
||
345 | |||
346 | |||
347 | /** |
||
348 | * Get route info for requested uri |
||
349 | * @param array $routeInfo |
||
350 | * @return array $routerData |
||
351 | */ |
||
352 | private function runDispatcher(array $routeInfo) : array |
||
368 | |||
369 | /** |
||
370 | * Get routeData according to dispatcher's results |
||
371 | * @param array $routeInfo |
||
372 | * @return array |
||
373 | */ |
||
374 | private function getRouteData(array $routeInfo) : array |
||
387 | } |
||
388 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.