Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e54d749

Browse filesBrowse files
committed
[Routing] Simplified php matcher dumper (and optimized generated matcher)
1 parent ccba363 commit e54d749
Copy full SHA for e54d749

File tree

Expand file treeCollapse file tree

7 files changed

+605
-161
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+605
-161
lines changed
+101Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
* Collection of routes.
16+
*
17+
* @author Arnaud Le Blanc <arnaud.lb@gmail.com>
18+
*/
19+
class DumperCollection implements \IteratorAggregate
20+
{
21+
private $parent;
22+
private $children = array();
23+
24+
/**
25+
* Returns the children routes and collections.
26+
*
27+
* @return array Array of DumperCollection|DumperRoute
28+
*/
29+
public function all()
30+
{
31+
return $this->children;
32+
}
33+
34+
/**
35+
* Adds a route or collection
36+
*
37+
* @param DumperRoute|DumperCollection The route or collection
38+
*/
39+
public function add($child)
40+
{
41+
if ($child instanceof DumperCollection) {
42+
$child->setParent($this);
43+
}
44+
$this->children[] = $child;
45+
}
46+
47+
/**
48+
* Sets children
49+
*
50+
* @param array $children The children
51+
*/
52+
public function setAll(array $children)
53+
{
54+
foreach ($children as $child) {
55+
if ($child instanceof DumperCollection) {
56+
$child->setParent($this);
57+
}
58+
}
59+
$this->children = $children;
60+
}
61+
62+
/**
63+
* Returns an iterator over the children.
64+
*
65+
* @return \Iterator The iterator
66+
*/
67+
public function getIterator()
68+
{
69+
return new \ArrayIterator($this->children);
70+
}
71+
72+
/**
73+
* Returns the root of the collection.
74+
*
75+
* @return DumperCollection The root collection
76+
*/
77+
public function getRoot()
78+
{
79+
return (null !== $this->parent) ? $this->parent->getRoot() : $this;
80+
}
81+
82+
/**
83+
* Returns the parent collection.
84+
*
85+
* @return DumperCollection|null The parent collection or null if the collection has no parent
86+
*/
87+
protected function getParent()
88+
{
89+
return $this->parent;
90+
}
91+
92+
/**
93+
* Sets the parent collection.
94+
*
95+
* @param DumperCollection $parent The parent collection
96+
*/
97+
protected function setParent(DumperCollection $parent)
98+
{
99+
$this->parent = $parent;
100+
}
101+
}
+103Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
class DumperPrefixCollection extends DumperCollection
20+
{
21+
private $prefix = '';
22+
23+
/**
24+
* Returns the prefix.
25+
*
26+
* @return string The prefix
27+
*/
28+
public function getPrefix()
29+
{
30+
return $this->prefix;
31+
}
32+
33+
/**
34+
* Sets the prefix.
35+
*
36+
* @param string $prefix The prefix
37+
*/
38+
public function setPrefix($prefix)
39+
{
40+
$this->prefix = $prefix;
41+
}
42+
43+
/**
44+
* Adds a route in the tree.
45+
*
46+
* @param DumperRoute $route The route
47+
*
48+
* @return DumperPrefixCollection The node the route was added to
49+
*/
50+
public function addPrefixRoute(DumperRoute $route)
51+
{
52+
$prefix = $route->getRoute()->compile()->getStaticPrefix();
53+
54+
// Same prefix, add to current leave
55+
if ($this->prefix === $prefix) {
56+
$this->add($route);
57+
58+
return $this;
59+
}
60+
61+
// Prefix starts with route's prefix
62+
if ('' === $this->prefix || 0 === strpos($prefix, $this->prefix)) {
63+
$collection = new DumperPrefixCollection();
64+
$collection->setPrefix(substr($prefix, 0, strlen($this->prefix)+1));
65+
$this->add($collection);
66+
67+
return $collection->addPrefixRoute($route);
68+
}
69+
70+
// No match, fallback to parent (recursively)
71+
72+
if (null === $parent = $this->getParent()) {
73+
throw new \LogicException("The collection root must not have a prefix");
74+
}
75+
76+
return $parent->addPrefixRoute($route);
77+
}
78+
79+
/**
80+
* Merges nodes whose prefix ends with a slash
81+
*
82+
* Children of a node whose prefix ends with a slash are moved to the parent node
83+
*/
84+
public function mergeSlashNodes()
85+
{
86+
$children = array();
87+
88+
foreach ($this as $child) {
89+
if ($child instanceof self) {
90+
$child->mergeSlashNodes();
91+
if ('/' === substr($child->prefix, -1)) {
92+
$children = array_merge($children, $child->all());
93+
} else {
94+
$children[] = $child;
95+
}
96+
} else {
97+
$children[] = $child;
98+
}
99+
}
100+
101+
$this->setAll($children);
102+
}
103+
}
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
use Symfony\Component\Routing\Route;
15+
use Symfony\Component\Routing\RouteCollection;
16+
17+
/**
18+
* Container for a Route.
19+
*
20+
* @author Arnaud Le Blanc <arnaud.lb@gmail.com>
21+
*/
22+
class DumperRoute
23+
{
24+
private $name;
25+
private $route;
26+
27+
/**
28+
* Constructor.
29+
*
30+
* @param string $name The route name
31+
* @param Route $route The route
32+
*/
33+
public function __construct($name, Route $route)
34+
{
35+
$this->name = $name;
36+
$this->route = $route;
37+
}
38+
39+
/**
40+
* Returns the route name.
41+
*
42+
* @return string The route name
43+
*/
44+
public function getName()
45+
{
46+
return $this->name;
47+
}
48+
49+
/**
50+
* Returns the route.
51+
*
52+
* @return Route The route
53+
*/
54+
public function getRoute()
55+
{
56+
return $this->route;
57+
}
58+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.