Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router class optimization. #6004

Merged
merged 3 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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;
}
Expand Down Expand Up @@ -672,4 +649,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);
}
}