function StaticPrefixCollection::addRoute
Adds a route to a group.
File
-
vendor/
symfony/ routing/ Matcher/ Dumper/ StaticPrefixCollection.php, line 62
Class
- StaticPrefixCollection
- Prefix tree of routes preserving routes order.
Namespace
Symfony\Component\Routing\Matcher\DumperCode
public function addRoute(string $prefix, array|self $route) : void {
[
$prefix,
$staticPrefix,
] = $this->getCommonPrefix($prefix, $prefix);
for ($i = \count($this->items) - 1; 0 <= $i; --$i) {
$item = $this->items[$i];
[
$commonPrefix,
$commonStaticPrefix,
] = $this->getCommonPrefix($prefix, $this->prefixes[$i]);
if ($this->prefix === $commonPrefix) {
// the new route and a previous one have no common prefix, let's see if they are exclusive to each others
if ($this->prefix !== $staticPrefix && $this->prefix !== $this->staticPrefixes[$i]) {
// the new route and the previous one have exclusive static prefixes
continue;
}
if ($this->prefix === $staticPrefix && $this->prefix === $this->staticPrefixes[$i]) {
// the new route and the previous one have no static prefix
break;
}
if ($this->prefixes[$i] !== $this->staticPrefixes[$i] && $this->prefix === $this->staticPrefixes[$i]) {
// the previous route is non-static and has no static prefix
break;
}
if ($prefix !== $staticPrefix && $this->prefix === $staticPrefix) {
// the new route is non-static and has no static prefix
break;
}
continue;
}
if ($item instanceof self && $this->prefixes[$i] === $commonPrefix) {
// the new route is a child of a previous one, let's nest it
$item->addRoute($prefix, $route);
}
else {
// the new route and a previous one have a common prefix, let's merge them
$child = new self($commonPrefix);
[
$child->prefixes[0],
$child->staticPrefixes[0],
] = $child->getCommonPrefix($this->prefixes[$i], $this->prefixes[$i]);
[
$child->prefixes[1],
$child->staticPrefixes[1],
] = $child->getCommonPrefix($prefix, $prefix);
$child->items = [
$this->items[$i],
$route,
];
$this->staticPrefixes[$i] = $commonStaticPrefix;
$this->prefixes[$i] = $commonPrefix;
$this->items[$i] = $child;
}
return;
}
// No optimised case was found, in this case we simple add the route for possible
// grouping when new routes are added.
$this->staticPrefixes[] = $staticPrefix;
$this->prefixes[] = $prefix;
$this->items[] = $route;
}