| Total Complexity | 90 |
| Total Lines | 523 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 5 | Features | 1 |
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 |
||
| 32 | class Router extends KernelRouter |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * Router::handle |
||
| 36 | * |
||
| 37 | * @param KernelMessageUri|null $uri |
||
| 38 | * |
||
| 39 | * @return bool |
||
| 40 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
| 41 | * @throws \ReflectionException |
||
| 42 | */ |
||
| 43 | public function handle(KernelMessageUri $uri = null) |
||
| 44 | { |
||
| 45 | $this->uri = is_null($uri) ? new KernelMessageUri() : $uri; |
||
| 46 | |||
| 47 | // Handle Extension Request |
||
| 48 | if ($this->uri->segments->count()) { |
||
| 49 | $this->handleExtensionRequest(); |
||
| 50 | } else { |
||
| 51 | $uriPath = urldecode( |
||
| 52 | parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH) |
||
| 53 | ); |
||
| 54 | |||
| 55 | $uriPathParts = explode('public/', $uriPath); |
||
| 56 | $uriPath = end($uriPathParts); |
||
| 57 | |||
| 58 | if ($uriPath !== '/') { |
||
| 59 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments( |
||
| 60 | array_filter(explode('/', $uriPath))) |
||
|
|
|||
| 61 | ); |
||
| 62 | } |
||
| 63 | |||
| 64 | unset($uriPathParts, $uriPath); |
||
| 65 | } |
||
| 66 | |||
| 67 | // Load app addresses config |
||
| 68 | $this->addresses = config()->loadFile('addresses', true); |
||
| 69 | |||
| 70 | if ($this->addresses instanceof KernelAddresses) { |
||
| 71 | // Domain routing |
||
| 72 | if (null !== ($domain = $this->addresses->getDomain())) { |
||
| 73 | if (is_array($domain)) { |
||
| 74 | $this->uri->segments->exchangeArray( |
||
| 75 | array_merge($domain, $this->uri->segments->getArrayCopy()) |
||
| 76 | ); |
||
| 77 | $domain = $this->uri->segments->first(); |
||
| 78 | } |
||
| 79 | |||
| 80 | if (false !== ($app = modules()->getApp($domain))) { |
||
| 81 | $this->registerModule($app); |
||
| 82 | } elseif (false !== ($module = modules()->getModule($domain))) { |
||
| 83 | $this->registerModule($module); |
||
| 84 | } |
||
| 85 | } elseif (false !== ($subdomain = $this->uri->getSubdomain())) { |
||
| 86 | if (false !== ($app = modules()->getApp($subdomain))) { |
||
| 87 | $this->registerModule($app); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | // App and Module routing |
||
| 93 | if ($numOfUriSegments = $this->uri->segments->count()) { |
||
| 94 | if (empty($app)) { |
||
| 95 | if (false !== ($module = modules()->getModule($this->uri->segments->first()))) { |
||
| 96 | $this->registerModule($module); |
||
| 97 | |||
| 98 | if ($module->getType() === 'APP') { |
||
| 99 | $this->uri->segments->shift(); |
||
| 100 | $this->handleAppRequest($module); |
||
| 101 | } else { |
||
| 102 | $this->handleSegmentsRequest(); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } elseif (false !== ($module = modules()->getModule($this->uri->segments->first()))) { |
||
| 106 | $this->registerModule($module); |
||
| 107 | |||
| 108 | if ($module->getType() === 'APP') { |
||
| 109 | $this->uri->segments->shift(); |
||
| 110 | $this->handleAppRequest($module); |
||
| 111 | } else { |
||
| 112 | $this->handleSegmentsRequest(); |
||
| 113 | } |
||
| 114 | } else { |
||
| 115 | $this->handleAppRequest($app); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | // Try to translate from uri string |
||
| 120 | if (false !== ($action = $this->addresses->getTranslation($this->uri->segments->__toString()))) { |
||
| 121 | if ( ! $action->isValidHttpMethod(input()->server('REQUEST_METHOD')) && ! $action->isAnyHttpMethod()) { |
||
| 122 | output()->sendError(405); |
||
| 123 | } else { |
||
| 124 | // Checks if action closure is an array |
||
| 125 | if (is_array($closureSegments = $action->getClosure())) { |
||
| 126 | $this->uri->segments->exchangeArray($closureSegments); |
||
| 127 | |||
| 128 | if (false !== ($module = modules()->getModule($this->uri->segments->first()))) { |
||
| 129 | $this->registerModule($module); |
||
| 130 | |||
| 131 | if ($module->getType() === 'APP') { |
||
| 132 | $this->uri->segments->shift(); |
||
| 133 | $this->handleAppRequest($module); |
||
| 134 | } else { |
||
| 135 | $this->handleSegmentsRequest(); |
||
| 136 | } |
||
| 137 | } else { |
||
| 138 | $this->handleSegmentsRequest(); |
||
| 139 | } |
||
| 140 | } else { |
||
| 141 | if (false !== ($parseSegments = $action->getParseUriString($this->uri->segments->__toString()))) { |
||
| 142 | $uriSegments = $parseSegments; |
||
| 143 | } else { |
||
| 144 | $uriSegments = []; |
||
| 145 | } |
||
| 146 | |||
| 147 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
| 148 | $uriString = $this->uri->segments->__toString(); |
||
| 149 | |||
| 150 | $this->parseAction($action, $uriSegments); |
||
| 151 | if ( ! empty(services()->has('controller'))) { |
||
| 152 | return true; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | // Try to get route from controller & page |
||
| 159 | if ($numOfUriSegments = $this->uri->segments->count()) { |
||
| 160 | $uriSegments = $this->uri->segments->getArrayCopy(); |
||
| 161 | $uriString = $this->uri->segments->__toString(); |
||
| 162 | |||
| 163 | for ($i = 0; $i <= $numOfUriSegments; $i++) { |
||
| 164 | $uriRoutedSegments = array_slice($uriSegments, 0, ($numOfUriSegments - $i)); |
||
| 165 | $modules = modules()->getArrayCopy(); |
||
| 166 | |||
| 167 | foreach ($modules as $module) { |
||
| 168 | $controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
||
| 169 | |||
| 170 | if ($module->getNamespace() === 'O2System\Framework\\') { |
||
| 171 | $controllerNamespace = 'O2System\Framework\Http\Controllers\\'; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Try to find requested controller |
||
| 176 | */ |
||
| 177 | if (class_exists($controllerClassName = $controllerNamespace . implode('\\', |
||
| 178 | array_map('studlycase', $uriRoutedSegments)))) { |
||
| 179 | |||
| 180 | if ($controllerClassName::$inherited) { |
||
| 181 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
| 182 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
| 183 | $uriSegments); |
||
| 184 | |||
| 185 | break; |
||
| 186 | } else { |
||
| 187 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
| 188 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
| 189 | $uriSegments); |
||
| 190 | |||
| 191 | break; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Try to find requested page |
||
| 197 | */ |
||
| 198 | if (false !== ($pagesDir = $module->getResourcesDir('pages'))) { |
||
| 199 | if ($controllerClassName = $this->getPagesControllerClassName()) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Try to find from database |
||
| 203 | */ |
||
| 204 | $modelClassName = str_replace('Controllers', 'Models', $controllerClassName); |
||
| 205 | |||
| 206 | if (class_exists($modelClassName)) { |
||
| 207 | models()->load($modelClassName, 'controller'); |
||
| 208 | |||
| 209 | if (false !== ($page = models('controller')->find($uriString, 'segments'))) { |
||
| 210 | if (isset($page->content)) { |
||
| 211 | presenter()->partials->offsetSet('content', $page->content); |
||
| 212 | |||
| 213 | $this->setController( |
||
| 214 | (new KernelControllerDataStructure($controllerClassName)) |
||
| 215 | ->setRequestMethod('index') |
||
| 216 | ); |
||
| 217 | |||
| 218 | return true; |
||
| 219 | break; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Try to find from page file |
||
| 226 | */ |
||
| 227 | foreach (['.phtml', '.vue'] as $pageExtension) { |
||
| 228 | $pageFilePath = $pagesDir . implode(DIRECTORY_SEPARATOR, |
||
| 229 | array_map('dash', $uriRoutedSegments)) . $pageExtension; |
||
| 230 | |||
| 231 | if (is_file($pageFilePath)) { |
||
| 232 | presenter()->page->setFile($pageFilePath); |
||
| 233 | break; |
||
| 234 | } else { |
||
| 235 | $pageFilePath = str_replace($pageExtension, |
||
| 236 | DIRECTORY_SEPARATOR . 'index' . $pageExtension, $pageFilePath); |
||
| 237 | if (is_file($pageFilePath)) { |
||
| 238 | presenter()->page->setFile($pageFilePath); |
||
| 239 | break; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | if (presenter()->page->file instanceof SplFileInfo) { |
||
| 245 | $this->setController( |
||
| 246 | (new KernelControllerDataStructure($controllerClassName)) |
||
| 247 | ->setRequestMethod('index') |
||
| 248 | ); |
||
| 249 | |||
| 250 | return true; |
||
| 251 | break; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | // break the loop if the controller has been set |
||
| 258 | if (services()->has('controller')) { |
||
| 259 | return true; |
||
| 260 | break; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | if (class_exists($controllerClassName = modules()->top()->getDefaultControllerClassName())) { |
||
| 266 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
| 267 | $this->uri->segments->getArrayCopy()); |
||
| 268 | |||
| 269 | return true; |
||
| 270 | } |
||
| 271 | |||
| 272 | // Let's the framework do the rest when there is no controller found |
||
| 273 | // the framework will redirect to PAGE 404 |
||
| 274 | } |
||
| 275 | |||
| 276 | // ------------------------------------------------------------------------ |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Router::handleExtensionRequest |
||
| 280 | */ |
||
| 281 | protected function handleExtensionRequest() |
||
| 282 | { |
||
| 283 | $lastSegment = $this->uri->segments->last(); |
||
| 284 | |||
| 285 | if (strpos($lastSegment, '.json') !== false) { |
||
| 286 | output()->setContentType('application/json'); |
||
| 287 | $lastSegment = str_replace('.json', '', $lastSegment); |
||
| 288 | $this->uri->segments->pop(); |
||
| 289 | $this->uri->segments->push($lastSegment); |
||
| 290 | } elseif (strpos($lastSegment, '.xml') !== false) { |
||
| 291 | output()->setContentType('application/xml'); |
||
| 292 | $lastSegment = str_replace('.xml', '', $lastSegment); |
||
| 293 | $this->uri->segments->pop(); |
||
| 294 | $this->uri->segments->push($lastSegment); |
||
| 295 | } elseif (strpos($lastSegment, '.js') !== false) { |
||
| 296 | output()->setContentType('application/x-javascript'); |
||
| 297 | $lastSegment = str_replace('.js', '', $lastSegment); |
||
| 298 | $this->uri->segments->pop(); |
||
| 299 | $this->uri->segments->push($lastSegment); |
||
| 300 | } elseif (strpos($lastSegment, '.css') !== false) { |
||
| 301 | output()->setContentType('text/css'); |
||
| 302 | $lastSegment = str_replace('.css', '', $lastSegment); |
||
| 303 | $this->uri->segments->pop(); |
||
| 304 | $this->uri->segments->push($lastSegment); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | // ------------------------------------------------------------------------ |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Router::handleAppRequest |
||
| 312 | * |
||
| 313 | * @param \O2System\Framework\Containers\Modules\DataStructures\Module $app |
||
| 314 | */ |
||
| 315 | public function handleAppRequest(FrameworkModuleDataStructure $app) |
||
| 316 | { |
||
| 317 | // Find App module |
||
| 318 | foreach([null,'modules', 'plugins'] as $additionalSegment) { |
||
| 319 | if(empty($additionalSegment)) { |
||
| 320 | $segments = [ |
||
| 321 | $app->getParameter(), |
||
| 322 | $this->uri->segments->first(), |
||
| 323 | ]; |
||
| 324 | } else { |
||
| 325 | $segments = [ |
||
| 326 | $app->getParameter(), |
||
| 327 | $additionalSegment, |
||
| 328 | $this->uri->segments->first(), |
||
| 329 | ]; |
||
| 330 | } |
||
| 331 | |||
| 332 | if (false !== ($module = modules()->getModule($segments))) { |
||
| 333 | $this->uri->segments->shift(); |
||
| 334 | |||
| 335 | $this->registerModule($module); |
||
| 336 | $this->handleSegmentsRequest(); |
||
| 337 | break; |
||
| 338 | } |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | // ------------------------------------------------------------------------ |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Router::handleModuleRequest |
||
| 346 | */ |
||
| 347 | public function handleSegmentsRequest() |
||
| 374 | } |
||
| 375 | } |
||
| 376 | } |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | // ------------------------------------------------------------------------ |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Router::getPagesControllerClassName |
||
| 384 | * |
||
| 385 | * @return bool|string |
||
| 386 | */ |
||
| 387 | final protected function getPagesControllerClassName() |
||
| 388 | { |
||
| 389 | $modules = modules()->getArrayCopy(); |
||
| 390 | |||
| 391 | foreach ($modules as $module) { |
||
| 392 | $controllerClassName = $module->getNamespace() . 'Controllers\Pages'; |
||
| 393 | if ($module->getNamespace() === 'O2System\Framework\\') { |
||
| 394 | $controllerClassName = 'O2System\Framework\Http\Controllers\Pages'; |
||
| 395 | } |
||
| 396 | |||
| 397 | if (class_exists($controllerClassName)) { |
||
| 398 | return $controllerClassName; |
||
| 399 | break; |
||
| 400 | } |
||
| 401 | } |
||
| 402 | |||
| 403 | if (class_exists('O2System\Framework\Http\Controllers\Pages')) { |
||
| 404 | return 'O2System\Framework\Http\Controllers\Pages'; |
||
| 405 | } |
||
| 406 | |||
| 407 | return false; |
||
| 408 | } |
||
| 409 | |||
| 410 | // ------------------------------------------------------------------------ |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Router::registerModule |
||
| 414 | * |
||
| 415 | * @param FrameworkModuleDataStructure $module |
||
| 416 | */ |
||
| 417 | final public function registerModule(FrameworkModuleDataStructure $module) |
||
| 418 | { |
||
| 419 | // Push Subdomain App Module |
||
| 420 | modules()->push($module); |
||
| 421 | |||
| 422 | // Add Config FilePath |
||
| 423 | config()->addFilePath($module->getRealPath()); |
||
| 424 | |||
| 425 | // Reload Config |
||
| 426 | config()->reload(); |
||
| 427 | |||
| 428 | // Load modular addresses config |
||
| 429 | if (false !== ($configDir = $module->getDir('config', true))) { |
||
| 430 | unset($addresses); |
||
| 431 | |||
| 432 | $reconfig = false; |
||
| 433 | if (is_file( |
||
| 434 | $filePath = $configDir . ucfirst( |
||
| 435 | strtolower(ENVIRONMENT) |
||
| 436 | ) . DIRECTORY_SEPARATOR . 'Addresses.php' |
||
| 437 | )) { |
||
| 438 | require($filePath); |
||
| 439 | $reconfig = true; |
||
| 440 | } elseif (is_file( |
||
| 441 | $filePath = $configDir . 'Addresses.php' |
||
| 442 | )) { |
||
| 443 | require($filePath); |
||
| 444 | $reconfig = true; |
||
| 445 | } |
||
| 446 | |||
| 447 | if ( ! $reconfig) { |
||
| 448 | $controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
||
| 449 | $controllerClassName = $controllerNamespace . studlycase($module->getParameter()); |
||
| 450 | |||
| 451 | if (class_exists($controllerClassName)) { |
||
| 452 | $this->addresses->any( |
||
| 453 | '/', |
||
| 454 | function () use ($controllerClassName) { |
||
| 455 | return new $controllerClassName(); |
||
| 456 | } |
||
| 457 | ); |
||
| 458 | } |
||
| 459 | } elseif (isset($addresses)) { |
||
| 460 | $this->addresses = $addresses; |
||
| 461 | } |
||
| 462 | } else { |
||
| 463 | $controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
||
| 464 | $controllerClassName = $controllerNamespace . studlycase($module->getParameter()); |
||
| 465 | |||
| 466 | if (class_exists($controllerClassName)) { |
||
| 467 | $this->addresses->any( |
||
| 468 | '/', |
||
| 469 | function () use ($controllerClassName) { |
||
| 470 | return new $controllerClassName(); |
||
| 471 | } |
||
| 472 | ); |
||
| 473 | } |
||
| 474 | } |
||
| 475 | } |
||
| 476 | |||
| 477 | // ------------------------------------------------------------------------ |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Router::parseAction |
||
| 481 | * |
||
| 482 | * @param KernelActionDataStructure $action |
||
| 483 | * @param array $uriSegments |
||
| 484 | * |
||
| 485 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
| 486 | * @throws \ReflectionException |
||
| 487 | */ |
||
| 488 | protected function parseAction(KernelActionDataStructure $action, array $uriSegments = []) |
||
| 555 | } |
||
| 556 | } |
||
| 557 | } |
||
| 558 | } |