class TraceableUrlMatcher
TraceableUrlMatcher helps debug path info matching by tracing the match.
@author Fabien Potencier <fabien@symfony.com>
Hierarchy
- class \Symfony\Component\Routing\Matcher\UrlMatcher implements \Symfony\Component\Routing\Matcher\UrlMatcherInterface, \Symfony\Component\Routing\Matcher\RequestMatcherInterface
- class \Symfony\Component\Routing\Matcher\TraceableUrlMatcher extends \Symfony\Component\Routing\Matcher\UrlMatcher
Expanded class hierarchy of TraceableUrlMatcher
File
-
vendor/
symfony/ routing/ Matcher/ TraceableUrlMatcher.php, line 24
Namespace
Symfony\Component\Routing\MatcherView source
class TraceableUrlMatcher extends UrlMatcher {
public const ROUTE_DOES_NOT_MATCH = 0;
public const ROUTE_ALMOST_MATCHES = 1;
public const ROUTE_MATCHES = 2;
protected array $traces;
public function getTraces(string $pathinfo) : array {
$this->traces = [];
try {
$this->match($pathinfo);
} catch (ExceptionInterface) {
}
return $this->traces;
}
public function getTracesForRequest(Request $request) : array {
$this->request = $request;
$traces = $this->getTraces($request->getPathInfo());
$this->request = null;
return $traces;
}
protected function matchCollection(string $pathinfo, RouteCollection $routes) : array {
// HEAD and GET are equivalent as per RFC
if ('HEAD' === ($method = $this->context
->getMethod())) {
$method = 'GET';
}
$supportsTrailingSlash = 'GET' === $method && $this instanceof RedirectableUrlMatcherInterface;
$trimmedPathinfo = rtrim($pathinfo, '/') ?: '/';
foreach ($routes as $name => $route) {
$compiledRoute = $route->compile();
$staticPrefix = rtrim($compiledRoute->getStaticPrefix(), '/');
$requiredMethods = $route->getMethods();
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
if ('' !== $staticPrefix && !str_starts_with($trimmedPathinfo, $staticPrefix)) {
$this->addTrace(\sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
continue;
}
$regex = $compiledRoute->getRegex();
$pos = strrpos($regex, '$');
$hasTrailingSlash = '/' === $regex[$pos - 1];
$regex = substr_replace($regex, '/?$', $pos - $hasTrailingSlash, 1 + $hasTrailingSlash);
if (!preg_match($regex, $pathinfo, $matches)) {
// does it match without any requirements?
$r = new Route($route->getPath(), $route->getDefaults(), [], $route->getOptions());
$cr = $r->compile();
if (!preg_match($cr->getRegex(), $pathinfo)) {
$this->addTrace(\sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
continue;
}
foreach ($route->getRequirements() as $n => $regex) {
$r = new Route($route->getPath(), $route->getDefaults(), [
$n => $regex,
], $route->getOptions());
$cr = $r->compile();
if (\in_array($n, $cr->getVariables()) && !preg_match($cr->getRegex(), $pathinfo)) {
$this->addTrace(\sprintf('Requirement for "%s" does not match (%s)', $n, $regex), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue 2;
}
}
continue;
}
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\\{[\\w\\x80-\\xFF]+\\}/?$#', $route->getPath());
if ($hasTrailingVar && ($hasTrailingSlash || null === ($m = $matches[\count($compiledRoute->getPathVariables())] ?? null) || '/' !== ($m[-1] ?? '/')) && preg_match($regex, $trimmedPathinfo, $m)) {
if ($hasTrailingSlash) {
$matches = $m;
}
else {
$hasTrailingVar = false;
}
}
$hostMatches = [];
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context
->getHost(), $hostMatches)) {
$this->addTrace(\sprintf('Host "%s" does not match the requirement ("%s")', $this->context
->getHost(), $route->getHost()), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue;
}
$attributes = $this->getAttributes($route, $name, array_replace($matches, $hostMatches));
$status = $this->handleRouteRequirements($pathinfo, $name, $route, $attributes);
if (self::REQUIREMENT_MISMATCH === $status[0]) {
$this->addTrace(\sprintf('Condition "%s" does not evaluate to "true"', $route->getCondition()), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue;
}
if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods, true))) {
$this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
return $this->allow = $this->allowSchemes = [];
}
$this->addTrace(\sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
continue;
}
if ($route->getSchemes() && !$route->hasScheme($this->context
->getScheme())) {
$this->allowSchemes = array_merge($this->allowSchemes, $route->getSchemes());
$this->addTrace(\sprintf('Scheme "%s" does not match any of the required schemes (%s)', $this->context
->getScheme(), implode(', ', $route->getSchemes())), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue;
}
if ($requiredMethods && !\in_array($method, $requiredMethods, true)) {
$this->allow = array_merge($this->allow, $requiredMethods);
$this->addTrace(\sprintf('Method "%s" does not match any of the required methods (%s)', $this->context
->getMethod(), implode(', ', $requiredMethods)), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue;
}
$this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
return array_replace($attributes, $status[1] ?? []);
}
return [];
}
private function addTrace(string $log, int $level = self::ROUTE_DOES_NOT_MATCH, ?string $name = null, ?Route $route = null) : void {
$this->traces[] = [
'log' => $log,
'name' => $name,
'level' => $level,
'path' => $route?->getPath(),
];
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
TraceableUrlMatcher::$traces | protected | property | |||
TraceableUrlMatcher::addTrace | private | function | |||
TraceableUrlMatcher::getTraces | public | function | |||
TraceableUrlMatcher::getTracesForRequest | public | function | |||
TraceableUrlMatcher::matchCollection | protected | function | Tries to match a URL with a set of routes. | Overrides UrlMatcher::matchCollection | |
TraceableUrlMatcher::ROUTE_ALMOST_MATCHES | public | constant | |||
TraceableUrlMatcher::ROUTE_DOES_NOT_MATCH | public | constant | |||
TraceableUrlMatcher::ROUTE_MATCHES | public | constant | |||
UrlMatcher::$allow | protected | property | Collects HTTP methods that would be allowed for the request. | ||
UrlMatcher::$allowSchemes | protected | property | Collects URI schemes that would be allowed for the request. | ||
UrlMatcher::$expressionLanguage | protected | property | |||
UrlMatcher::$expressionLanguageProviders | protected | property | |||
UrlMatcher::$request | protected | property | |||
UrlMatcher::addExpressionLanguageProvider | public | function | |||
UrlMatcher::createRequest | protected | function | @internal | ||
UrlMatcher::getAttributes | protected | function | Returns an array of values to use as request attributes. | 1 | |
UrlMatcher::getContext | public | function | Gets the request context. | Overrides RequestContextAwareInterface::getContext | |
UrlMatcher::getExpressionLanguage | protected | function | |||
UrlMatcher::handleRouteRequirements | protected | function | Handles specific route requirements. | ||
UrlMatcher::match | public | function | Tries to match a URL path with a set of routes. | Overrides UrlMatcherInterface::match | 2 |
UrlMatcher::matchRequest | public | function | Tries to match a request with a set of routes. | Overrides RequestMatcherInterface::matchRequest | 1 |
UrlMatcher::mergeDefaults | protected | function | Get merged default parameters. | ||
UrlMatcher::REQUIREMENT_MATCH | public | constant | |||
UrlMatcher::REQUIREMENT_MISMATCH | public | constant | |||
UrlMatcher::ROUTE_MATCH | public | constant | |||
UrlMatcher::setContext | public | function | Sets the request context. | Overrides RequestContextAwareInterface::setContext | |
UrlMatcher::__construct | public | function | 2 |