Total Complexity | 54 |
Total Lines | 389 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | 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 |
||
40 | class Router implements SingletonInterface |
||
41 | { |
||
42 | const CACHE_FILE_NAME = 'router.cache.php'; |
||
43 | |||
44 | /** @var RouteCollector FastRoute, null if usingCache is set */ |
||
45 | private $routeCollector = null; |
||
46 | |||
47 | /** @var array List of middlewares called using the middleware() method. */ |
||
48 | private $currentMiddlewares = []; |
||
49 | |||
50 | /** @var string List of group prefixes called using the group() method. */ |
||
51 | private $currentGroupPrefix; |
||
52 | |||
53 | /** @var Context The current context */ |
||
54 | private $context; |
||
55 | |||
56 | /** @var string The path to the router cache file */ |
||
57 | private $cacheFilePath; |
||
58 | |||
59 | /** @var bool Wheter the cache currently exists */ |
||
60 | private $cacheExists = false; |
||
61 | |||
62 | /** |
||
63 | * Router constructor. |
||
64 | * |
||
65 | * @param Context $context |
||
66 | */ |
||
67 | public function __construct(Context $context) |
||
68 | { |
||
69 | $this->routeCollector = new RouteCollector( |
||
70 | new RouteParser\Std(), |
||
71 | new DataGenerator\GroupCountBased() |
||
72 | ); |
||
73 | |||
74 | $this->context = $context; |
||
75 | |||
76 | $root_dir = $this->context->getConfig()->get('app/root_dir'); |
||
77 | $cache_dir = $this->context->getConfig()->get('app/cache_dir') ?? 'var' . DIRECTORY_SEPARATOR . 'cache'; |
||
78 | |||
79 | if (!$root_dir || strlen($root_dir) <= 0) { |
||
80 | throw new ConfigurationNotFoundException('app/root_dir'); |
||
81 | } |
||
82 | |||
83 | $this->cacheFilePath = $root_dir . DIRECTORY_SEPARATOR . $cache_dir . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME; |
||
84 | |||
85 | if ($this->context->getConfig()->get('app/production') == true && file_exists($this->cacheFilePath)) { |
||
86 | $this->cacheExists = true; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Load every PHP Routes files under the directory |
||
92 | * |
||
93 | * @param string $path |
||
94 | */ |
||
95 | public function loadPath(string $path): void |
||
96 | { |
||
97 | if ($this->cacheExists) { |
||
98 | return; |
||
99 | } |
||
100 | |||
101 | if (file_exists($path . \DIRECTORY_SEPARATOR . 'Routes')) { |
||
102 | |||
103 | foreach (PathUtils::getRecursivePHPFilesIterator($path . \DIRECTORY_SEPARATOR . 'Routes') as $file) { |
||
104 | |||
105 | $callback = require $file->getPathName(); |
||
106 | |||
107 | if (is_callable($callback)) { |
||
108 | $callback($this); |
||
109 | } |
||
110 | |||
111 | } |
||
112 | |||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Encapsulate all the routes that are added from $func(Router) with this middleware. |
||
118 | * |
||
119 | * If the return value of the middleware is false, throws a RouteMiddlewareFailedException. |
||
120 | * |
||
121 | * @param string[]|string $middlewareClass The middleware to use |
||
122 | * @param callable $func |
||
123 | */ |
||
124 | public function middleware($middlewareClass, callable $func): void |
||
125 | { |
||
126 | array_push($this->currentMiddlewares, $middlewareClass); |
||
127 | |||
128 | $func($this); |
||
129 | |||
130 | array_pop($this->currentMiddlewares); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Adds a prefix in front of all the encapsulated routes. |
||
135 | * |
||
136 | * @param string $prefix The prefix of the group. |
||
137 | * @param callable $func |
||
138 | * @param array|string $middlewareClass string[] or string representing middleware(s) class(es) |
||
139 | */ |
||
140 | public function group(string $prefix, callable $func, $middlewareClass = null): void |
||
141 | { |
||
142 | $previousGroupPrefix = $this->currentGroupPrefix; |
||
143 | $this->currentGroupPrefix = $previousGroupPrefix . $prefix; |
||
144 | |||
145 | if (is_array($middlewareClass) || is_string($middlewareClass)) { |
||
146 | $this->middleware($middlewareClass, $func); |
||
147 | } else { |
||
148 | $func($this); |
||
149 | } |
||
150 | |||
151 | $this->currentGroupPrefix = $previousGroupPrefix; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Dispatch the request to the router. |
||
156 | * |
||
157 | * @return Route |
||
158 | * @throws MethodNotAllowedException if the request method is not supported, but others are for this route. |
||
159 | * @throws RouteNotFoundException if the requested route did not match any routes. |
||
160 | */ |
||
161 | public function dispatch(): Route |
||
162 | { |
||
163 | $dispatcher = $this->getDispatcher(); |
||
164 | |||
165 | if (defined('STDIN')) { |
||
166 | $requestMethod = 'CLI'; |
||
167 | } else { |
||
168 | $requestMethod = $_SERVER['REQUEST_METHOD']; |
||
169 | } |
||
170 | |||
171 | $routeInfo = $dispatcher->dispatch($requestMethod, $this->detectUri()); |
||
172 | |||
173 | if ($routeInfo[0] === Dispatcher::FOUND) { |
||
174 | |||
175 | $handler = $routeInfo[1]; |
||
176 | $arguments = $routeInfo[2]; |
||
177 | $middlewares = $handler['middlewares']; |
||
178 | |||
179 | $route = new Route($handler['controller_class'], $handler['parameters'], $arguments); |
||
180 | |||
181 | if (is_array($middlewares)) { |
||
182 | $this->handleMiddlewares($middlewares, $route); |
||
183 | } |
||
184 | |||
185 | return $route; |
||
186 | |||
187 | } elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) { |
||
188 | throw new MethodNotAllowedException; |
||
189 | } else { |
||
190 | throw new RouteNotFoundException; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Add a new route. |
||
196 | * |
||
197 | * @param string|string[] $httpMethod The HTTP method, example: GET, HEAD, POST, PATCH, PUT, DELETE, CLI, etc. |
||
198 | * Can be an array of values. |
||
199 | * @param string $route The route |
||
200 | * @param string $controllerClass The Controller's class |
||
201 | * @param array $parameters The parameters to pass |
||
202 | */ |
||
203 | public function addRoute($httpMethod, string $route, string $controllerClass, array $parameters = []): void |
||
204 | { |
||
205 | if ($this->cacheExists) { |
||
206 | return; |
||
207 | } |
||
208 | |||
209 | $route = $this->currentGroupPrefix . $route; |
||
210 | |||
211 | $this->routeCollector->addRoute($httpMethod, $route, [ |
||
212 | 'controller_class' => $controllerClass, |
||
213 | 'middlewares' => $this->currentMiddlewares, |
||
214 | 'parameters' => $parameters |
||
215 | ]); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Add a new route with GET as HTTP method. |
||
220 | * |
||
221 | * @param string $route The route |
||
222 | * @param string $controllerClass The Controller's class |
||
223 | * @param array $parameters The parameters to pass |
||
224 | */ |
||
225 | public function get(string $route, string $controllerClass, array $parameters = []): void |
||
226 | { |
||
227 | $this->addRoute('GET', $route, $controllerClass, $parameters); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Add a new route with HEAD as HTTP method. |
||
232 | * |
||
233 | * @param string $route The route |
||
234 | * @param string $controllerClass The Controller's class |
||
235 | * @param array $parameters The parameters to pass |
||
236 | */ |
||
237 | public function head(string $route, string $controllerClass, array $parameters = []): void |
||
238 | { |
||
239 | $this->addRoute('HEAD', $route, $controllerClass, $parameters); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Add a new route with POST as HTTP method. |
||
244 | * |
||
245 | * @param string $route The route |
||
246 | * @param string $controllerClass The Controller's class |
||
247 | * @param array $parameters The parameters to pass |
||
248 | */ |
||
249 | public function post(string $route, string $controllerClass, array $parameters = []): void |
||
250 | { |
||
251 | $this->addRoute('POST', $route, $controllerClass, $parameters); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Add a new route with PUT as HTTP method. |
||
256 | * |
||
257 | * @param string $route The route |
||
258 | * @param string $controllerClass The Controller's class |
||
259 | * @param array $parameters The parameters to pass |
||
260 | */ |
||
261 | public function put(string $route, string $controllerClass, array $parameters = []): void |
||
262 | { |
||
263 | $this->addRoute('PUT', $route, $controllerClass, $parameters); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Add a new route with PATCH as HTTP method. |
||
268 | * |
||
269 | * @param string $route The route |
||
270 | * @param string $controllerClass The Controller's class |
||
271 | * @param array $parameters The parameters to pass |
||
272 | */ |
||
273 | public function patch(string $route, string $controllerClass, array $parameters = []): void |
||
274 | { |
||
275 | $this->addRoute('PATCH', $route, $controllerClass, $parameters); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Add a new route with DELETE as HTTP method. |
||
280 | * |
||
281 | * @param string $route The route |
||
282 | * @param string $controllerClass The Controller's class |
||
283 | * @param array $parameters The parameters to pass |
||
284 | */ |
||
285 | public function delete(string $route, string $controllerClass, array $parameters = []): void |
||
286 | { |
||
287 | $this->addRoute('DELETE', $route, $controllerClass, $parameters); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Add a new route with CLI as method. |
||
292 | * |
||
293 | * @param string $route The route |
||
294 | * @param string $controllerClass The Controller's class |
||
295 | * @param array $parameters The parameters to pass |
||
296 | */ |
||
297 | public function cli(string $route, string $controllerClass, array $parameters = []): void |
||
298 | { |
||
299 | $this->addRoute('CLI', $route, $controllerClass, $parameters); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @param array $dispatchData |
||
304 | * |
||
305 | * @return bool |
||
306 | */ |
||
307 | private function generateCache(array $dispatchData) : bool |
||
308 | { |
||
309 | $dir = dirname($this->cacheFilePath); |
||
310 | |||
311 | if (!file_exists($this->cacheFilePath) && is_dir($dir) && is_writable($dir)) { |
||
312 | |||
313 | return file_put_contents( |
||
314 | $this->cacheFilePath, |
||
315 | '<?php return ' . var_export($dispatchData, true) . ';' . PHP_EOL, |
||
316 | LOCK_EX |
||
317 | ) !== false; |
||
318 | |||
319 | } else { |
||
320 | return false; |
||
321 | } |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @return Dispatcher\GroupCountBased |
||
326 | */ |
||
327 | private function getDispatcher(): Dispatcher\GroupCountBased |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Returns a parsable URI |
||
352 | * |
||
353 | * @return string |
||
354 | */ |
||
355 | private function detectUri(): string |
||
356 | { |
||
357 | if (php_sapi_name() == 'cli') { |
||
358 | $args = array_slice($_SERVER['argv'], 1); |
||
359 | return count($args) > 0 ? '/' . implode('/', $args) : '/'; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Return the base URI for a request |
||
379 | * |
||
380 | * @return string |
||
381 | */ |
||
382 | private function getBaseUri(): string |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Throws an exception or return void. |
||
401 | * |
||
402 | * @param array $middlewares |
||
403 | * @param Route $route |
||
404 | * |
||
405 | * @return void |
||
406 | * @throws RouteMiddlewareFailedException if a route middleware returned false. |
||
407 | * @throws InvalidMiddlewareException if a middleware is invalid. |
||
408 | */ |
||
409 | private function handleMiddlewares(array $middlewares, Route $route): void |
||
429 | } |
||
430 | |||
431 | } |
||
432 | } |
||
434 |