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