Skip to content

Commit 0bd1f75

Browse files
committed
minor symfony#28367 [DI] Forward Container::reset() to services implementing ResetInterface (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [DI] Forward Container::reset() to services implementing ResetInterface | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | not really | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Should help the garbage collector during tests. Commits ------- 5d26ba1 [DI] Forward Container::reset() to services implementing ResetInterface
2 parents 14fa58e + 5d26ba1 commit 0bd1f75

37 files changed

+24
-387
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
2121
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
2222
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
23+
use Symfony\Contracts\Service\ResetInterface;
2324

2425
/**
2526
* Container is a dependency injection container.
@@ -42,6 +43,7 @@ class Container implements ResettableContainerInterface
4243
{
4344
protected $parameterBag;
4445
protected $services = array();
46+
protected $privates = array();
4547
protected $fileMap = array();
4648
protected $methodMap = array();
4749
protected $factories = array();
@@ -301,7 +303,18 @@ public function initialized($id)
301303
*/
302304
public function reset()
303305
{
304-
$this->services = $this->factories = array();
306+
$services = $this->services + $this->privates;
307+
$this->services = $this->factories = $this->privates = array();
308+
309+
foreach ($services as $service) {
310+
try {
311+
if ($service instanceof ResetInterface) {
312+
$service->reset();
313+
}
314+
} catch (\Throwable $e) {
315+
continue;
316+
}
317+
}
305318
}
306319

307320
/**

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -960,11 +960,6 @@ class $class extends $baseClass
960960
private \$parameters;
961961
private \$targetDirs = array();
962962
963-
/*{$this->docStar}
964-
* @internal but protected for BC on cache:clear
965-
*/
966-
protected \$privates = array();
967-
968963
public function __construct()
969964
{
970965
@@ -1011,12 +1006,6 @@ public function __construct()
10111006
$code .= <<<EOF
10121007
}
10131008
1014-
public function reset()
1015-
{
1016-
\$this->privates = array();
1017-
parent::reset();
1018-
}
1019-
10201009
public function compile()
10211010
{
10221011
throw new LogicException('You cannot compile a dumped container that was already compiled.');

src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\ContainerInterface;
1818
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
1919
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
20+
use Symfony\Contracts\Service\ResetInterface;
2021

2122
class ContainerTest extends TestCase
2223
{
@@ -317,11 +318,19 @@ public function testInitializedWithPrivateService()
317318
public function testReset()
318319
{
319320
$c = new Container();
320-
$c->set('bar', new \stdClass());
321+
$c->set('bar', $bar = new class() implements ResetInterface {
322+
public $resetCounter = 0;
323+
324+
public function reset()
325+
{
326+
++$this->resetCounter;
327+
}
328+
});
321329

322330
$c->reset();
323331

324332
$this->assertNull($c->get('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE));
333+
$this->assertSame(1, $bar->resetCounter);
325334
}
326335

327336
/**

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_constructor_without_arguments.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tes
2121
private $parameters;
2222
private $targetDirs = array();
2323

24-
/**
25-
* @internal but protected for BC on cache:clear
26-
*/
27-
protected $privates = array();
28-
2924
public function __construct()
3025
{
3126
parent::__construct();
@@ -36,12 +31,6 @@ public function __construct()
3631
$this->aliases = array();
3732
}
3833

39-
public function reset()
40-
{
41-
$this->privates = array();
42-
parent::reset();
43-
}
44-
4534
public function compile()
4635
{
4736
throw new LogicException('You cannot compile a dumped container that was already compiled.');

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_mandatory_constructor_arguments.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,13 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tes
2121
private $parameters;
2222
private $targetDirs = array();
2323

24-
/**
25-
* @internal but protected for BC on cache:clear
26-
*/
27-
protected $privates = array();
28-
2924
public function __construct()
3025
{
3126
$this->services = $this->privates = array();
3227

3328
$this->aliases = array();
3429
}
3530

36-
public function reset()
37-
{
38-
$this->privates = array();
39-
parent::reset();
40-
}
41-
4231
public function compile()
4332
{
4433
throw new LogicException('You cannot compile a dumped container that was already compiled.');

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_optional_constructor_arguments.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tes
2121
private $parameters;
2222
private $targetDirs = array();
2323

24-
/**
25-
* @internal but protected for BC on cache:clear
26-
*/
27-
protected $privates = array();
28-
2924
public function __construct()
3025
{
3126
parent::__construct();
@@ -36,12 +31,6 @@ public function __construct()
3631
$this->aliases = array();
3732
}
3833

39-
public function reset()
40-
{
41-
$this->privates = array();
42-
parent::reset();
43-
}
44-
4534
public function compile()
4635
{
4736
throw new LogicException('You cannot compile a dumped container that was already compiled.');

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_without_constructor.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,13 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tes
2121
private $parameters;
2222
private $targetDirs = array();
2323

24-
/**
25-
* @internal but protected for BC on cache:clear
26-
*/
27-
protected $privates = array();
28-
2924
public function __construct()
3025
{
3126
$this->services = $this->privates = array();
3227

3328
$this->aliases = array();
3429
}
3530

36-
public function reset()
37-
{
38-
$this->privates = array();
39-
parent::reset();
40-
}
41-
4231
public function compile()
4332
{
4433
throw new LogicException('You cannot compile a dumped container that was already compiled.');

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,13 @@ class Container extends \Symfony\Component\DependencyInjection\Dump\AbstractCont
2121
private $parameters;
2222
private $targetDirs = array();
2323

24-
/**
25-
* @internal but protected for BC on cache:clear
26-
*/
27-
protected $privates = array();
28-
2924
public function __construct()
3025
{
3126
$this->services = $this->privates = array();
3227

3328
$this->aliases = array();
3429
}
3530

36-
public function reset()
37-
{
38-
$this->privates = array();
39-
parent::reset();
40-
}
41-
4231
public function compile()
4332
{
4433
throw new LogicException('You cannot compile a dumped container that was already compiled.');

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,13 @@ class ProjectServiceContainer extends Container
1919
private $parameters;
2020
private $targetDirs = array();
2121

22-
/**
23-
* @internal but protected for BC on cache:clear
24-
*/
25-
protected $privates = array();
26-
2722
public function __construct()
2823
{
2924
$this->services = $this->privates = array();
3025

3126
$this->aliases = array();
3227
}
3328

34-
public function reset()
35-
{
36-
$this->privates = array();
37-
parent::reset();
38-
}
39-
4029
public function compile()
4130
{
4231
throw new LogicException('You cannot compile a dumped container that was already compiled.');

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
1919
private $parameters;
2020
private $targetDirs = array();
2121

22-
/**
23-
* @internal but protected for BC on cache:clear
24-
*/
25-
protected $privates = array();
26-
2722
public function __construct()
2823
{
2924
$this->parameters = $this->getDefaultParameters();
@@ -36,12 +31,6 @@ public function __construct()
3631
$this->aliases = array();
3732
}
3833

39-
public function reset()
40-
{
41-
$this->privates = array();
42-
parent::reset();
43-
}
44-
4534
public function compile()
4635
{
4736
throw new LogicException('You cannot compile a dumped container that was already compiled.');

0 commit comments

Comments
 (0)