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 | |||
| 28 | use SingletonTrait; |
||
| 29 | |||
| 30 | protected $routing; |
||
| 31 | protected $slugs; |
||
| 32 | private $domains; |
||
| 33 | /** |
||
| 34 | * @var Finder $finder |
||
| 35 | */ |
||
| 36 | private $finder; |
||
| 37 | /** |
||
| 38 | * @var \PSFS\base\Cache $cache |
||
| 39 | */ |
||
| 40 | private $cache; |
||
| 41 | /** |
||
| 42 | * @var bool headersSent |
||
| 43 | */ |
||
| 44 | protected $headersSent = false; |
||
| 45 | /** |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | protected $cacheType = Cache::JSON; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Constructor Router |
||
| 52 | * @throws ConfigException |
||
| 53 | */ |
||
| 54 | 1 | public function __construct() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Inicializador Router |
||
| 63 | * @throws ConfigException |
||
| 64 | */ |
||
| 65 | 1 | public function init() |
|
| 66 | { |
||
| 67 | 1 | if(Cache::canUseMemcache()) { |
|
| 68 | $this->cacheType = Cache::MEMCACHE; |
||
| 69 | } |
||
| 70 | 1 | list($this->routing, $this->slugs) = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json", $this->cacheType, TRUE); |
|
|
|
|||
| 71 | 1 | $this->domains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", $this->cacheType, TRUE); |
|
| 72 | 1 | if (empty($this->routing) || Config::getInstance()->getDebugMode()) { |
|
| 73 | 1 | $this->debugLoad(); |
|
| 74 | 1 | } |
|
| 75 | 1 | $this->checkExternalModules(false); |
|
| 76 | 1 | } |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Load routes and domains and store them |
||
| 80 | */ |
||
| 81 | 1 | private function debugLoad() { |
|
| 82 | 1 | Logger::log('Begin routes load', LOG_DEBUG); |
|
| 83 | 1 | $this->hydrateRouting(); |
|
| 84 | 1 | $this->simpatize(); |
|
| 85 | 1 | Logger::log('End routes load', LOG_DEBUG); |
|
| 86 | 1 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Método que deriva un error HTTP de página no encontrada |
||
| 90 | * |
||
| 91 | * @param \Exception $e |
||
| 92 | * |
||
| 93 | * @return string HTML |
||
| 94 | */ |
||
| 95 | 1 | public function httpNotFound(\Exception $e = NULL) |
|
| 96 | { |
||
| 97 | Logger::log('Throw not found exception'); |
||
| 98 | if (NULL === $e) { |
||
| 99 | Logger::log('Not found page throwed without previous exception', LOG_WARNING); |
||
| 100 | $e = new \Exception(_('Page not found'), 404); |
||
| 101 | 1 | } |
|
| 102 | $template = Template::getInstance()->setStatus($e->getCode()); |
||
| 103 | if (preg_match('/json/i', Request::getInstance()->getServer('CONTENT_TYPE'))) { |
||
| 104 | return $template->output(json_encode(array( |
||
| 105 | "success" => FALSE, |
||
| 106 | "error" => $e->getMessage(), |
||
| 107 | )), 'application/json'); |
||
| 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 | * Método que devuelve las rutas |
||
| 119 | * @return string|null |
||
| 120 | */ |
||
| 121 | 1 | public function getSlugs() |
|
| 122 | { |
||
| 123 | 1 | return $this->slugs; |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return mixed |
||
| 128 | */ |
||
| 129 | 1 | public function getRoutes() { |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Method that extract all routes in the platform |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function getAllRoutes() |
||
| 138 | { |
||
| 139 | $routes = []; |
||
| 140 | foreach ($this->getRoutes() as $path => $route) { |
||
| 141 | if (array_key_exists('slug', $route)) { |
||
| 142 | $routes[$route['slug']] = $path; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | return $routes; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Método que calcula el objeto a enrutar |
||
| 150 | * |
||
| 151 | * @param string|null $route |
||
| 152 | * |
||
| 153 | * @throws \Exception |
||
| 154 | * @return string HTML |
||
| 155 | */ |
||
| 156 | public function execute($route) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Método que busca el componente que ejecuta la ruta |
||
| 181 | * |
||
| 182 | * @param string $route |
||
| 183 | * |
||
| 184 | * @throws \PSFS\base\exception\RouterException |
||
| 185 | */ |
||
| 186 | protected function searchAction($route) |
||
| 187 | { |
||
| 188 | Logger::log('Searching action to execute: ' . $route, LOG_INFO); |
||
| 189 | //Revisamos si tenemos la ruta registrada |
||
| 190 | $parts = parse_url($route); |
||
| 191 | $path = (array_key_exists('path', $parts)) ? $parts['path'] : $route; |
||
| 192 | $httpRequest = Request::getInstance()->getMethod(); |
||
| 193 | foreach ($this->routing as $pattern => $action) { |
||
| 194 | list($httpMethod, $routePattern) = RouterHelper::extractHttpRoute($pattern); |
||
| 195 | $matched = RouterHelper::matchRoutePattern($routePattern, $path); |
||
| 196 | if ($matched && ($httpMethod === "ALL" || $httpRequest === $httpMethod) && RouterHelper::compareSlashes($routePattern, $path)) { |
||
| 197 | $get = RouterHelper::extractComponents($route, $routePattern); |
||
| 198 | /** @var $class \PSFS\base\types\Controller */ |
||
| 199 | $class = RouterHelper::getClassToCall($action); |
||
| 200 | try { |
||
| 201 | $this->executeCachedRoute($route, $action, $class, $get); |
||
| 202 | } catch (\Exception $e) { |
||
| 203 | Logger::log($e->getMessage(), LOG_ERR); |
||
| 204 | throw new RouterException($e->getMessage(), 404, $e); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | throw new RouterException(_("Ruta no encontrada")); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Método que manda las cabeceras de autenticación |
||
| 213 | * @return string HTML |
||
| 214 | */ |
||
| 215 | protected function sentAuthHeader() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Method that check if the proyect has sub project to include |
||
| 222 | * @param boolean $hydrateRoute |
||
| 223 | */ |
||
| 224 | 1 | private function checkExternalModules($hydrateRoute = true) |
|
| 225 | { |
||
| 226 | 1 | $externalModules = Config::getParam('modules.extend'); |
|
| 227 | 1 | if (null !== $externalModules) { |
|
| 228 | $externalModules = explode(',', $externalModules); |
||
| 229 | foreach ($externalModules as &$module) { |
||
| 230 | $module = preg_replace('/(\\\|\/)/', DIRECTORY_SEPARATOR, $module); |
||
| 231 | $externalModulePath = VENDOR_DIR . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . 'src'; |
||
| 232 | if (file_exists($externalModulePath)) { |
||
| 233 | $externalModule = $this->finder->directories()->in($externalModulePath)->depth(0); |
||
| 234 | if (!empty($externalModule)) { |
||
| 235 | foreach ($externalModule as $modulePath) { |
||
| 236 | $extModule = $modulePath->getBasename(); |
||
| 237 | $moduleAutoloader = realpath($externalModulePath . DIRECTORY_SEPARATOR . $extModule . DIRECTORY_SEPARATOR . 'autoload.php'); |
||
| 238 | if (file_exists($moduleAutoloader)) { |
||
| 239 | @include $moduleAutoloader; |
||
| 240 | if ($hydrateRoute) { |
||
| 241 | $this->routing = $this->inspectDir($externalModulePath . DIRECTORY_SEPARATOR . $extModule, '\\' . $extModule, $this->routing); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 | 1 | } |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Method that gather all the routes in the project |
||
| 253 | */ |
||
| 254 | 1 | private function generateRouting() |
|
| 255 | { |
||
| 256 | 1 | $base = SOURCE_DIR; |
|
| 257 | 1 | $modulesPath = realpath(CORE_DIR); |
|
| 258 | 1 | $this->routing = $this->inspectDir($base, "PSFS", array()); |
|
| 259 | 1 | if (file_exists($modulesPath)) { |
|
| 260 | $modules = $this->finder->directories()->in($modulesPath)->depth(0); |
||
| 261 | foreach ($modules as $modulePath) { |
||
| 262 | $module = $modulePath->getBasename(); |
||
| 263 | $this->routing = $this->inspectDir($modulesPath . DIRECTORY_SEPARATOR . $module, $module, $this->routing); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | 1 | $this->checkExternalModules(); |
|
| 267 | 1 | $this->cache->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", $this->domains, Cache::JSON, TRUE); |
|
| 268 | 1 | } |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Método que regenera el fichero de rutas |
||
| 272 | * @throws ConfigException |
||
| 273 | */ |
||
| 274 | 1 | public function hydrateRouting() |
|
| 275 | { |
||
| 276 | 1 | $this->generateRouting(); |
|
| 277 | 1 | $home = Config::getInstance()->get('home_action'); |
|
| 278 | 1 | if (NULL !== $home || $home !== '') { |
|
| 279 | 1 | $home_params = NULL; |
|
| 280 | 1 | foreach ($this->routing as $pattern => $params) { |
|
| 281 | 1 | list($method, $route) = RouterHelper::extractHttpRoute($pattern); |
|
| 282 | 1 | if (preg_match("/" . preg_quote($route, "/") . "$/i", "/" . $home)) { |
|
| 283 | $home_params = $params; |
||
| 284 | } |
||
| 285 | 1 | } |
|
| 286 | 1 | if (NULL !== $home_params) { |
|
| 287 | $this->routing['/'] = $home_params; |
||
| 288 | } |
||
| 289 | 1 | } |
|
| 290 | 1 | } |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
| 294 | * |
||
| 295 | * @param string $origen |
||
| 296 | * @param string $namespace |
||
| 297 | * @param array $routing |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | * @throws ConfigException |
||
| 301 | */ |
||
| 302 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
| 303 | { |
||
| 304 | 1 | $files = $this->finder->files()->in($origen)->path('/(controller|api)/i')->depth(1)->name("*.php"); |
|
| 305 | 1 | foreach ($files as $file) { |
|
| 306 | 1 | $filename = str_replace("/", '\\', str_replace($origen, '', $file->getPathname())); |
|
| 307 | 1 | $routing = $this->addRouting($namespace . str_replace('.php', '', $filename), $routing, $namespace); |
|
| 308 | 1 | } |
|
| 309 | 1 | $this->finder = new Finder(); |
|
| 310 | |||
| 311 | 1 | return $routing; |
|
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Checks that a namespace exists |
||
| 316 | * @param string $namespace |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | 1 | public static function exists($namespace) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Método que añade nuevas rutas al array de referencia |
||
| 326 | * |
||
| 327 | * @param string $namespace |
||
| 328 | * @param array $routing |
||
| 329 | * @param string $module |
||
| 330 | * |
||
| 331 | * @return array |
||
| 332 | * @throws ConfigException |
||
| 333 | */ |
||
| 334 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
| 335 | { |
||
| 336 | 1 | if (self::exists($namespace)) { |
|
| 337 | 1 | $reflection = new \ReflectionClass($namespace); |
|
| 338 | 1 | if (FALSE === $reflection->isAbstract() && FALSE === $reflection->isInterface()) { |
|
| 339 | 1 | $this->extractDomain($reflection); |
|
| 340 | 1 | $classComments = $reflection->getDocComment(); |
|
| 341 | 1 | preg_match('/@api\ (.*)\n/im', $classComments, $apiPath); |
|
| 342 | 1 | $api = ''; |
|
| 343 | 1 | if (count($apiPath)) { |
|
| 344 | $api = array_key_exists(1, $apiPath) ? $apiPath[1] : $api; |
||
| 345 | } |
||
| 346 | 1 | foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
| 347 | 1 | if (preg_match('/@route\ /i', $method->getDocComment())) { |
|
| 348 | 1 | list($route, $info) = RouterHelper::extractRouteInfo($method, str_replace('\\', '', $api), str_replace('\\', '', $module)); |
|
| 349 | |||
| 350 | 1 | if (null !== $route && null !== $info) { |
|
| 351 | 1 | $info['class'] = $namespace; |
|
| 352 | 1 | $routing[$route] = $info; |
|
| 353 | 1 | } |
|
| 354 | 1 | } |
|
| 355 | 1 | } |
|
| 356 | 1 | } |
|
| 357 | 1 | } |
|
| 358 | |||
| 359 | 1 | return $routing; |
|
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
| 364 | * |
||
| 365 | * @param \ReflectionClass $class |
||
| 366 | * |
||
| 367 | * @return Router |
||
| 368 | * @throws ConfigException |
||
| 369 | */ |
||
| 370 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
| 371 | { |
||
| 372 | //Calculamos los dominios para las plantillas |
||
| 373 | 1 | if ($class->hasConstant("DOMAIN") && !$class->isAbstract()) { |
|
| 374 | 1 | if (!$this->domains) { |
|
| 375 | 1 | $this->domains = []; |
|
| 376 | 1 | } |
|
| 377 | 1 | $domain = "@" . $class->getConstant("DOMAIN") . "/"; |
|
| 378 | 1 | if (!array_key_exists($domain, $this->domains)) { |
|
| 379 | 1 | $this->domains[$domain] = RouterHelper::extractDomainInfo($class, $domain); |
|
| 380 | 1 | } |
|
| 381 | 1 | } |
|
| 382 | |||
| 383 | 1 | return $this; |
|
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Método que genera las urls amigables para usar dentro del framework |
||
| 388 | * @return Router |
||
| 389 | */ |
||
| 390 | 1 | public function simpatize() |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Método que devuelve una ruta del framework |
||
| 403 | * |
||
| 404 | * @param string $slug |
||
| 405 | * @param boolean $absolute |
||
| 406 | * @param array $params |
||
| 407 | * |
||
| 408 | * @return string|null |
||
| 409 | * @throws RouterException |
||
| 410 | */ |
||
| 411 | 1 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
|
| 412 | { |
||
| 413 | 1 | if (strlen($slug) === 0) { |
|
| 414 | return ($absolute) ? Request::getInstance()->getRootUrl() . '/' : '/'; |
||
| 415 | } |
||
| 416 | 1 | if (!is_array($this->slugs) || !array_key_exists($slug, $this->slugs)) { |
|
| 417 | throw new RouterException(_("No existe la ruta especificada")); |
||
| 418 | } |
||
| 419 | 1 | $url = ($absolute) ? Request::getInstance()->getRootUrl() . $this->slugs[$slug] : $this->slugs[$slug]; |
|
| 420 | 1 | if (!empty($params)) foreach ($params as $key => $value) { |
|
| 421 | $url = str_replace("{" . $key . "}", $value, $url); |
||
| 422 | 1 | } elseif (!empty($this->routing[$this->slugs[$slug]]["default"])) { |
|
| 423 | 1 | $url = ($absolute) ? Request::getInstance()->getRootUrl() . $this->routing[$this->slugs[$slug]]["default"] : $this->routing[$this->slugs[$slug]]["default"]; |
|
| 424 | 1 | } |
|
| 425 | |||
| 426 | 1 | return preg_replace('/(GET|POST|PUT|DELETE|ALL)\#\|\#/', '', $url); |
|
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Método que devuelve las rutas de administración |
||
| 431 | * @deprecated |
||
| 432 | * @return array |
||
| 433 | */ |
||
| 434 | public function getAdminRoutes() |
||
| 435 | { |
||
| 436 | return AdminHelper::getAdminRoutes($this->routing); |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Método que devuelve le controlador del admin |
||
| 441 | * @deprecated |
||
| 442 | * @return Admin |
||
| 443 | */ |
||
| 444 | public function getAdmin() |
||
| 445 | { |
||
| 446 | return Admin::getInstance(); |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Método que extrae los dominios |
||
| 451 | * @return array |
||
| 452 | */ |
||
| 453 | public function getDomains() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
| 460 | * |
||
| 461 | * @param string $route |
||
| 462 | * @param array|null $action |
||
| 463 | * @param types\Controller $class |
||
| 464 | * @param array $params |
||
| 465 | */ |
||
| 466 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
| 467 | { |
||
| 468 | Logger::log('Executing route ' . $route, LOG_INFO); |
||
| 469 | Security::getInstance()->setSessionKey("__CACHE__", $action); |
||
| 470 | $cache = Cache::needCache(); |
||
| 471 | $execute = TRUE; |
||
| 472 | if (FALSE !== $cache && Config::getInstance()->getDebugMode() === FALSE) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Parse slugs to create translations |
||
| 496 | * |
||
| 497 | * @param string $absoluteTranslationFileName |
||
| 498 | */ |
||
| 499 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
| 517 | |||
| 518 | } |
||
| 519 |
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.