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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
24 | class Router |
||
25 | { |
||
26 | use SingletonTrait; |
||
27 | |||
28 | protected $routing; |
||
29 | protected $slugs; |
||
30 | private $domains = []; |
||
31 | /** |
||
32 | * @var Finder $finder |
||
33 | */ |
||
34 | private $finder; |
||
35 | /** |
||
36 | * @var \PSFS\base\Cache $cache |
||
37 | */ |
||
38 | private $cache; |
||
39 | /** |
||
40 | * @var bool headersSent |
||
41 | */ |
||
42 | protected $headersSent = false; |
||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | protected $cacheType = Cache::JSON; |
||
47 | |||
48 | /** |
||
49 | * Constructor Router |
||
50 | * @throws ConfigException |
||
51 | */ |
||
52 | 1 | public function __construct() |
|
53 | { |
||
54 | 1 | $this->finder = new Finder(); |
|
55 | 1 | $this->cache = Cache::getInstance(); |
|
56 | 1 | $this->init(); |
|
57 | 1 | } |
|
58 | |||
59 | /** |
||
60 | * Initializer Router |
||
61 | * @throws ConfigException |
||
62 | */ |
||
63 | 1 | public function init() |
|
64 | { |
||
65 | 1 | list($this->routing, $this->slugs) = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json", $this->cacheType, TRUE); |
|
|
|||
66 | 1 | if (empty($this->routing) || Config::getInstance()->getDebugMode()) { |
|
67 | 1 | $this->debugLoad(); |
|
68 | } else { |
||
69 | $this->domains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", $this->cacheType, TRUE); |
||
70 | } |
||
71 | 1 | $this->checkExternalModules(false); |
|
72 | 1 | $this->setLoaded(true); |
|
73 | 1 | } |
|
74 | |||
75 | /** |
||
76 | * Load routes and domains and store them |
||
77 | */ |
||
78 | 1 | private function debugLoad() { |
|
84 | |||
85 | /** |
||
86 | * Método que deriva un error HTTP de página no encontrada |
||
87 | * |
||
88 | * @param \Exception $e |
||
89 | * @param boolean $isJson |
||
90 | * |
||
91 | * @return string HTML |
||
92 | */ |
||
93 | public function httpNotFound(\Exception $e = NULL, $isJson = false) |
||
94 | { |
||
95 | Logger::log('Throw not found exception'); |
||
96 | if (NULL === $e) { |
||
97 | Logger::log('Not found page throwed without previous exception', LOG_WARNING); |
||
98 | $e = new \Exception(_('Page not found'), 404); |
||
99 | } |
||
100 | $template = Template::getInstance()->setStatus($e->getCode()); |
||
101 | if (preg_match('/json/i', Request::getInstance()->getServer('CONTENT_TYPE')) || $isJson) { |
||
102 | $response = new JsonResponse(null, false, 0, 0, $e->getMessage()); |
||
103 | return $template->output(json_encode($response), 'application/json'); |
||
104 | } else { |
||
105 | $not_found_route = Config::getParam('route.404'); |
||
106 | if(null !== $not_found_route) { |
||
107 | Request::getInstance()->redirect($this->getRoute($not_found_route, true)); |
||
108 | } else { |
||
109 | return $template->render('error.html.twig', array( |
||
110 | 'exception' => $e, |
||
111 | 'trace' => $e->getTraceAsString(), |
||
112 | 'error_page' => TRUE, |
||
113 | )); |
||
114 | } |
||
115 | } |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Método que devuelve las rutas |
||
120 | * @return string|null |
||
121 | */ |
||
122 | 1 | public function getSlugs() |
|
126 | |||
127 | /** |
||
128 | * @return mixed |
||
129 | */ |
||
130 | 2 | public function getRoutes() { |
|
133 | |||
134 | /** |
||
135 | * Method that extract all routes in the platform |
||
136 | * @return array |
||
137 | */ |
||
138 | 1 | public function getAllRoutes() |
|
148 | |||
149 | /** |
||
150 | * Método que calcula el objeto a enrutar |
||
151 | * |
||
152 | * @param string|null $route |
||
153 | * |
||
154 | * @throws \Exception |
||
155 | * @return string HTML |
||
156 | */ |
||
157 | 1 | public function execute($route) |
|
175 | |||
176 | /** |
||
177 | * Método que busca el componente que ejecuta la ruta |
||
178 | * |
||
179 | * @param string $route |
||
180 | * |
||
181 | * @throws \PSFS\base\exception\RouterException |
||
182 | */ |
||
183 | 1 | protected function searchAction($route) |
|
213 | |||
214 | /** |
||
215 | * @param array $action |
||
216 | * @param array $params |
||
217 | * @return bool |
||
218 | */ |
||
219 | private function checkRequirements(array $action, array $params = []) { |
||
233 | |||
234 | /** |
||
235 | * Método que manda las cabeceras de autenticación |
||
236 | * @return string HTML |
||
237 | */ |
||
238 | protected function sentAuthHeader() |
||
242 | |||
243 | /** |
||
244 | * @return string|null |
||
245 | */ |
||
246 | 1 | private function getExternalModules() { |
|
251 | |||
252 | /** |
||
253 | * Method that check if the project has sub project to include |
||
254 | * @param boolean $hydrateRoute |
||
255 | */ |
||
256 | 1 | private function checkExternalModules($hydrateRoute = true) |
|
266 | |||
267 | /** |
||
268 | * Method that gather all the routes in the project |
||
269 | */ |
||
270 | 1 | private function generateRouting() |
|
285 | |||
286 | /** |
||
287 | * Método que regenera el fichero de rutas |
||
288 | * @throws ConfigException |
||
289 | */ |
||
290 | 1 | public function hydrateRouting() |
|
307 | |||
308 | /** |
||
309 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
310 | * |
||
311 | * @param string $origen |
||
312 | * @param string $namespace |
||
313 | * @param array $routing |
||
314 | * |
||
315 | * @return array |
||
316 | * @throws ConfigException |
||
317 | */ |
||
318 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
329 | |||
330 | /** |
||
331 | * Checks that a namespace exists |
||
332 | * @param string $namespace |
||
333 | * @return bool |
||
334 | */ |
||
335 | 2 | public static function exists($namespace) |
|
339 | |||
340 | /** |
||
341 | * Método que añade nuevas rutas al array de referencia |
||
342 | * |
||
343 | * @param string $namespace |
||
344 | * @param array $routing |
||
345 | * @param string $module |
||
346 | * |
||
347 | * @return array |
||
348 | * @throws ConfigException |
||
349 | */ |
||
350 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
380 | |||
381 | /** |
||
382 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
383 | * |
||
384 | * @param \ReflectionClass $class |
||
385 | * |
||
386 | * @return Router |
||
387 | * @throws ConfigException |
||
388 | */ |
||
389 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
404 | |||
405 | /** |
||
406 | * Método que genera las urls amigables para usar dentro del framework |
||
407 | * @return Router |
||
408 | */ |
||
409 | 1 | public function simpatize() |
|
419 | |||
420 | /** |
||
421 | * Método que devuelve una ruta del framework |
||
422 | * |
||
423 | * @param string $slug |
||
424 | * @param boolean $absolute |
||
425 | * @param array $params |
||
426 | * |
||
427 | * @return string|null |
||
428 | * @throws RouterException |
||
429 | */ |
||
430 | 3 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
|
447 | |||
448 | /** |
||
449 | * Método que devuelve las rutas de administración |
||
450 | * @deprecated |
||
451 | * @return array |
||
452 | */ |
||
453 | public function getAdminRoutes() |
||
457 | |||
458 | /** |
||
459 | * Método que devuelve le controlador del admin |
||
460 | * @deprecated |
||
461 | * @return Admin |
||
462 | */ |
||
463 | public function getAdmin() |
||
467 | |||
468 | /** |
||
469 | * Método que extrae los dominios |
||
470 | * @return array |
||
471 | */ |
||
472 | 1 | public function getDomains() |
|
476 | |||
477 | /** |
||
478 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
479 | * |
||
480 | * @param string $route |
||
481 | * @param array|null $action |
||
482 | * @param types\Controller $class |
||
483 | * @param array $params |
||
484 | */ |
||
485 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
510 | |||
511 | /** |
||
512 | * Parse slugs to create translations |
||
513 | * |
||
514 | * @param string $absoluteTranslationFileName |
||
515 | */ |
||
516 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
531 | |||
532 | /** |
||
533 | * @param bool $hydrateRoute |
||
534 | * @param $modulePath |
||
535 | * @param $externalModulePath |
||
536 | */ |
||
537 | private function loadExternalAutoloader($hydrateRoute, SplFileInfo $modulePath, $externalModulePath) |
||
546 | |||
547 | /** |
||
548 | * @param $hydrateRoute |
||
549 | * @param $module |
||
550 | * @return mixed |
||
551 | */ |
||
552 | 1 | private function loadExternalModule($hydrateRoute, $module) |
|
567 | |||
568 | } |
||
569 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.