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\Dumper;
13
 
14
use Symfony\Component\Routing\RouteCollection;
15
 
16
/**
17
 * Prefix tree of routes preserving routes order.
18
 *
19
 * @author Frank de Jonge <info@frankdejonge.nl>
20
 * @author Nicolas Grekas <p@tchwork.com>
21
 *
22
 * @internal
23
 */
24
class StaticPrefixCollection
25
{
26
    private string $prefix;
27
 
28
    /**
29
     * @var string[]
30
     */
31
    private array $staticPrefixes = [];
32
 
33
    /**
34
     * @var string[]
35
     */
36
    private array $prefixes = [];
37
 
38
    /**
39
     * @var array[]|self[]
40
     */
41
    private array $items = [];
42
 
43
    public function __construct(string $prefix = '/')
44
    {
45
        $this->prefix = $prefix;
46
    }
47
 
48
    public function getPrefix(): string
49
    {
50
        return $this->prefix;
51
    }
52
 
53
    /**
54
     * @return array[]|self[]
55
     */
56
    public function getRoutes(): array
57
    {
58
        return $this->items;
59
    }
60
 
61
    /**
62
     * Adds a route to a group.
63
     */
64
    public function addRoute(string $prefix, array|StaticPrefixCollection $route)
65
    {
66
        [$prefix, $staticPrefix] = $this->getCommonPrefix($prefix, $prefix);
67
 
68
        for ($i = \count($this->items) - 1; 0 <= $i; --$i) {
69
            $item = $this->items[$i];
70
 
71
            [$commonPrefix, $commonStaticPrefix] = $this->getCommonPrefix($prefix, $this->prefixes[$i]);
72
 
73
            if ($this->prefix === $commonPrefix) {
74
                // the new route and a previous one have no common prefix, let's see if they are exclusive to each others
75
 
76
                if ($this->prefix !== $staticPrefix && $this->prefix !== $this->staticPrefixes[$i]) {
77
                    // the new route and the previous one have exclusive static prefixes
78
                    continue;
79
                }
80
 
81
                if ($this->prefix === $staticPrefix && $this->prefix === $this->staticPrefixes[$i]) {
82
                    // the new route and the previous one have no static prefix
83
                    break;
84
                }
85
 
86
                if ($this->prefixes[$i] !== $this->staticPrefixes[$i] && $this->prefix === $this->staticPrefixes[$i]) {
87
                    // the previous route is non-static and has no static prefix
88
                    break;
89
                }
90
 
91
                if ($prefix !== $staticPrefix && $this->prefix === $staticPrefix) {
92
                    // the new route is non-static and has no static prefix
93
                    break;
94
                }
95
 
96
                continue;
97
            }
98
 
99
            if ($item instanceof self && $this->prefixes[$i] === $commonPrefix) {
100
                // the new route is a child of a previous one, let's nest it
101
                $item->addRoute($prefix, $route);
102
            } else {
103
                // the new route and a previous one have a common prefix, let's merge them
104
                $child = new self($commonPrefix);
105
                [$child->prefixes[0], $child->staticPrefixes[0]] = $child->getCommonPrefix($this->prefixes[$i], $this->prefixes[$i]);
106
                [$child->prefixes[1], $child->staticPrefixes[1]] = $child->getCommonPrefix($prefix, $prefix);
107
                $child->items = [$this->items[$i], $route];
108
 
109
                $this->staticPrefixes[$i] = $commonStaticPrefix;
110
                $this->prefixes[$i] = $commonPrefix;
111
                $this->items[$i] = $child;
112
            }
113
 
114
            return;
115
        }
116
 
117
        // No optimised case was found, in this case we simple add the route for possible
118
        // grouping when new routes are added.
119
        $this->staticPrefixes[] = $staticPrefix;
120
        $this->prefixes[] = $prefix;
121
        $this->items[] = $route;
122
    }
123
 
124
    /**
125
     * Linearizes back a set of nested routes into a collection.
126
     */
127
    public function populateCollection(RouteCollection $routes): RouteCollection
128
    {
129
        foreach ($this->items as $route) {
130
            if ($route instanceof self) {
131
                $route->populateCollection($routes);
132
            } else {
133
                $routes->add(...$route);
134
            }
135
        }
136
 
137
        return $routes;
138
    }
139
 
140
    /**
141
     * Gets the full and static common prefixes between two route patterns.
142
     *
143
     * The static prefix stops at last at the first opening bracket.
144
     */
145
    private function getCommonPrefix(string $prefix, string $anotherPrefix): array
146
    {
147
        $baseLength = \strlen($this->prefix);
148
        $end = min(\strlen($prefix), \strlen($anotherPrefix));
149
        $staticLength = null;
150
        set_error_handler([__CLASS__, 'handleError']);
151
 
152
        try {
153
            for ($i = $baseLength; $i < $end && $prefix[$i] === $anotherPrefix[$i]; ++$i) {
154
                if ('(' === $prefix[$i]) {
155
                    $staticLength ??= $i;
156
                    for ($j = 1 + $i, $n = 1; $j < $end && 0 < $n; ++$j) {
157
                        if ($prefix[$j] !== $anotherPrefix[$j]) {
158
                            break 2;
159
                        }
160
                        if ('(' === $prefix[$j]) {
161
                            ++$n;
162
                        } elseif (')' === $prefix[$j]) {
163
                            --$n;
164
                        } elseif ('\\' === $prefix[$j] && (++$j === $end || $prefix[$j] !== $anotherPrefix[$j])) {
165
                            --$j;
166
                            break;
167
                        }
168
                    }
169
                    if (0 < $n) {
170
                        break;
171
                    }
172
                    if (('?' === ($prefix[$j] ?? '') || '?' === ($anotherPrefix[$j] ?? '')) && ($prefix[$j] ?? '') !== ($anotherPrefix[$j] ?? '')) {
173
                        break;
174
                    }
175
                    $subPattern = substr($prefix, $i, $j - $i);
176
                    if ($prefix !== $anotherPrefix && !preg_match('/^\(\[[^\]]++\]\+\+\)$/', $subPattern) && !preg_match('{(?<!'.$subPattern.')}', '')) {
177
                        // sub-patterns of variable length are not considered as common prefixes because their greediness would break in-order matching
178
                        break;
179
                    }
180
                    $i = $j - 1;
181
                } elseif ('\\' === $prefix[$i] && (++$i === $end || $prefix[$i] !== $anotherPrefix[$i])) {
182
                    --$i;
183
                    break;
184
                }
185
            }
186
        } finally {
187
            restore_error_handler();
188
        }
189
        if ($i < $end && 0b10 === (\ord($prefix[$i]) >> 6) && preg_match('//u', $prefix.' '.$anotherPrefix)) {
190
            do {
191
                // Prevent cutting in the middle of an UTF-8 characters
192
                --$i;
193
            } while (0b10 === (\ord($prefix[$i]) >> 6));
194
        }
195
 
196
        return [substr($prefix, 0, $i), substr($prefix, 0, $staticLength ?? $i)];
197
    }
198
 
199
    public static function handleError(int $type, string $msg)
200
    {
201
        return str_contains($msg, 'Compilation failed: lookbehind assertion is not fixed length');
202
    }
203
}