bug #26318 [Routing] Fix GC control of PHP-DSL (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[Routing] Fix GC control of PHP-DSL

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Adding fluently in a collection is broken currently.

Commits
-------

239f2e2 [Routing] Fix GC control of PHP-DSL
This commit is contained in:
Nicolas Grekas 2018-02-26 16:49:59 +01:00
commit 119291883f
4 changed files with 12 additions and 5 deletions

View File

@ -23,13 +23,15 @@ class CollectionConfigurator
use Traits\RouteTrait;
private $parent;
private $parentConfigurator;
public function __construct(RouteCollection $parent, $name)
public function __construct(RouteCollection $parent, $name, self $parentConfigurator = null)
{
$this->parent = $parent;
$this->name = $name;
$this->collection = new RouteCollection();
$this->route = new Route('');
$this->parentConfigurator = $parentConfigurator; // for GC control
}
public function __destruct()
@ -50,7 +52,7 @@ class CollectionConfigurator
{
$this->collection->add($this->name.$name, $route = clone $this->route);
return new RouteConfigurator($this->collection, $route->setPath($path), $this->name);
return new RouteConfigurator($this->collection, $route->setPath($path), $this->name, $this);
}
/**
@ -60,7 +62,7 @@ class CollectionConfigurator
*/
final public function collection($name = '')
{
return new self($this->collection, $this->name.$name);
return new self($this->collection, $this->name.$name, $this);
}
/**

View File

@ -22,10 +22,13 @@ class RouteConfigurator
use Traits\AddTrait;
use Traits\RouteTrait;
public function __construct(RouteCollection $collection, Route $route, $name = '')
private $parentConfigurator;
public function __construct(RouteCollection $collection, Route $route, $name = '', CollectionConfigurator $parentConfigurator = null)
{
$this->collection = $collection;
$this->route = $route;
$this->name = $name;
$this->parentConfigurator = $parentConfigurator; // for GC control
}
}

View File

@ -34,9 +34,10 @@ trait AddTrait
*/
final public function add($name, $path)
{
$parentConfigurator = $this instanceof RouteConfigurator ? $this->parentConfigurator : null;
$this->collection->add($this->name.$name, $route = new Route($path));
return new RouteConfigurator($this->collection, $route);
return new RouteConfigurator($this->collection, $route, $parentConfigurator);
}
/**

View File

@ -4,6 +4,7 @@ namespace Symfony\Component\Routing\Loader\Configurator;
return function (RoutingConfigurator $routes) {
$routes
->collection()
->add('foo', '/foo')
->condition('abc')
->options(array('utf8' => true))