1 | <?php |
||
23 | final class Router |
||
24 | { |
||
25 | /** |
||
26 | * routes array to be registered. |
||
27 | * Some routes may have aliases to be used in templating system |
||
28 | * Route item can be defined using array key as an alias key. |
||
29 | * @var array |
||
30 | */ |
||
31 | private $routes = []; |
||
32 | |||
33 | /** |
||
34 | * aliases array to be registered. |
||
35 | * Each route item is an array has items respectively : Request Method, Request Uri, Controller/Action, Return Type. |
||
36 | * @var array |
||
37 | */ |
||
38 | private $aliases = []; |
||
39 | |||
40 | /** |
||
41 | * HTTP request Method |
||
42 | * @var string |
||
43 | */ |
||
44 | private $method; |
||
45 | |||
46 | /** |
||
47 | * Request Uri |
||
48 | * @var string |
||
49 | */ |
||
50 | private $requestedPath; |
||
51 | |||
52 | /** |
||
53 | * Default return type if not noted in the $routes |
||
54 | * @var string |
||
55 | */ |
||
56 | private $defaultReturnType; |
||
57 | |||
58 | |||
59 | /** |
||
60 | * Translation array. |
||
61 | * Make sures about return type. |
||
62 | * @var array |
||
63 | */ |
||
64 | private static $translations = [ |
||
65 | 'h' => 'html', |
||
66 | 'html' => 'html', |
||
67 | 'r' => 'redirect', |
||
68 | 'redirect' => 'redirect', |
||
69 | 'j' => 'json', |
||
70 | 'json' => 'json', |
||
71 | 't' => 'text', |
||
72 | 'text' => 'text', |
||
73 | 'd' => 'download', |
||
74 | 'download' => 'download' |
||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * Valid Request Methods array. |
||
79 | 8 | * Make sures about requested methods. |
|
80 | * @var array |
||
81 | */ |
||
82 | private static $validRequestMethods = [ |
||
83 | 'GET', |
||
84 | 'OPTIONS', |
||
85 | 'HEAD', |
||
86 | 'POST', |
||
87 | 8 | 'PUT', |
|
88 | 8 | 'DELETE', |
|
89 | 8 | 'PATCH' |
|
90 | 8 | ]; |
|
91 | 8 | ||
92 | |||
93 | /** |
||
94 | * Valid Request Methods array. |
||
95 | * Make sures about return type. |
||
96 | * @var array |
||
97 | */ |
||
98 | private static $validReturnTypes = [ |
||
99 | 8 | 'html', |
|
100 | 'json', |
||
101 | 8 | 'text', |
|
102 | 1 | 'redirect', |
|
103 | 'download' |
||
104 | 8 | ]; |
|
105 | 1 | ||
106 | /** |
||
107 | 8 | * Router constructor. |
|
108 | * Create new router. |
||
109 | * |
||
110 | * @param array $routes |
||
|
|||
111 | * @param string $defaultReturnType |
||
112 | * @param string $method |
||
113 | * @param string $requestedPath |
||
114 | 3 | * @param string $folder |
|
115 | * @throws InvalidArgumentException |
||
116 | */ |
||
117 | 3 | public function __construct( |
|
131 | |||
132 | /** |
||
133 | * Remove sub folder from requestedPath if defined |
||
134 | 3 | * @param string $requestPath |
|
135 | * @param string $folder |
||
136 | 3 | * @return string |
|
137 | 3 | */ |
|
138 | 3 | private function extractFolder(string $requestPath, string $folder) |
|
148 | |||
149 | /** |
||
150 | 4 | * add route to routes list |
|
151 | * @param string|array requestMethods |
||
152 | 4 | * @param string $route |
|
153 | 4 | * @param string $action |
|
154 | 4 | * @param string $returnType |
|
155 | 4 | * @param string $alias |
|
156 | * @return string |
||
157 | * @throws InvalidArgumentException |
||
158 | 4 | */ |
|
159 | public function add($requestMethods, string $route, string $action, string $returnType=null, string $alias=null) |
||
177 | |||
178 | /** |
||
179 | * Dispatch against the provided HTTP method verb and URI. |
||
180 | 3 | * @return array |
|
181 | */ |
||
182 | 3 | private function dispatcher() |
|
197 | |||
198 | /** |
||
199 | * Define Closures for all routes that returns controller info to be used. |
||
200 | * @param FastRoute\RouteCollector $route |
||
201 | */ |
||
202 | 3 | private function addRoutes(FastRoute\RouteCollector $route) |
|
213 | |||
214 | |||
215 | |||
216 | /** |
||
217 | * Get router data that includes route info and aliases |
||
218 | */ |
||
219 | public function getRoute() |
||
229 | |||
230 | |||
231 | /** |
||
232 | * Get route info for requested uri |
||
233 | * @param array $routeInfo |
||
234 | * @return array $routerData |
||
235 | */ |
||
236 | private function runDispatcher(array $routeInfo) |
||
252 | |||
253 | /** |
||
254 | * Get routeData according to dispatcher's results |
||
255 | * @param array $routeInfo |
||
256 | * @return array |
||
257 | */ |
||
258 | private function getRouteData(array $routeInfo) |
||
271 | } |
||
272 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.