4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Matcher;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\Routing\Exception\ExceptionInterface;
16 use Symfony\Component\Routing\Route;
17 use Symfony\Component\Routing\RouteCollection;
20 * TraceableUrlMatcher helps debug path info matching by tracing the match.
22 * @author Fabien Potencier <fabien@symfony.com>
24 class TraceableUrlMatcher extends UrlMatcher
26 const ROUTE_DOES_NOT_MATCH = 0;
27 const ROUTE_ALMOST_MATCHES = 1;
28 const ROUTE_MATCHES = 2;
32 public function getTraces($pathinfo)
34 $this->traces = array();
37 $this->match($pathinfo);
38 } catch (ExceptionInterface $e) {
44 public function getTracesForRequest(Request $request)
46 $this->request = $request;
47 $traces = $this->getTraces($request->getPathInfo());
48 $this->request = null;
53 protected function matchCollection($pathinfo, RouteCollection $routes)
55 foreach ($routes as $name => $route) {
56 $compiledRoute = $route->compile();
58 if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
59 // does it match without any requirements?
60 $r = new Route($route->getPath(), $route->getDefaults(), array(), $route->getOptions());
62 if (!preg_match($cr->getRegex(), $pathinfo)) {
63 $this->addTrace(sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
68 foreach ($route->getRequirements() as $n => $regex) {
69 $r = new Route($route->getPath(), $route->getDefaults(), array($n => $regex), $route->getOptions());
72 if (\in_array($n, $cr->getVariables()) && !preg_match($cr->getRegex(), $pathinfo)) {
73 $this->addTrace(sprintf('Requirement for "%s" does not match (%s)', $n, $regex), self::ROUTE_ALMOST_MATCHES, $name, $route);
82 // check host requirement
83 $hostMatches = array();
84 if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
85 $this->addTrace(sprintf('Host "%s" does not match the requirement ("%s")', $this->context->getHost(), $route->getHost()), self::ROUTE_ALMOST_MATCHES, $name, $route);
90 // check HTTP method requirement
91 if ($requiredMethods = $route->getMethods()) {
92 // HEAD and GET are equivalent as per RFC
93 if ('HEAD' === $method = $this->context->getMethod()) {
97 if (!\in_array($method, $requiredMethods)) {
98 $this->allow = array_merge($this->allow, $requiredMethods);
100 $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);
107 if ($condition = $route->getCondition()) {
108 if (!$this->getExpressionLanguage()->evaluate($condition, array('context' => $this->context, 'request' => $this->request ?: $this->createRequest($pathinfo)))) {
109 $this->addTrace(sprintf('Condition "%s" does not evaluate to "true"', $condition), self::ROUTE_ALMOST_MATCHES, $name, $route);
115 // check HTTP scheme requirement
116 if ($requiredSchemes = $route->getSchemes()) {
117 $scheme = $this->context->getScheme();
119 if (!$route->hasScheme($scheme)) {
120 $this->addTrace(sprintf('Scheme "%s" does not match any of the required schemes (%s); the user will be redirected to first required scheme', $scheme, implode(', ', $requiredSchemes)), self::ROUTE_ALMOST_MATCHES, $name, $route);
126 $this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
132 private function addTrace($log, $level = self::ROUTE_DOES_NOT_MATCH, $name = null, $route = null)
134 $this->traces[] = array(
138 'path' => null !== $route ? $route->getPath() : null,