From abdeaa560d2a1e1380d6834d2d7115250d04fe3b Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Wed, 18 May 2022 11:13:53 +0800 Subject: [PATCH 1/3] Router class optimization. Signed-off-by: Andrey Pyzhikov <5071@mail.ru> --- phpstan-baseline.neon.dist | 2 +- system/Router/Router.php | 59 ++++++++++++++++---------------------- 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/phpstan-baseline.neon.dist b/phpstan-baseline.neon.dist index 58c7bd61823c..9c7258cefc44 100644 --- a/phpstan-baseline.neon.dist +++ b/phpstan-baseline.neon.dist @@ -687,7 +687,7 @@ parameters: - message: "#^Call to an undefined method CodeIgniter\\\\Router\\\\RouteCollectionInterface\\:\\:getRoutesOptions\\(\\)\\.$#" - count: 2 + count: 1 path: system/Router/Router.php - diff --git a/system/Router/Router.php b/system/Router/Router.php index ea5eb79b8d70..e8eaa94e5c82 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -457,54 +457,31 @@ protected function checkRoutes(string $uri): bool $this->params = $matches; - $this->matchedRoute = [ - $matchedKey, - $handler, - ]; - - $this->matchedRouteOptions = $this->collection->getRoutesOptions($matchedKey); + $this->setMatchedRoute($matchedKey, $handler); return true; } - if (strpos($handler, '$') !== false && strpos($routeKey, '(') !== false) { - // Using back-references + [$controller, ] = explode('::', $handler); + // Checks `/` in controller name + if (strpos($controller, '/') !== false) { + throw RouterException::forInvalidControllerName($handler); + } + + if (strpos($handler, '$') !== false && strpos($routeKey, '(') !== false) { // Checks dynamic controller - [$controller, ] = explode('::', $handler); if (strpos($controller, '$') !== false) { throw RouterException::forDynamicController($handler); } - // Checks `/` in controller name - if (strpos($controller, '/') !== false) { - throw RouterException::forInvalidControllerName($handler); - } - - if (strpos($routeKey, '/') !== false) { - $replacekey = str_replace('/(.*)', '', $routeKey); - $handler = preg_replace('#^' . $routeKey . '$#u', $handler, $uri); - $handler = str_replace($replacekey, str_replace('/', '\\', $replacekey), $handler); - } else { - $handler = preg_replace('#^' . $routeKey . '$#u', $handler, $uri); - } - } elseif (strpos($handler, '/') !== false) { - [$controller, $method] = explode('::', $handler); - - // Only replace slashes in the controller, not in the method. - $controller = str_replace('/', '\\', $controller); - - $handler = $controller . '::' . $method; + // Using back-references + $handler = preg_replace('#^' . $routeKey . '$#u', $handler, $uri); } $this->setRequest(explode('/', $handler)); - $this->matchedRoute = [ - $matchedKey, - $handler, - ]; - - $this->matchedRouteOptions = $this->collection->getRoutesOptions($matchedKey); + $this->setMatchedRoute($matchedKey, $handler); return true; } @@ -672,4 +649,18 @@ protected function setDefaultController() log_message('info', 'Used the default controller.'); } + + /** + * @param callable|string $handler + * + * @return static + */ + protected function setMatchedRoute(string $route, $handler) + { + $this->matchedRoute = [$route, $handler]; + + $this->matchedRouteOptions = $this->collection->getRoutesOptions($route); + + return $this; + } } From 218b6bb2973b68479404d644ededc21e67909c48 Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Fri, 20 May 2022 00:49:17 +0800 Subject: [PATCH 2/3] The setMatchedRoute method now returns nothing. Signed-off-by: Andrey Pyzhikov <5071@mail.ru> --- system/Router/Router.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/system/Router/Router.php b/system/Router/Router.php index e8eaa94e5c82..a11e7127d77a 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -653,14 +653,12 @@ protected function setDefaultController() /** * @param callable|string $handler * - * @return static + * @return void */ protected function setMatchedRoute(string $route, $handler) { $this->matchedRoute = [$route, $handler]; $this->matchedRouteOptions = $this->collection->getRoutesOptions($route); - - return $this; } } From 2724e4b2e0a868a1cda15380fb18ecf0842c85e7 Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Fri, 20 May 2022 00:53:04 +0800 Subject: [PATCH 3/3] Added return type Signed-off-by: Andrey Pyzhikov <5071@mail.ru> --- system/Router/Router.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/system/Router/Router.php b/system/Router/Router.php index a11e7127d77a..f319433732aa 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -652,10 +652,8 @@ protected function setDefaultController() /** * @param callable|string $handler - * - * @return void */ - protected function setMatchedRoute(string $route, $handler) + protected function setMatchedRoute(string $route, $handler): void { $this->matchedRoute = [$route, $handler];