Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Routing\Matcher\Dumper; Chris@0: Chris@0: /** Chris@0: * Prefix tree of routes preserving routes order. Chris@0: * Chris@0: * @author Arnaud Le Blanc Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: class DumperPrefixCollection extends DumperCollection Chris@0: { Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: private $prefix = ''; Chris@0: Chris@0: /** Chris@0: * Returns the prefix. Chris@0: * Chris@0: * @return string The prefix Chris@0: */ Chris@0: public function getPrefix() Chris@0: { Chris@0: return $this->prefix; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the prefix. Chris@0: * Chris@0: * @param string $prefix The prefix Chris@0: */ Chris@0: public function setPrefix($prefix) Chris@0: { Chris@0: $this->prefix = $prefix; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a route in the tree. Chris@0: * Chris@0: * @param DumperRoute $route The route Chris@0: * Chris@0: * @return self Chris@0: * Chris@0: * @throws \LogicException Chris@0: */ Chris@0: public function addPrefixRoute(DumperRoute $route) Chris@0: { Chris@0: $prefix = $route->getRoute()->compile()->getStaticPrefix(); Chris@0: Chris@0: for ($collection = $this; null !== $collection; $collection = $collection->getParent()) { Chris@0: // Same prefix, add to current leave Chris@0: if ($collection->prefix === $prefix) { Chris@0: $collection->add($route); Chris@0: Chris@0: return $collection; Chris@0: } Chris@0: Chris@0: // Prefix starts with route's prefix Chris@0: if ('' === $collection->prefix || 0 === strpos($prefix, $collection->prefix)) { Chris@0: $child = new self(); Chris@0: $child->setPrefix(substr($prefix, 0, strlen($collection->prefix) + 1)); Chris@0: $collection->add($child); Chris@0: Chris@0: return $child->addPrefixRoute($route); Chris@0: } Chris@0: } Chris@0: Chris@0: // Reached only if the root has a non empty prefix Chris@0: throw new \LogicException('The collection root must not have a prefix'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Merges nodes whose prefix ends with a slash. Chris@0: * Chris@0: * Children of a node whose prefix ends with a slash are moved to the parent node Chris@0: */ Chris@0: public function mergeSlashNodes() Chris@0: { Chris@0: $children = array(); Chris@0: Chris@0: foreach ($this as $child) { Chris@0: if ($child instanceof self) { Chris@0: $child->mergeSlashNodes(); Chris@0: if ('/' === substr($child->prefix, -1)) { Chris@0: $children = array_merge($children, $child->all()); Chris@0: } else { Chris@0: $children[] = $child; Chris@0: } Chris@0: } else { Chris@0: $children[] = $child; Chris@0: } Chris@0: } Chris@0: Chris@0: $this->setAll($children); Chris@0: } Chris@0: }