Skip to content

Commit efc7125

Browse files
[Routing] Fix GC control of PHP-DSL
1 parent 43738be commit efc7125

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

Loader/Configurator/CollectionConfigurator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ class CollectionConfigurator
2323
use Traits\RouteTrait;
2424

2525
private $parent;
26+
private $parentConfigurator;
2627

27-
public function __construct(RouteCollection $parent, $name)
28+
public function __construct(RouteCollection $parent, $name, self $parentConfigurator = null)
2829
{
2930
$this->parent = $parent;
3031
$this->name = $name;
3132
$this->collection = new RouteCollection();
3233
$this->route = new Route('');
34+
$this->parentConfigurator = $parentConfigurator; // for GC control
3335
}
3436

3537
public function __destruct()
@@ -50,7 +52,7 @@ final public function add($name, $path)
5052
{
5153
$this->collection->add($this->name.$name, $route = clone $this->route);
5254

53-
return new RouteConfigurator($this->collection, $route->setPath($path), $this->name);
55+
return new RouteConfigurator($this->collection, $route->setPath($path), $this->name, $this);
5456
}
5557

5658
/**
@@ -60,7 +62,7 @@ final public function add($name, $path)
6062
*/
6163
final public function collection($name = '')
6264
{
63-
return new self($this->collection, $this->name.$name);
65+
return new self($this->collection, $this->name.$name, $this);
6466
}
6567

6668
/**

Loader/Configurator/RouteConfigurator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ class RouteConfigurator
2222
use Traits\AddTrait;
2323
use Traits\RouteTrait;
2424

25-
public function __construct(RouteCollection $collection, Route $route, $name = '')
25+
private $parentConfigurator;
26+
27+
public function __construct(RouteCollection $collection, Route $route, $name = '', CollectionConfigurator $parentConfigurator = null)
2628
{
2729
$this->collection = $collection;
2830
$this->route = $route;
2931
$this->name = $name;
32+
$this->parentConfigurator = $parentConfigurator; // for GC control
3033
}
3134
}

Loader/Configurator/Traits/AddTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ trait AddTrait
3434
*/
3535
final public function add($name, $path)
3636
{
37+
$parentConfigurator = $this instanceof RouteConfigurator ? $this->parentConfigurator : null;
3738
$this->collection->add($this->name.$name, $route = new Route($path));
3839

39-
return new RouteConfigurator($this->collection, $route);
40+
return new RouteConfigurator($this->collection, $route, $parentConfigurator);
4041
}
4142

4243
/**

Tests/Fixtures/php_dsl.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
return function (RoutingConfigurator $routes) {
66
$routes
7+
->collection()
78
->add('foo', '/foo')
89
->condition('abc')
910
->options(array('utf8' => true))

0 commit comments

Comments
 (0)