Skip to content

Commit fa6eebc

Browse files
committed
reduced recursion when building DumperPrefixCollection
1 parent 9278b27 commit fa6eebc

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

src/Symfony/Component/Routing/Matcher/Dumper/DumperPrefixCollection.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,28 @@ public function setPrefix($prefix)
5555
public function addPrefixRoute(DumperRoute $route)
5656
{
5757
$prefix = $route->getRoute()->compile()->getStaticPrefix();
58-
$collection = $this;
5958

60-
// Same prefix, add to current leave
61-
if ($collection->prefix === $prefix) {
62-
$collection->add($route);
59+
for ($collection = $this; null !== $collection; $collection = $collection->getParent()) {
6360

64-
return $collection;
65-
}
66-
67-
// Prefix starts with route's prefix
68-
if ('' === $collection->prefix || 0 === strpos($prefix, $collection->prefix)) {
69-
$child = new DumperPrefixCollection();
70-
$child->setPrefix(substr($prefix, 0, strlen($collection->prefix)+1));
71-
$collection->add($child);
61+
// Same prefix, add to current leave
62+
if ($collection->prefix === $prefix) {
63+
$collection->add($route);
7264

73-
return $child->addPrefixRoute($route);
74-
}
65+
return $collection;
66+
}
7567

76-
// No match, fallback to parent (recursively)
68+
// Prefix starts with route's prefix
69+
if ('' === $collection->prefix || 0 === strpos($prefix, $collection->prefix)) {
70+
$child = new DumperPrefixCollection();
71+
$child->setPrefix(substr($prefix, 0, strlen($collection->prefix)+1));
72+
$collection->add($child);
7773

78-
if (null === $parent = $collection->getParent()) {
79-
throw new \LogicException("The collection root must not have a prefix");
74+
return $child->addPrefixRoute($route);
75+
}
8076
}
8177

82-
return $parent->addPrefixRoute($route);
78+
// Reached only if the root has a non empty prefix
79+
throw new \LogicException("The collection root must not have a prefix");
8380
}
8481

8582
/**

0 commit comments

Comments
 (0)