Total Complexity | 60 |
Total Lines | 403 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 1 | Features | 0 |
Complex classes like Router often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class Router extends RouteController |
||
36 | { |
||
37 | |||
38 | const VALID_PARAM_NAME_PATTERN = '/^[a-zA-Z]+$/'; |
||
39 | |||
40 | /** |
||
41 | * Parameter types |
||
42 | */ |
||
43 | const PARAM_TYPES = [ |
||
44 | ':alpha' => '[a-zA-Z]', |
||
45 | ':num' => '[0-9]', |
||
46 | ':any' => '[^\/]' |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * Request instance |
||
51 | * @var Request; |
||
52 | */ |
||
53 | private $request; |
||
54 | |||
55 | /** |
||
56 | * Response instance |
||
57 | * @var Response; |
||
58 | */ |
||
59 | private $response; |
||
60 | |||
61 | /** |
||
62 | * List of routes |
||
63 | * @var array |
||
64 | */ |
||
65 | protected static $routes = []; |
||
66 | |||
67 | /** |
||
68 | * matched routes |
||
69 | * @var array |
||
70 | */ |
||
71 | private $matchedRoutes = []; |
||
72 | |||
73 | /** |
||
74 | * Router constructor. |
||
75 | * @param Request $request |
||
76 | * @param Response $response |
||
77 | */ |
||
78 | public function __construct(Request $request, Response $response) |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Finds the current route |
||
86 | * @throws StopExecutionException |
||
87 | * @throws ViewException |
||
88 | * @throws DiException |
||
89 | * @throws LoaderError |
||
90 | * @throws RuntimeError |
||
91 | * @throws SyntaxError |
||
92 | * @throws RouteException |
||
93 | * @throws ReflectionException |
||
94 | */ |
||
95 | public function findRoute() |
||
96 | { |
||
97 | $uri = $this->request->getUri(); |
||
98 | |||
99 | if (!$uri) { |
||
100 | throw RouteException::notFound(); |
||
101 | } |
||
102 | |||
103 | $this->matchedRoutes = $this->findMatches($uri); |
||
104 | |||
105 | if (empty($this->matchedRoutes)) { |
||
106 | stop(function () { |
||
107 | $this->handleNotFound(); |
||
108 | }); |
||
109 | } |
||
110 | |||
111 | if (count($this->matchedRoutes) > 1) { |
||
112 | $this->checkCollision(); |
||
113 | } |
||
114 | |||
115 | $currentRoute = $this->currentRoute(); |
||
116 | |||
117 | if (!$currentRoute) { |
||
118 | throw RouteException::incorrectMethod($this->request->getMethod()); |
||
119 | } |
||
120 | |||
121 | if (!empty($currentRoute['cache_settings']) && key_exists('shouldCache', $currentRoute)){ |
||
122 | $viewCacheInstance = ViewCache::getInstance(); |
||
123 | |||
124 | $viewCacheInstance->enableCaching($currentRoute['cache_settings']['shouldCache']); |
||
125 | |||
126 | if (!empty($currentRoute['cache_settings']['ttl'])){ |
||
127 | $viewCacheInstance->setTtl($currentRoute['cache_settings']['ttl']); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | self::setCurrentRoute($currentRoute); |
||
132 | |||
133 | if (filter_var(config()->get(Debugger::DEBUG_ENABLED), FILTER_VALIDATE_BOOLEAN)) { |
||
134 | $this->collectDebugData($currentRoute); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Resets the routes |
||
140 | */ |
||
141 | public function resetRoutes() |
||
142 | { |
||
143 | parent::$currentRoute = null; |
||
144 | $this->matchedRoutes = []; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Gets the current route |
||
149 | * @return array|null |
||
150 | */ |
||
151 | private function currentRoute(): ?array |
||
152 | { |
||
153 | foreach ($this->matchedRoutes as $matchedRoute) { |
||
154 | if ($this->checkMethod($matchedRoute)) { |
||
155 | return $matchedRoute; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | return null; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Collects debug data |
||
164 | * @param array $currentRoute |
||
165 | * @return void |
||
166 | */ |
||
167 | private function collectDebugData(array $currentRoute) |
||
168 | { |
||
169 | $routeInfo = []; |
||
170 | |||
171 | foreach ($currentRoute as $key => $value) { |
||
172 | $routeInfo[ucfirst($key)] = json_encode($value); |
||
173 | } |
||
174 | |||
175 | Debugger::addToStore(Debugger::ROUTES, LogLevel::INFO, $routeInfo); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Finds matches by pattern |
||
180 | * @param string $uri |
||
181 | * @return array |
||
182 | * @throws RouteException |
||
183 | */ |
||
184 | private function findMatches(string $uri): array |
||
185 | { |
||
186 | $requestUri = urldecode(parse_url($uri, PHP_URL_PATH)); |
||
187 | |||
188 | $matches = []; |
||
189 | |||
190 | foreach (self::$routes as $route) { |
||
191 | list($pattern, $params) = $this->handleRoutePattern($route); |
||
192 | |||
193 | if (preg_match("/^" . $this->escape($pattern) . "$/u", $requestUri, $matchedParams)) { |
||
194 | $route['uri'] = $uri; |
||
195 | $route['params'] = $this->routeParams($params, $matchedParams); |
||
196 | $route['pattern'] = $pattern; |
||
197 | $matches[] = $route; |
||
198 | } |
||
199 | } |
||
200 | |||
201 | return $matches; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Handles the route pattern |
||
206 | * @param array $route |
||
207 | * @return array |
||
208 | * @throws RouteException |
||
209 | */ |
||
210 | private function handleRoutePattern(array $route): array |
||
211 | { |
||
212 | $routeSegments = explode('/', trim($route['route'], '/')); |
||
213 | |||
214 | $routePattern = '(\/)?'; |
||
215 | $routeParams = []; |
||
216 | |||
217 | $lastIndex = (int)array_key_last($routeSegments); |
||
218 | |||
219 | foreach ($routeSegments as $index => $segment) { |
||
220 | $segmentParam = $this->getSegmentParam($segment, $index, $lastIndex); |
||
221 | |||
222 | if (!empty($segmentParam)) { |
||
223 | $this->checkParamName($routeParams, $segmentParam['name']); |
||
224 | |||
225 | $routeParams[] = [ |
||
226 | 'route_pattern' => $segment, |
||
227 | 'pattern' => $segmentParam['pattern'], |
||
228 | 'name' => $segmentParam['name'] |
||
229 | ]; |
||
230 | |||
231 | $routePattern = $this->normalizePattern($routePattern, $segmentParam, $index, $lastIndex); |
||
232 | } else { |
||
233 | $routePattern .= $segment . ($index != $lastIndex ? '(\/)' : ''); |
||
234 | } |
||
235 | } |
||
236 | |||
237 | return [ |
||
238 | $routePattern, |
||
239 | $routeParams |
||
240 | ]; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Normalize the pattern |
||
245 | * @param string $routePattern |
||
246 | * @param array $segmentParam |
||
247 | * @param int $index |
||
248 | * @param int $lastIndex |
||
249 | * @return string |
||
250 | */ |
||
251 | private function normalizePattern(string $routePattern, array $segmentParam, int $index, int $lastIndex): string |
||
252 | { |
||
253 | if ($index == $lastIndex) { |
||
254 | if (mb_substr($routePattern, -5) == '(\/)?') { |
||
255 | $routePattern = mb_substr($routePattern, 0, mb_strlen($routePattern) - 5); |
||
256 | } elseif (mb_substr($routePattern, -4) == '(\/)') { |
||
257 | $routePattern = mb_substr($routePattern, 0, mb_strlen($routePattern) - 4); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | return $routePattern . $segmentParam['pattern']; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Gets the route parameters |
||
266 | * @param array $params |
||
267 | * @param array $arguments |
||
268 | * @return array |
||
269 | */ |
||
270 | private function routeParams(array $params, array $arguments): array |
||
271 | { |
||
272 | $arguments = array_diff($arguments, ['', '/']); |
||
273 | |||
274 | foreach ($params as &$param) { |
||
275 | $param['value'] = $arguments[$param['name']] ?? null; |
||
276 | if (mb_substr($param['name'], 0, 1) == '_') { |
||
277 | $param['name'] = null; |
||
278 | } |
||
279 | } |
||
280 | |||
281 | return $params; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Checks the segment for parameter |
||
286 | * @param string $segment |
||
287 | * @param int $index |
||
288 | * @param int $lastIndex |
||
289 | * @return array |
||
290 | * @throws RouteException |
||
291 | */ |
||
292 | private function getSegmentParam(string $segment, int $index, int $lastIndex): array |
||
293 | { |
||
294 | foreach (self::PARAM_TYPES as $type => $expr) { |
||
295 | if (preg_match('/\[(.*=)*(' . $type . ')(:([0-9]+))*\](\?)?/', $segment, $match)) { |
||
296 | return $this->getParamPattern($match, $expr, $index, $lastIndex); |
||
297 | } |
||
298 | } |
||
299 | |||
300 | return []; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Checks the parameter name availability |
||
305 | * @param array $routeParams |
||
306 | * @param string $name |
||
307 | * @throws RouteException |
||
308 | */ |
||
309 | private function checkParamName(array $routeParams, string $name) |
||
310 | { |
||
311 | foreach ($routeParams as $param) { |
||
312 | if ($param['name'] == $name) { |
||
313 | throw RouteException::paramNameNotAvailable($name); |
||
314 | } |
||
315 | } |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Finds pattern for parameter |
||
320 | * @param array $match |
||
321 | * @param string $expr |
||
322 | * @param int $index |
||
323 | * @param int $lastIndex |
||
324 | * @return array |
||
325 | * @throws RouteException |
||
326 | */ |
||
327 | private function getParamPattern(array $match, string $expr, int $index, int $lastIndex): array |
||
328 | { |
||
329 | $name = $this->getParamName($match, $index); |
||
330 | |||
331 | $pattern = '(?<' . $name . '>' . $expr; |
||
332 | |||
333 | if (isset($match[4]) && is_numeric($match[4])) { |
||
334 | $pattern .= (isset($match[5]) && $match[5] == '?') ? '{0,' . $match[4] . '})' : '{' . $match[4] . '})'; |
||
335 | } else { |
||
336 | $pattern .= (isset($match[5]) && $match[5] == '?') ? '*)' : '+)'; |
||
337 | } |
||
338 | |||
339 | if (isset($match[5]) && $match[5] == '?') { |
||
340 | $pattern = ($index == $lastIndex ? '(\/)?' . $pattern : $pattern . '(\/)?'); |
||
341 | } else { |
||
342 | $pattern = ($index == $lastIndex ? '(\/)' . $pattern : $pattern . '(\/)'); |
||
343 | } |
||
344 | |||
345 | return [ |
||
346 | 'name' => $name, |
||
347 | 'pattern' => $pattern |
||
348 | ]; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Gets the parameter name |
||
353 | * @param array $match |
||
354 | * @param int $index |
||
355 | * @return string |
||
356 | * @throws RouteException |
||
357 | */ |
||
358 | private function getParamName(array $match, int $index): string |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Checks the route collisions |
||
375 | * @throws RouteException |
||
376 | */ |
||
377 | private function checkCollision() |
||
388 | } |
||
389 | } |
||
390 | } |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Checks the request method against defined route method |
||
395 | * @param array $matchedRoute |
||
396 | * @return bool |
||
397 | */ |
||
398 | private function checkMethod(array $matchedRoute): bool |
||
399 | { |
||
400 | $allowedMethods = explode('|', $matchedRoute['method']); |
||
401 | |||
402 | return in_array($this->request->getMethod(), $allowedMethods, true); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Escapes the slashes |
||
407 | * @param string $str |
||
408 | * @return string |
||
409 | */ |
||
410 | private function escape(string $str): string |
||
411 | { |
||
412 | return str_replace('/', '\/', stripslashes($str)); |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Handles page not found |
||
417 | * @throws DiException |
||
418 | * @throws ViewException |
||
419 | * @throws ReflectionException |
||
420 | * @throws LoaderError |
||
421 | * @throws RuntimeError |
||
422 | * @throws SyntaxError |
||
423 | */ |
||
424 | private function handleNotFound() |
||
438 | ); |
||
439 | } |
||
440 | } |
||
441 | |||
442 | } |
||
443 |