5ea622c7d49cabf96c79e3b0b1999cd61d68d03f
[yaffs-website] / vendor / symfony / routing / Matcher / Dumper / DumperPrefixCollection.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Routing\Matcher\Dumper;
13
14 /**
15  * Prefix tree of routes preserving routes order.
16  *
17  * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
18  *
19  * @internal
20  */
21 class DumperPrefixCollection extends DumperCollection
22 {
23     /**
24      * @var string
25      */
26     private $prefix = '';
27
28     /**
29      * Returns the prefix.
30      *
31      * @return string The prefix
32      */
33     public function getPrefix()
34     {
35         return $this->prefix;
36     }
37
38     /**
39      * Sets the prefix.
40      *
41      * @param string $prefix The prefix
42      */
43     public function setPrefix($prefix)
44     {
45         $this->prefix = $prefix;
46     }
47
48     /**
49      * Adds a route in the tree.
50      *
51      * @param DumperRoute $route The route
52      *
53      * @return self
54      *
55      * @throws \LogicException
56      */
57     public function addPrefixRoute(DumperRoute $route)
58     {
59         $prefix = $route->getRoute()->compile()->getStaticPrefix();
60
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);
65
66                 return $collection;
67             }
68
69             // Prefix starts with route's prefix
70             if ('' === $collection->prefix || 0 === strpos($prefix, $collection->prefix)) {
71                 $child = new self();
72                 $child->setPrefix(substr($prefix, 0, strlen($collection->prefix) + 1));
73                 $collection->add($child);
74
75                 return $child->addPrefixRoute($route);
76             }
77         }
78
79         // Reached only if the root has a non empty prefix
80         throw new \LogicException('The collection root must not have a prefix');
81     }
82
83     /**
84      * Merges nodes whose prefix ends with a slash.
85      *
86      * Children of a node whose prefix ends with a slash are moved to the parent node
87      */
88     public function mergeSlashNodes()
89     {
90         $children = array();
91
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());
97                 } else {
98                     $children[] = $child;
99                 }
100             } else {
101                 $children[] = $child;
102             }
103         }
104
105         $this->setAll($children);
106     }
107 }