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 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $routing = []; |
||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $slugs = []; |
||
| 37 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private $domains = []; |
||
| 41 | /** |
||
| 42 | * @var Finder $finder |
||
| 43 | */ |
||
| 44 | private $finder; |
||
| 45 | /** |
||
| 46 | * @var \PSFS\base\Cache $cache |
||
| 47 | */ |
||
| 48 | private $cache; |
||
| 49 | /** |
||
| 50 | * @var bool headersSent |
||
| 51 | */ |
||
| 52 | protected $headersSent = false; |
||
| 53 | /** |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | protected $cacheType = Cache::JSON; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Router constructor. |
||
| 60 | * @throws exception\GeneratorException |
||
| 61 | * @throws ConfigException |
||
| 62 | * @throws \InvalidArgumentException |
||
| 63 | */ |
||
| 64 | 1 | public function __construct() |
|
| 65 | { |
||
| 66 | 1 | $this->finder = new Finder(); |
|
| 67 | 1 | $this->cache = Cache::getInstance(); |
|
| 68 | 1 | $this->init(); |
|
| 69 | 1 | } |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @throws exception\GeneratorException |
||
| 73 | * @throws ConfigException |
||
| 74 | * @throws \InvalidArgumentException |
||
| 75 | */ |
||
| 76 | 1 | public function init() |
|
| 77 | { |
||
| 78 | 1 | list($this->routing, $this->slugs) = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'urls.json', $this->cacheType, TRUE); |
|
|
|
|||
| 79 | 1 | if (empty($this->routing) || Config::getInstance()->getDebugMode()) { |
|
| 80 | 1 | $this->debugLoad(); |
|
| 81 | } else { |
||
| 82 | $this->domains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'domains.json', $this->cacheType, TRUE); |
||
| 83 | } |
||
| 84 | 1 | $this->checkExternalModules(false); |
|
| 85 | 1 | $this->setLoaded(); |
|
| 86 | 1 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @throws exception\GeneratorException |
||
| 90 | * @throws ConfigException |
||
| 91 | * @throws \InvalidArgumentException |
||
| 92 | */ |
||
| 93 | 1 | private function debugLoad() { |
|
| 94 | 1 | Logger::log('Begin routes load'); |
|
| 95 | 1 | $this->hydrateRouting(); |
|
| 96 | 1 | $this->simpatize(); |
|
| 97 | 1 | Logger::log('End routes load'); |
|
| 98 | 1 | } |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param \Exception|NULL $e |
||
| 102 | * @param bool $isJson |
||
| 103 | * @return string |
||
| 104 | * @throws RouterException |
||
| 105 | */ |
||
| 106 | public function httpNotFound(\Exception $e = NULL, $isJson = false) |
||
| 107 | { |
||
| 108 | Logger::log('Throw not found exception'); |
||
| 109 | if (NULL === $e) { |
||
| 110 | Logger::log('Not found page thrown without previous exception', LOG_WARNING); |
||
| 111 | $e = new \Exception(_('Page not found'), 404); |
||
| 112 | } |
||
| 113 | $template = Template::getInstance()->setStatus($e->getCode()); |
||
| 114 | if ($isJson || false !== stripos(Request::getInstance()->getServer('CONTENT_TYPE'), 'json')) { |
||
| 115 | $response = new JsonResponse(null, false, 0, 0, $e->getMessage()); |
||
| 116 | return $template->output(json_encode($response), 'application/json'); |
||
| 117 | } |
||
| 118 | |||
| 119 | $not_found_route = Config::getParam('route.404'); |
||
| 120 | if(null !== $not_found_route) { |
||
| 121 | Request::getInstance()->redirect($this->getRoute($not_found_route, true)); |
||
| 122 | } else { |
||
| 123 | return $template->render('error.html.twig', array( |
||
| 124 | 'exception' => $e, |
||
| 125 | 'trace' => $e->getTraceAsString(), |
||
| 126 | 'error_page' => TRUE, |
||
| 127 | )); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | 1 | public function getSlugs() |
|
| 135 | { |
||
| 136 | 1 | return $this->slugs; |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | 2 | public function getRoutes() { |
|
| 143 | 2 | return $this->routing; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Method that extract all routes in the platform |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | 1 | public function getAllRoutes() |
|
| 151 | { |
||
| 152 | 1 | $routes = []; |
|
| 153 | 1 | foreach ($this->getRoutes() as $path => $route) { |
|
| 154 | 1 | if (array_key_exists('slug', $route)) { |
|
| 155 | 1 | $routes[$route['slug']] = $path; |
|
| 156 | } |
||
| 157 | } |
||
| 158 | 1 | return $routes; |
|
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string|null $route |
||
| 163 | * |
||
| 164 | * @throws \Exception |
||
| 165 | * @return string HTML |
||
| 166 | */ |
||
| 167 | 1 | public function execute($route) |
|
| 168 | { |
||
| 169 | 1 | Logger::log('Executing the request'); |
|
| 170 | try { |
||
| 171 | //Search action and execute |
||
| 172 | 1 | $this->searchAction($route); |
|
| 173 | 1 | } catch (AccessDeniedException $e) { |
|
| 174 | Logger::log(_('Solicitamos credenciales de acceso a zona restringida'), LOG_WARNING, ['file' => $e->getFile() . '[' . $e->getLine() . ']']); |
||
| 175 | return Admin::staticAdminLogon($route); |
||
| 176 | 1 | } catch (RouterException $r) { |
|
| 177 | 1 | Logger::log($r->getMessage(), LOG_WARNING); |
|
| 178 | } catch (\Exception $e) { |
||
| 179 | Logger::log($e->getMessage(), LOG_ERR); |
||
| 180 | throw $e; |
||
| 181 | } |
||
| 182 | |||
| 183 | 1 | throw new RouterException(_('Página no encontrada'), 404); |
|
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param $route |
||
| 188 | * @throws AccessDeniedException |
||
| 189 | * @throws AdminCredentialsException |
||
| 190 | * @throws RouterException |
||
| 191 | * @throws \Exception |
||
| 192 | */ |
||
| 193 | 1 | protected function searchAction($route) |
|
| 194 | { |
||
| 195 | 1 | Logger::log('Searching action to execute: ' . $route, LOG_INFO); |
|
| 196 | //Revisamos si tenemos la ruta registrada |
||
| 197 | 1 | $parts = parse_url($route); |
|
| 198 | 1 | $path = array_key_exists('path', $parts) ? $parts['path'] : $route; |
|
| 199 | 1 | $httpRequest = Request::getInstance()->getMethod(); |
|
| 200 | 1 | foreach ($this->routing as $pattern => $action) { |
|
| 201 | 1 | list($httpMethod, $routePattern) = RouterHelper::extractHttpRoute($pattern); |
|
| 202 | 1 | $matched = RouterHelper::matchRoutePattern($routePattern, $path); |
|
| 203 | 1 | if ($matched && ($httpMethod === 'ALL' || $httpRequest === $httpMethod) && RouterHelper::compareSlashes($routePattern, $path)) { |
|
| 204 | // Checks restricted access |
||
| 205 | SecurityHelper::checkRestrictedAccess($route); |
||
| 206 | $get = RouterHelper::extractComponents($route, $routePattern); |
||
| 207 | /** @var $class \PSFS\base\types\Controller */ |
||
| 208 | $class = RouterHelper::getClassToCall($action); |
||
| 209 | try { |
||
| 210 | if($this->checkRequirements($action, $get)) { |
||
| 211 | $this->executeCachedRoute($route, $action, $class, $get); |
||
| 212 | } else { |
||
| 213 | throw new RouterException(_('La ruta no es válida'), 400); |
||
| 214 | } |
||
| 215 | } catch (\Exception $e) { |
||
| 216 | Logger::log($e->getMessage(), LOG_ERR); |
||
| 217 | 1 | throw $e; |
|
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | 1 | throw new RouterException(_('Ruta no encontrada')); |
|
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param array $action |
||
| 226 | * @param array $params |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | private function checkRequirements(array $action, $params = []) { |
||
| 230 | if(!empty($params) && !empty($action['requirements'])) { |
||
| 231 | $checked = 0; |
||
| 232 | foreach(array_keys($params) as $key) { |
||
| 233 | if(in_array($key, $action['requirements'], true)) { |
||
| 234 | $checked++; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | $valid = count($action['requirements']) === $checked; |
||
| 238 | } else { |
||
| 239 | $valid = true; |
||
| 240 | } |
||
| 241 | return $valid; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return string HTML |
||
| 246 | */ |
||
| 247 | protected function sentAuthHeader() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return string|null |
||
| 254 | */ |
||
| 255 | 1 | private function getExternalModules() { |
|
| 256 | 1 | $externalModules = Config::getParam('modules.extend', ''); |
|
| 257 | 1 | $externalModules .= ',psfs/auth'; |
|
| 258 | 1 | return $externalModules; |
|
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param boolean $hydrateRoute |
||
| 263 | */ |
||
| 264 | 1 | private function checkExternalModules($hydrateRoute = true) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @throws exception\GeneratorException |
||
| 277 | * @throws ConfigException |
||
| 278 | * @throws \InvalidArgumentException |
||
| 279 | */ |
||
| 280 | 1 | private function generateRouting() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @throws exception\GeneratorException |
||
| 300 | * @throws ConfigException |
||
| 301 | * @throws \InvalidArgumentException |
||
| 302 | */ |
||
| 303 | 1 | public function hydrateRouting() |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @param string $origen |
||
| 323 | * @param string $namespace |
||
| 324 | * @param array $routing |
||
| 325 | * @return array |
||
| 326 | * @throws ConfigException |
||
| 327 | * @throws \InvalidArgumentException |
||
| 328 | */ |
||
| 329 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * @param string $namespace |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | 3 | public static function exists($namespace) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * |
||
| 358 | * @param string $namespace |
||
| 359 | * @param array $routing |
||
| 360 | * @param string $module |
||
| 361 | * |
||
| 362 | * @return array |
||
| 363 | * @throws ConfigException |
||
| 364 | */ |
||
| 365 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
| 395 | |||
| 396 | /** |
||
| 397 | * |
||
| 398 | * @param \ReflectionClass $class |
||
| 399 | * |
||
| 400 | * @return Router |
||
| 401 | * @throws ConfigException |
||
| 402 | */ |
||
| 403 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @return $this |
||
| 421 | * @throws exception\GeneratorException |
||
| 422 | * @throws ConfigException |
||
| 423 | */ |
||
| 424 | 1 | public function simpatize() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @param string $slug |
||
| 437 | * @param boolean $absolute |
||
| 438 | * @param array $params |
||
| 439 | * |
||
| 440 | * @return string|null |
||
| 441 | * @throws RouterException |
||
| 442 | */ |
||
| 443 | 3 | public function getRoute($slug = '', $absolute = FALSE, array $params = []) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * @deprecated |
||
| 465 | * @return array |
||
| 466 | */ |
||
| 467 | public function getAdminRoutes() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @deprecated |
||
| 474 | * @return Admin |
||
| 475 | */ |
||
| 476 | public function getAdmin() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @return array |
||
| 483 | */ |
||
| 484 | 1 | public function getDomains() |
|
| 488 | |||
| 489 | /** |
||
| 490 | * @param string $route |
||
| 491 | * @param array $action |
||
| 492 | * @param string $class |
||
| 493 | * @param array $params |
||
| 494 | * @throws exception\GeneratorException |
||
| 495 | * @throws ConfigException |
||
| 496 | */ |
||
| 497 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Parse slugs to create translations |
||
| 525 | * |
||
| 526 | * @param string $absoluteTranslationFileName |
||
| 527 | */ |
||
| 528 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
| 543 | |||
| 544 | /** |
||
| 545 | * @param bool $hydrateRoute |
||
| 546 | * @param $modulePath |
||
| 547 | * @param $externalModulePath |
||
| 548 | */ |
||
| 549 | private function loadExternalAutoloader($hydrateRoute, SplFileInfo $modulePath, $externalModulePath) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @param $hydrateRoute |
||
| 563 | * @param $module |
||
| 564 | * @return mixed |
||
| 565 | */ |
||
| 566 | 1 | private function loadExternalModule($hydrateRoute, $module) |
|
| 585 | |||
| 586 | } |
||
| 587 |
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.