Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Routing\Matcher;
13
 
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;
18
 
19
/**
20
 * TraceableUrlMatcher helps debug path info matching by tracing the match.
21
 *
22
 * @author Fabien Potencier <fabien@symfony.com>
23
 */
24
class TraceableUrlMatcher extends UrlMatcher
25
{
26
    const ROUTE_DOES_NOT_MATCH = 0;
27
    const ROUTE_ALMOST_MATCHES = 1;
28
    const ROUTE_MATCHES = 2;
29
 
30
    protected $traces;
31
 
32
    public function getTraces(string $pathinfo)
33
    {
34
        $this->traces = [];
35
 
36
        try {
37
            $this->match($pathinfo);
38
        } catch (ExceptionInterface $e) {
39
        }
40
 
41
        return $this->traces;
42
    }
43
 
44
    public function getTracesForRequest(Request $request)
45
    {
46
        $this->request = $request;
47
        $traces = $this->getTraces($request->getPathInfo());
48
        $this->request = null;
49
 
50
        return $traces;
51
    }
52
 
53
    protected function matchCollection(string $pathinfo, RouteCollection $routes)
54
    {
55
        // HEAD and GET are equivalent as per RFC
56
        if ('HEAD' === $method = $this->context->getMethod()) {
57
            $method = 'GET';
58
        }
59
        $supportsTrailingSlash = 'GET' === $method && $this instanceof RedirectableUrlMatcherInterface;
60
        $trimmedPathinfo = rtrim($pathinfo, '/') ?: '/';
61
 
62
        foreach ($routes as $name => $route) {
63
            $compiledRoute = $route->compile();
64
            $staticPrefix = rtrim($compiledRoute->getStaticPrefix(), '/');
65
            $requiredMethods = $route->getMethods();
66
 
67
            // check the static prefix of the URL first. Only use the more expensive preg_match when it matches
68
            if ('' !== $staticPrefix && 0 !== strpos($trimmedPathinfo, $staticPrefix)) {
69
                $this->addTrace(sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
70
                continue;
71
            }
72
            $regex = $compiledRoute->getRegex();
73
 
74
            $pos = strrpos($regex, '$');
75
            $hasTrailingSlash = '/' === $regex[$pos - 1];
76
            $regex = substr_replace($regex, '/?$', $pos - $hasTrailingSlash, 1 + $hasTrailingSlash);
77
 
78
            if (!preg_match($regex, $pathinfo, $matches)) {
79
                // does it match without any requirements?
80
                $r = new Route($route->getPath(), $route->getDefaults(), [], $route->getOptions());
81
                $cr = $r->compile();
82
                if (!preg_match($cr->getRegex(), $pathinfo)) {
83
                    $this->addTrace(sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
84
 
85
                    continue;
86
                }
87
 
88
                foreach ($route->getRequirements() as $n => $regex) {
89
                    $r = new Route($route->getPath(), $route->getDefaults(), [$n => $regex], $route->getOptions());
90
                    $cr = $r->compile();
91
 
92
                    if (\in_array($n, $cr->getVariables()) && !preg_match($cr->getRegex(), $pathinfo)) {
93
                        $this->addTrace(sprintf('Requirement for "%s" does not match (%s)', $n, $regex), self::ROUTE_ALMOST_MATCHES, $name, $route);
94
 
95
                        continue 2;
96
                    }
97
                }
98
 
99
                continue;
100
            }
101
 
102
            $hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\{\w+\}/?$#', $route->getPath());
103
 
104
            if ($hasTrailingVar && ($hasTrailingSlash || (null === $m = $matches[\count($compiledRoute->getPathVariables())] ?? null) || '/' !== ($m[-1] ?? '/')) && preg_match($regex, $trimmedPathinfo, $m)) {
105
                if ($hasTrailingSlash) {
106
                    $matches = $m;
107
                } else {
108
                    $hasTrailingVar = false;
109
                }
110
            }
111
 
112
            $hostMatches = [];
113
            if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
114
                $this->addTrace(sprintf('Host "%s" does not match the requirement ("%s")', $this->context->getHost(), $route->getHost()), self::ROUTE_ALMOST_MATCHES, $name, $route);
115
                continue;
116
            }
117
 
118
            $status = $this->handleRouteRequirements($pathinfo, $name, $route);
119
 
120
            if (self::REQUIREMENT_MISMATCH === $status[0]) {
121
                $this->addTrace(sprintf('Condition "%s" does not evaluate to "true"', $route->getCondition()), self::ROUTE_ALMOST_MATCHES, $name, $route);
122
                continue;
123
            }
124
 
125
            if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
126
                if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods))) {
127
                    $this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
128
 
129
                    return $this->allow = $this->allowSchemes = [];
130
                }
131
                $this->addTrace(sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
132
                continue;
133
            }
134
 
135
            if ($route->getSchemes() && !$route->hasScheme($this->context->getScheme())) {
136
                $this->allowSchemes = array_merge($this->allowSchemes, $route->getSchemes());
137
                $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);
138
                continue;
139
            }
140
 
141
            if ($requiredMethods && !\in_array($method, $requiredMethods)) {
142
                $this->allow = array_merge($this->allow, $requiredMethods);
143
                $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);
144
                continue;
145
            }
146
 
147
            $this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
148
 
149
            return $this->getAttributes($route, $name, array_replace($matches, $hostMatches, isset($status[1]) ? $status[1] : []));
150
        }
151
 
152
        return [];
153
    }
154
 
155
    private function addTrace(string $log, int $level = self::ROUTE_DOES_NOT_MATCH, string $name = null, Route $route = null)
156
    {
157
        $this->traces[] = [
158
            'log' => $log,
159
            'name' => $name,
160
            'level' => $level,
161
            'path' => null !== $route ? $route->getPath() : null,
162
        ];
163
    }
164
}