Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
    public const ROUTE_DOES_NOT_MATCH = 0;
27
    public const ROUTE_ALMOST_MATCHES = 1;
28
    public 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) {
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): array
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 && !str_starts_with($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\x80-\xFF]+\}/?$#', $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
            $attributes = $this->getAttributes($route, $name, array_replace($matches, $hostMatches));
119
 
120
            $status = $this->handleRouteRequirements($pathinfo, $name, $route, $attributes);
121
 
122
            if (self::REQUIREMENT_MISMATCH === $status[0]) {
123
                $this->addTrace(sprintf('Condition "%s" does not evaluate to "true"', $route->getCondition()), self::ROUTE_ALMOST_MATCHES, $name, $route);
124
                continue;
125
            }
126
 
127
            if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
128
                if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods))) {
129
                    $this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
130
 
131
                    return $this->allow = $this->allowSchemes = [];
132
                }
133
                $this->addTrace(sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
134
                continue;
135
            }
136
 
137
            if ($route->getSchemes() && !$route->hasScheme($this->context->getScheme())) {
138
                $this->allowSchemes = array_merge($this->allowSchemes, $route->getSchemes());
139
                $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);
140
                continue;
141
            }
142
 
143
            if ($requiredMethods && !\in_array($method, $requiredMethods)) {
144
                $this->allow = array_merge($this->allow, $requiredMethods);
145
                $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);
146
                continue;
147
            }
148
 
149
            $this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
150
 
151
            return array_replace($attributes, $status[1] ?? []);
152
        }
153
 
154
        return [];
155
    }
156
 
157
    private function addTrace(string $log, int $level = self::ROUTE_DOES_NOT_MATCH, string $name = null, Route $route = null)
158
    {
159
        $this->traces[] = [
160
            'log' => $log,
161
            'name' => $name,
162
            'level' => $level,
163
            'path' => $route?->getPath(),
164
        ];
165
    }
166
}