minor #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
-------

5d26ba1fd6 [DI] Forward Container::reset() to services implementing ResetInterface
This commit is contained in:
Fabien Potencier 2018-09-05 10:58:51 +02:00
commit 0bd1f7538e
37 changed files with 24 additions and 387 deletions

View File

@ -20,6 +20,7 @@ use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Contracts\Service\ResetInterface;
/**
* Container is a dependency injection container.
@ -42,6 +43,7 @@ class Container implements ResettableContainerInterface
{
protected $parameterBag;
protected $services = array();
protected $privates = array();
protected $fileMap = array();
protected $methodMap = array();
protected $factories = array();
@ -301,7 +303,18 @@ class Container implements ResettableContainerInterface
*/
public function reset()
{
$this->services = $this->factories = array();
$services = $this->services + $this->privates;
$this->services = $this->factories = $this->privates = array();
foreach ($services as $service) {
try {
if ($service instanceof ResetInterface) {
$service->reset();
}
} catch (\Throwable $e) {
continue;
}
}
}
/**

View File

@ -960,11 +960,6 @@ class $class extends $baseClass
private \$parameters;
private \$targetDirs = array();
/*{$this->docStar}
* @internal but protected for BC on cache:clear
*/
protected \$privates = array();
public function __construct()
{
@ -1011,12 +1006,6 @@ EOF;
$code .= <<<EOF
}
public function reset()
{
\$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -17,6 +17,7 @@ use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Contracts\Service\ResetInterface;
class ContainerTest extends TestCase
{
@ -317,11 +318,19 @@ class ContainerTest extends TestCase
public function testReset()
{
$c = new Container();
$c->set('bar', new \stdClass());
$c->set('bar', $bar = new class() implements ResetInterface {
public $resetCounter = 0;
public function reset()
{
++$this->resetCounter;
}
});
$c->reset();
$this->assertNull($c->get('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE));
$this->assertSame(1, $bar->resetCounter);
}
/**

View File

@ -21,11 +21,6 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tes
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
parent::__construct();
@ -36,12 +31,6 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tes
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -21,11 +21,6 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tes
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -33,12 +28,6 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tes
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -21,11 +21,6 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tes
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
parent::__construct();
@ -36,12 +31,6 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tes
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -21,11 +21,6 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tes
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -33,12 +28,6 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tes
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -21,11 +21,6 @@ class Container extends \Symfony\Component\DependencyInjection\Dump\AbstractCont
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -33,12 +28,6 @@ class Container extends \Symfony\Component\DependencyInjection\Dump\AbstractCont
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -31,12 +26,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->parameters = $this->getDefaultParameters();
@ -36,12 +31,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$dir = __DIR__;
@ -40,12 +35,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -34,12 +29,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -35,12 +30,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -34,12 +29,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class Symfony_DI_PhpDumper_Test_EnvParameters extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$dir = __DIR__;
@ -41,12 +36,6 @@ class Symfony_DI_PhpDumper_Test_EnvParameters extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -35,12 +30,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->parameters = $this->getDefaultParameters();
@ -33,12 +28,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -377,11 +377,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct(array $buildParameters = array(), $containerDir = __DIR__)
{
$dir = $this->targetDirs[0] = \dirname($containerDir);
@ -432,12 +427,6 @@ class ProjectServiceContainer extends Container
);
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->parameters = $this->getDefaultParameters();
@ -64,12 +59,6 @@ class ProjectServiceContainer extends Container
);
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class Symfony_DI_PhpDumper_Test_Almost_Circular_Private extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -45,12 +40,6 @@ class Symfony_DI_PhpDumper_Test_Almost_Circular_Private extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class Symfony_DI_PhpDumper_Test_Almost_Circular_Public extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -52,12 +47,6 @@ class Symfony_DI_PhpDumper_Test_Almost_Circular_Public extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$dir = __DIR__;
@ -40,12 +35,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class Symfony_DI_PhpDumper_Test_Base64Parameters extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->parameters = $this->getDefaultParameters();
@ -33,12 +28,6 @@ class Symfony_DI_PhpDumper_Test_Base64Parameters extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class Symfony_DI_PhpDumper_Test_CsvParameters extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->parameters = $this->getDefaultParameters();
@ -33,12 +28,6 @@ class Symfony_DI_PhpDumper_Test_CsvParameters extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -35,12 +30,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->parameters = $this->getDefaultParameters();
@ -37,12 +32,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class Symfony_DI_PhpDumper_Errored_Definition extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->parameters = $this->getDefaultParameters();
@ -64,12 +59,6 @@ class Symfony_DI_PhpDumper_Errored_Definition extends Container
);
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$dir = __DIR__;
@ -49,12 +44,6 @@ class ProjectServiceContainer extends Container
};
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class Symfony_DI_PhpDumper_Test_JsonParameters extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->parameters = $this->getDefaultParameters();
@ -33,12 +28,6 @@ class Symfony_DI_PhpDumper_Test_JsonParameters extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -41,12 +36,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -34,12 +29,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -35,12 +30,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -34,12 +29,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class Symfony_DI_PhpDumper_Test_Rot13Parameters extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->parameters = $this->getDefaultParameters();
@ -37,12 +32,6 @@ class Symfony_DI_PhpDumper_Test_Rot13Parameters extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class Symfony_DI_PhpDumper_Service_Locator_Argument extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -38,12 +33,6 @@ class Symfony_DI_PhpDumper_Service_Locator_Argument extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class ProjectServiceContainer extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -35,12 +30,6 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');

View File

@ -19,11 +19,6 @@ class Symfony_DI_PhpDumper_Test_Uninitialized_Reference extends Container
private $parameters;
private $targetDirs = array();
/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();
public function __construct()
{
$this->services = $this->privates = array();
@ -36,12 +31,6 @@ class Symfony_DI_PhpDumper_Test_Uninitialized_Reference extends Container
$this->aliases = array();
}
public function reset()
{
$this->privates = array();
parent::reset();
}
public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');