Skip to content

Commit

Permalink
merged branch Tobion/collection-flat (PR symfony#6120)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Commits
-------

51223c0 added upgrade instructions
50e6259 adjusted tests
98f3ca8 [Routing] removed tree structure from RouteCollection

Discussion
----------

[Routing] removed tree structure from RouteCollection

BC break: yes (see below)
Deprecations: RouteCollection::getParent(); RouteCollection::getRoot()
tests pass: yes

The reason for this is so quite simple. The RouteCollection has been designed as a tree structure, but it cannot at all be used as one. There is no getter for a sub-collection at all. So you cannot access a sub-collection after you added it to the tree with `addCollection(new RouteCollection())`. In contrast to the form component, e.g. `$form->get('child')->get('grandchild')`.
So you can see the RouteCollection cannot be used as a tree and it should not, as the same can be achieved with a flat array!
Using a flat array removes all the need for recursive traversal and makes the code much faster, much lighter, less memory (big problem in CMS with many routes) and less error-prone.

BC break: there is only a BC break if somebody used the PHP API for defining RouteCollection and also added a Route to a collection after it has been added to another collection.
So
```
$rootCollection = new RouteCollection();
$subCollection = new RouteCollection();
$rootCollection->addCollection($subCollection);
$subCollection->add('foo', new Route('/foo'));
```
must be updated to the following (otherwise the 'foo' Route is not imported to the rootCollection)
```
$rootCollection = new RouteCollection();
$subCollection = new RouteCollection();
$subCollection->add('foo', new Route('/foo'));
$rootCollection->addCollection($subCollection);
```

Also one must call addCollection from the bottom to the top. So the correct sequence is the following (and not the reverse)
```
$childCollection->->addCollection($grandchildCollection);
$rootCollection->addCollection($childCollection);
```

Remeber, this is only needed when using PHP for defining routes and calling methods in a special order. There is no change required when using XML or YAML for definitions. Also, I'm pretty sure that neither the CMF, nor Drupal routing, nor Silex is relying on the tree stuff. So they should also still work.

cc @fabpot @Crell @dbu

One more thing: RouteCollection wasn't an appropriate name for a tree anyway as a collection of routes (that it now is) is definitely not a tree.
Yet another point: The XML declaration of routes uses the `<import>` element, which is excatly what the new implementation of addCollection without the need of a tree does. So this is now also more analogous.

---------------------------------------------------------------------------

by Koc at 2012-11-26T17:34:15Z

What benefit of this?

---------------------------------------------------------------------------

by Tobion at 2012-11-26T17:56:53Z

@Koc Why did you not simply wait for the description? ^^

---------------------------------------------------------------------------

by dbu at 2012-11-26T18:33:09Z

i love PR that remove more code than they add whithout removing functionality.

---------------------------------------------------------------------------

by Crell at 2012-11-26T18:49:52Z

There's an issue somewhere in Drupal where we're trying to use addCollection() as a shorthand for iterating over one collection and calling add() on the other for each item.  We can't do that, however, because the subcollections are not flattened properly when reading back and our current dumper can't cope with that.  So this change would not harm Drupal at all, and would mean I don't have fix a bug in our dumper. :-)  I cannot speak for any other projects, of course.

---------------------------------------------------------------------------

by Tobion at 2012-11-27T19:06:34Z

Ok, this is ready.
  • Loading branch information
fabpot committed Dec 5, 2012
2 parents e925978 + 51223c0 commit 344496f
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 266 deletions.
36 changes: 36 additions & 0 deletions UPGRADE-2.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@

* The PasswordType is now not trimmed by default.

### Routing

* RouteCollection does not behave like a tree structure anymore but as a flat
array of Routes. So when using PHP to build the RouteCollection, you must
make sure to add routes to the sub-collection before adding it to the parent
collection (this is not relevant when using YAML or XML for Route definitions).

Before:

```
$rootCollection = new RouteCollection();
$subCollection = new RouteCollection();
$rootCollection->addCollection($subCollection);
$subCollection->add('foo', new Route('/foo'));
```

After:

```
$rootCollection = new RouteCollection();
$subCollection = new RouteCollection();
$subCollection->add('foo', new Route('/foo'));
$rootCollection->addCollection($subCollection);
```

Also one must call `addCollection` from the bottom to the top hierarchy.
So the correct sequence is the following (and not the reverse):

```
$childCollection->->addCollection($grandchildCollection);
$rootCollection->addCollection($childCollection);
```

* The methods `RouteCollection::getParent()` and `RouteCollection::getRoot()`
have been deprecated and will be removed in Symfony 2.3.

### Validator

* Interfaces were created for the classes `ConstraintViolation`,
Expand Down
22 changes: 9 additions & 13 deletions src/Symfony/Bundle/FrameworkBundle/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,16 @@ public function warmUp($cacheDir)
private function resolveParameters(RouteCollection $collection)
{
foreach ($collection as $route) {
if ($route instanceof RouteCollection) {
$this->resolveParameters($route);
} else {
foreach ($route->getDefaults() as $name => $value) {
$route->setDefault($name, $this->resolve($value));
}

foreach ($route->getRequirements() as $name => $value) {
$route->setRequirement($name, $this->resolve($value));
}

$route->setPattern($this->resolve($route->getPattern()));
$route->setHostnamePattern($this->resolve($route->getHostnamePattern()));
foreach ($route->getDefaults() as $name => $value) {
$route->setDefault($name, $this->resolve($value));
}

foreach ($route->getRequirements() as $name => $value) {
$route->setRequirement($name, $this->resolve($value));
}

$route->setPattern($this->resolve($route->getPattern()));
$route->setHostnamePattern($this->resolve($route->getHostnamePattern()));
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,39 @@ CHANGELOG
2.2.0
-----

* [BC BREAK] RouteCollection does not behave like a tree structure anymore but as
a flat array of Routes. So when using PHP to build the RouteCollection, you must
make sure to add routes to the sub-collection before adding it to the parent
collection (this is not relevant when using YAML or XML for Route definitions).

Before:

```
$rootCollection = new RouteCollection();
$subCollection = new RouteCollection();
$rootCollection->addCollection($subCollection);
$subCollection->add('foo', new Route('/foo'));
```

After:

```
$rootCollection = new RouteCollection();
$subCollection = new RouteCollection();
$subCollection->add('foo', new Route('/foo'));
$rootCollection->addCollection($subCollection);
```

Also one must call `addCollection` from the bottom to the top hierarchy.
So the correct sequence is the following (and not the reverse):

```
$childCollection->->addCollection($grandchildCollection);
$rootCollection->addCollection($childCollection);
```

* The methods `RouteCollection::getParent()` and `RouteCollection::getRoot()`
have been deprecated and will be removed in Symfony 2.3.
* added support for the method default argument values when defining a @Route
* Adjacent placeholders without separator work now, e.g. `/{x}{y}{z}.{_format}`.
* Characters that function as separator between placeholders are now whitelisted
Expand Down
34 changes: 4 additions & 30 deletions src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections)
{
$fetchedHostname = false;

$routes = $this->flattenRouteCollection($routes);
$groups = $this->groupRoutesByHostnameRegex($routes);
$code = '';

Expand Down Expand Up @@ -321,31 +320,6 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
return $code;
}

/**
* Flattens a tree of routes to a single collection.
*
* @param RouteCollection $routes Collection of routes
* @param DumperCollection|null $to A DumperCollection to add routes to
*
* @return DumperCollection
*/
private function flattenRouteCollection(RouteCollection $routes, DumperCollection $to = null)
{
if (null === $to) {
$to = new DumperCollection();
}

foreach ($routes as $name => $route) {
if ($route instanceof RouteCollection) {
$this->flattenRouteCollection($route, $to);
} else {
$to->add(new DumperRoute($name, $route));
}
}

return $to;
}

/**
* Groups consecutive routes having the same hostname regex.
*
Expand All @@ -355,22 +329,22 @@ private function flattenRouteCollection(RouteCollection $routes, DumperCollectio
*
* @return DumperCollection A collection with routes grouped by hostname regex in sub-collections
*/
private function groupRoutesByHostnameRegex(DumperCollection $routes)
private function groupRoutesByHostnameRegex(RouteCollection $routes)
{
$groups = new DumperCollection();

$currentGroup = new DumperCollection();
$currentGroup->setAttribute('hostname_regex', null);
$groups->add($currentGroup);

foreach ($routes as $route) {
$hostnameRegex = $route->getRoute()->compile()->getHostnameRegex();
foreach ($routes as $name => $route) {
$hostnameRegex = $route->compile()->getHostnameRegex();
if ($currentGroup->getAttribute('hostname_regex') !== $hostnameRegex) {
$currentGroup = new DumperCollection();
$currentGroup->setAttribute('hostname_regex', $hostnameRegex);
$groups->add($currentGroup);
}
$currentGroup->add($route);
$currentGroup->add(new DumperRoute($name, $route));
}

return $groups;
Expand Down
8 changes: 0 additions & 8 deletions src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ public function getTraces($pathinfo)
protected function matchCollection($pathinfo, RouteCollection $routes)
{
foreach ($routes as $name => $route) {
if ($route instanceof RouteCollection) {
if (!$ret = $this->matchCollection($pathinfo, $route)) {
continue;
}

return true;
}

$compiledRoute = $route->compile();

if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
Expand Down
12 changes: 0 additions & 12 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,6 @@ public function match($pathinfo)
protected function matchCollection($pathinfo, RouteCollection $routes)
{
foreach ($routes as $name => $route) {
if ($route instanceof RouteCollection) {
if (false === strpos($route->getPrefix(), '{') && $route->getPrefix() !== substr($pathinfo, 0, strlen($route->getPrefix()))) {
continue;
}

if (!$ret = $this->matchCollection($pathinfo, $route)) {
continue;
}

return $ret;
}

$compiledRoute = $route->compile();

// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
Expand Down
Loading

0 comments on commit 344496f

Please sign in to comment.