Skip to content

Commit abdeaa5

Browse files
committed
Router class optimization.
Signed-off-by: Andrey Pyzhikov <5071@mail.ru>
1 parent c93b967 commit abdeaa5

2 files changed

Lines changed: 26 additions & 35 deletions

File tree

phpstan-baseline.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ parameters:
687687

688688
-
689689
message: "#^Call to an undefined method CodeIgniter\\\\Router\\\\RouteCollectionInterface\\:\\:getRoutesOptions\\(\\)\\.$#"
690-
count: 2
690+
count: 1
691691
path: system/Router/Router.php
692692

693693
-

system/Router/Router.php

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -457,54 +457,31 @@ protected function checkRoutes(string $uri): bool
457457

458458
$this->params = $matches;
459459

460-
$this->matchedRoute = [
461-
$matchedKey,
462-
$handler,
463-
];
464-
465-
$this->matchedRouteOptions = $this->collection->getRoutesOptions($matchedKey);
460+
$this->setMatchedRoute($matchedKey, $handler);
466461

467462
return true;
468463
}
469464

470-
if (strpos($handler, '$') !== false && strpos($routeKey, '(') !== false) {
471-
// Using back-references
465+
[$controller, ] = explode('::', $handler);
472466

467+
// Checks `/` in controller name
468+
if (strpos($controller, '/') !== false) {
469+
throw RouterException::forInvalidControllerName($handler);
470+
}
471+
472+
if (strpos($handler, '$') !== false && strpos($routeKey, '(') !== false) {
473473
// Checks dynamic controller
474-
[$controller, ] = explode('::', $handler);
475474
if (strpos($controller, '$') !== false) {
476475
throw RouterException::forDynamicController($handler);
477476
}
478477

479-
// Checks `/` in controller name
480-
if (strpos($controller, '/') !== false) {
481-
throw RouterException::forInvalidControllerName($handler);
482-
}
483-
484-
if (strpos($routeKey, '/') !== false) {
485-
$replacekey = str_replace('/(.*)', '', $routeKey);
486-
$handler = preg_replace('#^' . $routeKey . '$#u', $handler, $uri);
487-
$handler = str_replace($replacekey, str_replace('/', '\\', $replacekey), $handler);
488-
} else {
489-
$handler = preg_replace('#^' . $routeKey . '$#u', $handler, $uri);
490-
}
491-
} elseif (strpos($handler, '/') !== false) {
492-
[$controller, $method] = explode('::', $handler);
493-
494-
// Only replace slashes in the controller, not in the method.
495-
$controller = str_replace('/', '\\', $controller);
496-
497-
$handler = $controller . '::' . $method;
478+
// Using back-references
479+
$handler = preg_replace('#^' . $routeKey . '$#u', $handler, $uri);
498480
}
499481

500482
$this->setRequest(explode('/', $handler));
501483

502-
$this->matchedRoute = [
503-
$matchedKey,
504-
$handler,
505-
];
506-
507-
$this->matchedRouteOptions = $this->collection->getRoutesOptions($matchedKey);
484+
$this->setMatchedRoute($matchedKey, $handler);
508485

509486
return true;
510487
}
@@ -672,4 +649,18 @@ protected function setDefaultController()
672649

673650
log_message('info', 'Used the default controller.');
674651
}
652+
653+
/**
654+
* @param callable|string $handler
655+
*
656+
* @return static
657+
*/
658+
protected function setMatchedRoute(string $route, $handler)
659+
{
660+
$this->matchedRoute = [$route, $handler];
661+
662+
$this->matchedRouteOptions = $this->collection->getRoutesOptions($route);
663+
664+
return $this;
665+
}
675666
}

0 commit comments

Comments
 (0)