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