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 | * Make sures about requested methods. |
||
80 | * @var array |
||
81 | */ |
||
82 | private static $validRequestMethods = [ |
||
83 | 'GET', |
||
84 | 'OPTIONS', |
||
85 | 'HEAD', |
||
86 | 'POST', |
||
87 | 'PUT', |
||
88 | 'DELETE', |
||
89 | 'PATCH' |
||
90 | ]; |
||
91 | |||
92 | |||
93 | /** |
||
94 | * Valid Request Methods array. |
||
95 | * Make sures about return type. |
||
96 | * @var array |
||
97 | */ |
||
98 | private static $validReturnTypes = [ |
||
99 | 'html', |
||
100 | 'json', |
||
101 | 'text', |
||
102 | 'redirect', |
||
103 | 'download' |
||
104 | ]; |
||
105 | |||
106 | /** |
||
107 | * Router constructor. |
||
108 | * Create new router. |
||
109 | * |
||
110 | * @param array $routes |
||
|
|||
111 | * @param string $defaultReturnType |
||
112 | * @param string $method |
||
113 | * @param string $requestedPath |
||
114 | * @param string $folder |
||
115 | * @throws InvalidArgumentException |
||
116 | */ |
||
117 | 7 | public function __construct( |
|
118 | string $defaultReturnType, |
||
119 | string $method, |
||
120 | string $requestedPath, |
||
121 | string $folder = '' |
||
122 | ) { |
||
123 | 7 | if (!in_array($method, self::$validRequestMethods, true)) { |
|
124 | $message = sprintf('%s is nat valid Http request method.', $method); |
||
125 | throw new InvalidArgumentException($message); |
||
126 | } |
||
127 | 7 | $this->method = $method; |
|
128 | 7 | $this->requestedPath = $this->extractFolder($requestedPath, $folder); |
|
129 | 7 | $this->defaultReturnType = self::$translations[$defaultReturnType] ?? self::$defaultReturnType[0]; |
|
130 | 7 | } |
|
131 | |||
132 | /** |
||
133 | * Remove sub folder from requestedPath if defined |
||
134 | * @param string $requestPath |
||
135 | * @param string $folder |
||
136 | * @return string |
||
137 | */ |
||
138 | 7 | private function extractFolder(string $requestPath, string $folder) |
|
148 | |||
149 | /** |
||
150 | * add route to routes list |
||
151 | * @param string|array requestMethods |
||
152 | * @param string $route |
||
153 | * @param string $action |
||
154 | * @param string $returnType |
||
155 | * @param string $alias |
||
156 | * @return string |
||
157 | * @throws InvalidArgumentException |
||
158 | */ |
||
159 | 7 | public function add($requestMethods, string $route, string $action, string $returnType=null, string $alias=null) |
|
160 | { |
||
161 | 7 | $requestMethodParameterType = gettype($requestMethods); |
|
162 | 7 | if (!in_array($requestMethodParameterType, ['array', 'string'], true)) { |
|
163 | $message = sprintf( |
||
164 | 'Request method must be either string or array but %s given.', |
||
165 | $requestMethodParameterType); |
||
166 | throw new InvalidArgumentException($message); |
||
167 | } |
||
168 | 7 | $requestMethodsGiven = is_array($requestMethods) ? (array) $requestMethods : [0 => $requestMethods]; |
|
169 | 7 | $returnType = $returnType === null ? $this->defaultReturnType: self::$validReturnTypes[$returnType]?? $this->defaultReturnType; |
|
170 | 7 | foreach ($requestMethodsGiven as $requestMethod) { |
|
171 | 7 | if ($alias !== null) { |
|
172 | 7 | $this->aliases[$alias] = $route; |
|
173 | } |
||
174 | 7 | $this->routes[] = [strtoupper($requestMethod), $route, $action, $returnType]; |
|
175 | } |
||
176 | 7 | } |
|
177 | |||
178 | /** |
||
179 | * Dispatch against the provided HTTP method verb and URI. |
||
180 | * @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 | 3 | public function getRoute() |
|
229 | |||
230 | |||
231 | /** |
||
232 | * Get route info for requested uri |
||
233 | * @param array $routeInfo |
||
234 | * @return array $routerData |
||
235 | */ |
||
236 | 3 | private function runDispatcher(array $routeInfo) |
|
252 | |||
253 | /** |
||
254 | * Get routeData according to dispatcher's results |
||
255 | * @param array $routeInfo |
||
256 | * @return array |
||
257 | */ |
||
258 | 3 | 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.