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 | * @var string |
||
58 | */ |
||
59 | private $defaultReturnType; |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Translation array. |
||
64 | * Make sures about return type. |
||
65 | * @var array |
||
66 | */ |
||
67 | private static $translations = [ |
||
68 | 'h' => 'html', |
||
69 | 'html' => 'html', |
||
70 | 'r' => 'redirect', |
||
71 | 'redirect' => 'redirect', |
||
72 | 'j' => 'json', |
||
73 | 'json' => 'json', |
||
74 | 't' => 'text', |
||
75 | 'text' => 'text', |
||
76 | 'd' => 'download', |
||
77 | 'download' => 'download' |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * Valid Request Methods array. |
||
82 | * Make sures about requested methods. |
||
83 | * @var array |
||
84 | */ |
||
85 | private static $validRequestMethods = [ |
||
86 | 'GET', |
||
87 | 'OPTIONS', |
||
88 | 'HEAD', |
||
89 | 'POST', |
||
90 | 'PUT', |
||
91 | 'DELETE', |
||
92 | 'PATCH' |
||
93 | ]; |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Valid Request Methods array. |
||
98 | * Make sures about return type. |
||
99 | * Index 0 is also default value. |
||
100 | * @var array |
||
101 | */ |
||
102 | private static $validReturnTypes = [ |
||
103 | 'html', |
||
104 | 'json', |
||
105 | 'text', |
||
106 | 'redirect', |
||
107 | 'download' |
||
108 | ]; |
||
109 | |||
110 | /** |
||
111 | * Router constructor. |
||
112 | * Create new router. |
||
113 | * |
||
114 | * @param string $defaultReturnType |
||
115 | * @param string $method |
||
116 | * @param string $requestedPath |
||
117 | * @param string $folder |
||
118 | * @throws UnexpectedValueException |
||
119 | */ |
||
120 | public function __construct( |
||
134 | |||
135 | /** |
||
136 | * Remove sub folder from requestedPath if defined |
||
137 | * @param string $requestPath |
||
138 | * @param string $folder |
||
139 | * @return string |
||
140 | */ |
||
141 | private function extractFolder(string $requestPath, string $folder) : string |
||
151 | |||
152 | /** |
||
153 | * add route to routes list |
||
154 | * @param string|array requestMethods |
||
155 | * @param string $route |
||
156 | * @param string $action |
||
157 | * @param string $returnType |
||
158 | * @param string $alias |
||
159 | * @throws InvalidArgumentException |
||
160 | * @throws UnexpectedValueException |
||
161 | */ |
||
162 | public function add($requestMethods, string $route, string $action, ?string $returnType = null, ?string $alias = null) |
||
175 | |||
176 | /** |
||
177 | * @param string $method |
||
178 | * @param array $args |
||
179 | * @throws UnexpectedValueException |
||
180 | * @throws InvalidArgumentException |
||
181 | */ |
||
182 | public function __call(string $method, array $args) : void |
||
195 | |||
196 | /** |
||
197 | * @param string|null $returnType |
||
198 | * @return string |
||
199 | */ |
||
200 | private function determineReturnType(?string $returnType) : string |
||
207 | |||
208 | /** |
||
209 | * @param string $requestMethod |
||
210 | * Checks if request method is valid |
||
211 | * @throws UnexpectedValueException; |
||
212 | */ |
||
213 | private function checkRequestMethodIsValid(string $requestMethod) : void |
||
220 | |||
221 | /** |
||
222 | * @param $requestMethod |
||
223 | * @throws InvalidArgumentException |
||
224 | */ |
||
225 | private function checkRequestMethodParameterType($requestMethod) : void |
||
236 | |||
237 | /** |
||
238 | * Dispatch against the provided HTTP method verb and URI. |
||
239 | * @return array |
||
240 | */ |
||
241 | private function dispatcher() |
||
256 | |||
257 | /** |
||
258 | * Define Closures for all routes that returns controller info to be used. |
||
259 | * @param FastRoute\RouteCollector $route |
||
260 | */ |
||
261 | private function addRoutes(FastRoute\RouteCollector $route) |
||
272 | |||
273 | |||
274 | |||
275 | /** |
||
276 | * Get router data that includes route info and aliases |
||
277 | * @return array |
||
278 | */ |
||
279 | public function getRoute() : array |
||
289 | |||
290 | |||
291 | /** |
||
292 | * Get route info for requested uri |
||
293 | * @param array $routeInfo |
||
294 | * @return array $routerData |
||
295 | */ |
||
296 | private function runDispatcher(array $routeInfo) : array |
||
312 | |||
313 | /** |
||
314 | * Get routeData according to dispatcher's results |
||
315 | * @param array $routeInfo |
||
316 | * @return array |
||
317 | */ |
||
318 | private function getRouteData(array $routeInfo) : array |
||
331 | } |
||
332 |
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.