4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Matcher\Dumper
;
15 * Prefix tree of routes preserving routes order.
17 * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
19 class DumperPrefixCollection
extends DumperCollection
29 * @return string The prefix
31 public function getPrefix()
39 * @param string $prefix The prefix
41 public function setPrefix($prefix)
43 $this->prefix
= $prefix;
47 * Adds a route in the tree.
49 * @param DumperRoute $route The route
51 * @return DumperPrefixCollection The node the route was added to
53 * @throws \LogicException
55 public function addPrefixRoute(DumperRoute
$route)
57 $prefix = $route->getRoute()->compile()->getStaticPrefix();
59 // Same prefix, add to current leave
60 if ($this->prefix
=== $prefix) {
66 // Prefix starts with route's prefix
67 if ('' === $this->prefix
|| 0 === strpos($prefix, $this->prefix
)) {
68 $collection = new DumperPrefixCollection();
69 $collection->setPrefix(substr($prefix, 0, strlen($this->prefix
)+
1));
70 $this->add($collection);
72 return $collection->addPrefixRoute($route);
75 // No match, fallback to parent (recursively)
77 if (null === $parent = $this->getParent()) {
78 throw new \
LogicException("The collection root must not have a prefix");
81 return $parent->addPrefixRoute($route);
85 * Merges nodes whose prefix ends with a slash
87 * Children of a node whose prefix ends with a slash are moved to the parent node
89 public function mergeSlashNodes()
93 foreach ($this as $child) {
94 if ($child instanceof self
) {
95 $child->mergeSlashNodes();
96 if ('/' === substr($child->prefix
, -1)) {
97 $children = array_merge($children, $child->all());
102 $children[] = $child;
106 $this->setAll($children);