Skip to content

Commit

Permalink
Merge pull request #6004 from iRedds/router
Browse files Browse the repository at this point in the history
Router class optimization.
  • Loading branch information
samsonasik authored May 19, 2022
2 parents 8a8c183 + 2724e4b commit 9b474c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ parameters:

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

-
Expand Down
55 changes: 21 additions & 34 deletions system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,54 +454,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;
}
Expand Down Expand Up @@ -669,4 +646,14 @@ protected function setDefaultController()

log_message('info', 'Used the default controller.');
}

/**
* @param callable|string $handler
*/
protected function setMatchedRoute(string $route, $handler): void
{
$this->matchedRoute = [$route, $handler];

$this->matchedRouteOptions = $this->collection->getRoutesOptions($route);
}
}

0 comments on commit 9b474c7

Please sign in to comment.