From e0cf2440e9c3f5b33e44bdcf892a1597db07ceec Mon Sep 17 00:00:00 2001 From: Arnaud Le Blanc Date: Sun, 9 Feb 2014 15:57:08 +0100 Subject: [PATCH 1/2] renamed variables - making next change more readable --- .../Matcher/Dumper/DumperPrefixCollection.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/DumperPrefixCollection.php b/src/Symfony/Component/Routing/Matcher/Dumper/DumperPrefixCollection.php index 26382b0b4f..c02959fd92 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/DumperPrefixCollection.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/DumperPrefixCollection.php @@ -55,26 +55,27 @@ class DumperPrefixCollection extends DumperCollection public function addPrefixRoute(DumperRoute $route) { $prefix = $route->getRoute()->compile()->getStaticPrefix(); + $collection = $this; // Same prefix, add to current leave - if ($this->prefix === $prefix) { - $this->add($route); + if ($collection->prefix === $prefix) { + $collection->add($route); - return $this; + return $collection; } // Prefix starts with route's prefix - if ('' === $this->prefix || 0 === strpos($prefix, $this->prefix)) { - $collection = new DumperPrefixCollection(); - $collection->setPrefix(substr($prefix, 0, strlen($this->prefix)+1)); - $this->add($collection); + if ('' === $collection->prefix || 0 === strpos($prefix, $collection->prefix)) { + $child = new DumperPrefixCollection(); + $child->setPrefix(substr($prefix, 0, strlen($collection->prefix)+1)); + $collection->add($child); - return $collection->addPrefixRoute($route); + return $child->addPrefixRoute($route); } // No match, fallback to parent (recursively) - if (null === $parent = $this->getParent()) { + if (null === $parent = $collection->getParent()) { throw new \LogicException("The collection root must not have a prefix"); } From 979d5c0229137cd71d34c048da75f53d65b5ccc4 Mon Sep 17 00:00:00 2001 From: Arnaud Le Blanc Date: Fri, 7 Feb 2014 20:43:22 +0100 Subject: [PATCH 2/2] reduced recursion when building DumperPrefixCollection --- .../Matcher/Dumper/DumperPrefixCollection.php | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/DumperPrefixCollection.php b/src/Symfony/Component/Routing/Matcher/Dumper/DumperPrefixCollection.php index c02959fd92..99cd3caae6 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/DumperPrefixCollection.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/DumperPrefixCollection.php @@ -55,31 +55,28 @@ class DumperPrefixCollection extends DumperCollection public function addPrefixRoute(DumperRoute $route) { $prefix = $route->getRoute()->compile()->getStaticPrefix(); - $collection = $this; - // Same prefix, add to current leave - if ($collection->prefix === $prefix) { - $collection->add($route); + for ($collection = $this; null !== $collection; $collection = $collection->getParent()) { - return $collection; + // Same prefix, add to current leave + if ($collection->prefix === $prefix) { + $collection->add($route); + + return $collection; + } + + // Prefix starts with route's prefix + if ('' === $collection->prefix || 0 === strpos($prefix, $collection->prefix)) { + $child = new DumperPrefixCollection(); + $child->setPrefix(substr($prefix, 0, strlen($collection->prefix)+1)); + $collection->add($child); + + return $child->addPrefixRoute($route); + } } - // Prefix starts with route's prefix - if ('' === $collection->prefix || 0 === strpos($prefix, $collection->prefix)) { - $child = new DumperPrefixCollection(); - $child->setPrefix(substr($prefix, 0, strlen($collection->prefix)+1)); - $collection->add($child); - - return $child->addPrefixRoute($route); - } - - // No match, fallback to parent (recursively) - - if (null === $parent = $collection->getParent()) { - throw new \LogicException("The collection root must not have a prefix"); - } - - return $parent->addPrefixRoute($route); + // Reached only if the root has a non empty prefix + throw new \LogicException("The collection root must not have a prefix"); } /**