aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/symfony/routing/Symfony/Component/Routing/Matcher/Dumper/DumperPrefixCollection.php
blob: 26382b0b4fd2ece311f3b2d7112d72e07ffe5f2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Routing\Matcher\Dumper;

/**
 * Prefix tree of routes preserving routes order.
 *
 * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
 */
class DumperPrefixCollection extends DumperCollection
{
    /**
     * @var string
     */
    private $prefix = '';

    /**
     * Returns the prefix.
     *
     * @return string The prefix
     */
    public function getPrefix()
    {
        return $this->prefix;
    }

    /**
     * Sets the prefix.
     *
     * @param string $prefix The prefix
     */
    public function setPrefix($prefix)
    {
        $this->prefix = $prefix;
    }

    /**
     * Adds a route in the tree.
     *
     * @param DumperRoute $route The route
     *
     * @return DumperPrefixCollection The node the route was added to
     *
     * @throws \LogicException
     */
    public function addPrefixRoute(DumperRoute $route)
    {
        $prefix = $route->getRoute()->compile()->getStaticPrefix();

        // Same prefix, add to current leave
        if ($this->prefix === $prefix) {
            $this->add($route);

            return $this;
        }

        // Prefix starts with route's prefix
        if ('' === $this->prefix || 0 === strpos($prefix, $this->prefix)) {
            $collection = new DumperPrefixCollection();
            $collection->setPrefix(substr($prefix, 0, strlen($this->prefix)+1));
            $this->add($collection);

            return $collection->addPrefixRoute($route);
        }

        // No match, fallback to parent (recursively)

        if (null === $parent = $this->getParent()) {
            throw new \LogicException("The collection root must not have a prefix");
        }

        return $parent->addPrefixRoute($route);
    }

    /**
     * Merges nodes whose prefix ends with a slash
     *
     * Children of a node whose prefix ends with a slash are moved to the parent node
     */
    public function mergeSlashNodes()
    {
        $children = array();

        foreach ($this as $child) {
            if ($child instanceof self) {
                $child->mergeSlashNodes();
                if ('/' === substr($child->prefix, -1)) {
                    $children = array_merge($children, $child->all());
                } else {
                    $children[] = $child;
                }
            } else {
                $children[] = $child;
            }
        }

        $this->setAll($children);
    }
}