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 |
||
| 25 | class Router |
||
| 26 | { |
||
| 27 | use SingletonTrait; |
||
| 28 | |||
| 29 | protected $routing; |
||
| 30 | protected $slugs; |
||
| 31 | private $domains = []; |
||
| 32 | /** |
||
| 33 | * @var Finder $finder |
||
| 34 | */ |
||
| 35 | private $finder; |
||
| 36 | /** |
||
| 37 | * @var \PSFS\base\Cache $cache |
||
| 38 | */ |
||
| 39 | private $cache; |
||
| 40 | /** |
||
| 41 | * @var bool headersSent |
||
| 42 | */ |
||
| 43 | protected $headersSent = false; |
||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | protected $cacheType = Cache::JSON; |
||
| 48 | /** |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | protected $loaded = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Constructor Router |
||
| 55 | * @throws ConfigException |
||
| 56 | */ |
||
| 57 | 1 | public function __construct() |
|
| 58 | { |
||
| 59 | 1 | $this->finder = new Finder(); |
|
| 60 | 1 | $this->cache = Cache::getInstance(); |
|
| 61 | 1 | $this->init(); |
|
| 62 | 1 | } |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Inicializador Router |
||
| 66 | * @throws ConfigException |
||
| 67 | */ |
||
| 68 | 1 | public function init() |
|
| 69 | { |
||
| 70 | 1 | list($this->routing, $this->slugs) = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json", $this->cacheType, TRUE); |
|
|
|
|||
| 71 | 1 | if (empty($this->routing) || Config::getInstance()->getDebugMode()) { |
|
| 72 | 1 | $this->debugLoad(); |
|
| 73 | } else { |
||
| 74 | $this->domains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", $this->cacheType, TRUE); |
||
| 75 | } |
||
| 76 | 1 | $this->checkExternalModules(false); |
|
| 77 | 1 | $this->setLoaded(true); |
|
| 78 | 1 | } |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Load routes and domains and store them |
||
| 82 | */ |
||
| 83 | 1 | private function debugLoad() { |
|
| 84 | 1 | Logger::log('Begin routes load', LOG_DEBUG); |
|
| 85 | 1 | $this->hydrateRouting(); |
|
| 86 | 1 | $this->simpatize(); |
|
| 87 | 1 | Logger::log('End routes load', LOG_DEBUG); |
|
| 88 | 1 | } |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Método que deriva un error HTTP de página no encontrada |
||
| 92 | * |
||
| 93 | * @param \Exception $e |
||
| 94 | * @param boolean $isJson |
||
| 95 | * |
||
| 96 | * @return string HTML |
||
| 97 | */ |
||
| 98 | public function httpNotFound(\Exception $e = NULL, $isJson = false) |
||
| 99 | { |
||
| 100 | Logger::log('Throw not found exception'); |
||
| 101 | if (NULL === $e) { |
||
| 102 | Logger::log('Not found page throwed without previous exception', LOG_WARNING); |
||
| 103 | $e = new \Exception(_('Page not found'), 404); |
||
| 104 | } |
||
| 105 | $template = Template::getInstance()->setStatus($e->getCode()); |
||
| 106 | if (preg_match('/json/i', Request::getInstance()->getServer('CONTENT_TYPE')) || $isJson) { |
||
| 107 | $response = new JsonResponse(null, false, 0, 0, $e->getMessage()); |
||
| 108 | return $template->output(json_encode($response), 'application/json'); |
||
| 109 | } else { |
||
| 110 | $not_found_rouote = Config::getParam('route.404'); |
||
| 111 | if(null !== $not_found_rouote) { |
||
| 112 | Request::getInstance()->redirect($this->getRoute($not_found_rouote, true)); |
||
| 113 | } else { |
||
| 114 | return $template->render('error.html.twig', array( |
||
| 115 | 'exception' => $e, |
||
| 116 | 'trace' => $e->getTraceAsString(), |
||
| 117 | 'error_page' => TRUE, |
||
| 118 | )); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Método que devuelve las rutas |
||
| 125 | * @return string|null |
||
| 126 | */ |
||
| 127 | 1 | public function getSlugs() |
|
| 128 | { |
||
| 129 | 1 | return $this->slugs; |
|
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return mixed |
||
| 134 | */ |
||
| 135 | 2 | public function getRoutes() { |
|
| 136 | 2 | return $this->routing; |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Method that extract all routes in the platform |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | 1 | public function getAllRoutes() |
|
| 144 | { |
||
| 145 | 1 | $routes = []; |
|
| 146 | 1 | foreach ($this->getRoutes() as $path => $route) { |
|
| 147 | 1 | if (array_key_exists('slug', $route)) { |
|
| 148 | 1 | $routes[$route['slug']] = $path; |
|
| 149 | } |
||
| 150 | } |
||
| 151 | 1 | return $routes; |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Método que calcula el objeto a enrutar |
||
| 156 | * |
||
| 157 | * @param string|null $route |
||
| 158 | * |
||
| 159 | * @throws \Exception |
||
| 160 | * @return string HTML |
||
| 161 | */ |
||
| 162 | 1 | public function execute($route) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Método que busca el componente que ejecuta la ruta |
||
| 183 | * |
||
| 184 | * @param string $route |
||
| 185 | * |
||
| 186 | * @throws \PSFS\base\exception\RouterException |
||
| 187 | */ |
||
| 188 | 1 | protected function searchAction($route) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param array $action |
||
| 221 | * @param array $params |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | private function checkRequirements(array $action, array $params = []) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Método que manda las cabeceras de autenticación |
||
| 244 | * @return string HTML |
||
| 245 | */ |
||
| 246 | protected function sentAuthHeader() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return string|null |
||
| 253 | */ |
||
| 254 | 1 | private function getExternalModules() { |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Method that check if the proyect has sub project to include |
||
| 264 | * @param boolean $hydrateRoute |
||
| 265 | */ |
||
| 266 | 1 | private function checkExternalModules($hydrateRoute = true) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Method that gather all the routes in the project |
||
| 279 | */ |
||
| 280 | 1 | private function generateRouting() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Método que regenera el fichero de rutas |
||
| 298 | * @throws ConfigException |
||
| 299 | */ |
||
| 300 | 1 | public function hydrateRouting() |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
| 320 | * |
||
| 321 | * @param string $origen |
||
| 322 | * @param string $namespace |
||
| 323 | * @param array $routing |
||
| 324 | * |
||
| 325 | * @return array |
||
| 326 | * @throws ConfigException |
||
| 327 | */ |
||
| 328 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Checks that a namespace exists |
||
| 342 | * @param string $namespace |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | 1 | public static function exists($namespace) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Método que añade nuevas rutas al array de referencia |
||
| 352 | * |
||
| 353 | * @param string $namespace |
||
| 354 | * @param array $routing |
||
| 355 | * @param string $module |
||
| 356 | * |
||
| 357 | * @return array |
||
| 358 | * @throws ConfigException |
||
| 359 | */ |
||
| 360 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
| 390 | * |
||
| 391 | * @param \ReflectionClass $class |
||
| 392 | * |
||
| 393 | * @return Router |
||
| 394 | * @throws ConfigException |
||
| 395 | */ |
||
| 396 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Método que genera las urls amigables para usar dentro del framework |
||
| 414 | * @return Router |
||
| 415 | */ |
||
| 416 | 1 | public function simpatize() |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Método que devuelve una ruta del framework |
||
| 429 | * |
||
| 430 | * @param string $slug |
||
| 431 | * @param boolean $absolute |
||
| 432 | * @param array $params |
||
| 433 | * |
||
| 434 | * @return string|null |
||
| 435 | * @throws RouterException |
||
| 436 | */ |
||
| 437 | 1 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Método que devuelve las rutas de administración |
||
| 457 | * @deprecated |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | public function getAdminRoutes() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Método que devuelve le controlador del admin |
||
| 467 | * @deprecated |
||
| 468 | * @return Admin |
||
| 469 | */ |
||
| 470 | public function getAdmin() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Método que extrae los dominios |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | 1 | public function getDomains() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
| 486 | * |
||
| 487 | * @param string $route |
||
| 488 | * @param array|null $action |
||
| 489 | * @param types\Controller $class |
||
| 490 | * @param array $params |
||
| 491 | */ |
||
| 492 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Parse slugs to create translations |
||
| 520 | * |
||
| 521 | * @param string $absoluteTranslationFileName |
||
| 522 | */ |
||
| 523 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
| 541 | |||
| 542 | /** |
||
| 543 | * @param $hydrateRoute |
||
| 544 | * @param $modulePath |
||
| 545 | * @param $externalModulePath |
||
| 546 | */ |
||
| 547 | private function loadExternalAutoloader(bool $hydrateRoute, \DirectoryIterator $modulePath, $externalModulePath) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @param $hydrateRoute |
||
| 561 | * @param $module |
||
| 562 | * @return mixed |
||
| 563 | */ |
||
| 564 | private function loadExternalModule(bool $hydrateRoute, $module) |
||
| 578 | |||
| 579 | } |
||
| 580 |
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.