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>
21 class DumperPrefixCollection extends DumperCollection
31 * @return string The prefix
33 public function getPrefix()
41 * @param string $prefix The prefix
43 public function setPrefix($prefix)
45 $this->prefix = $prefix;
49 * Adds a route in the tree.
51 * @param DumperRoute $route The route
55 * @throws \LogicException
57 public function addPrefixRoute(DumperRoute $route)
59 $prefix = $route->getRoute()->compile()->getStaticPrefix();
61 for ($collection = $this; null !== $collection; $collection = $collection->getParent()) {
62 // Same prefix, add to current leave
63 if ($collection->prefix === $prefix) {
64 $collection->add($route);
69 // Prefix starts with route's prefix
70 if ('' === $collection->prefix || 0 === strpos($prefix, $collection->prefix)) {
72 $child->setPrefix(substr($prefix, 0, strlen($collection->prefix) + 1));
73 $collection->add($child);
75 return $child->addPrefixRoute($route);
79 // Reached only if the root has a non empty prefix
80 throw new \LogicException('The collection root must not have a prefix');
84 * Merges nodes whose prefix ends with a slash.
86 * Children of a node whose prefix ends with a slash are moved to the parent node
88 public function mergeSlashNodes()
92 foreach ($this as $child) {
93 if ($child instanceof self) {
94 $child->mergeSlashNodes();
95 if ('/' === substr($child->prefix, -1)) {
96 $children = array_merge($children, $child->all());
101 $children[] = $child;
105 $this->setAll($children);