feature #39806 [DependencyInjection] Add a remove() method to the PHP configurator (dunglas)

This PR was merged into the 5.3-dev branch.

Discussion
----------

[DependencyInjection] Add a remove() method to the PHP configurator

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| License       | MIT
| Doc PR        | todo

This especially useful in the `configureContainer()` method of the kernel. It allows to not use a compiler pass to remove some services (for instance, in test mode).

Commits
-------

dcf75fab3e [DependencyInjection] Add a remove() method to the PHP configurator
This commit is contained in:
Nicolas Grekas 2021-01-19 12:08:15 +01:00
commit 49889c961a
7 changed files with 59 additions and 0 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG CHANGELOG
========= =========
5.3
---
* Add `ServicesConfigurator::remove()` in the PHP-DSL
5.2.0 5.2.0
----- -----

View File

@ -81,6 +81,16 @@ abstract class AbstractServiceConfigurator extends AbstractConfigurator
return $this->parent->get($id); return $this->parent->get($id);
} }
/**
* Removes an already defined service definition or alias.
*/
final public function remove(string $id): ServicesConfigurator
{
$this->__destruct();
return $this->parent->remove($id);
}
/** /**
* Registers a stack of decorator services. * Registers a stack of decorator services.
* *

View File

@ -45,6 +45,7 @@ class ServiceConfigurator extends AbstractServiceConfigurator
private $instanceof; private $instanceof;
private $allowParent; private $allowParent;
private $path; private $path;
private $destructed = false;
public function __construct(ContainerBuilder $container, array $instanceof, bool $allowParent, ServicesConfigurator $parent, Definition $definition, $id, array $defaultTags, string $path = null) public function __construct(ContainerBuilder $container, array $instanceof, bool $allowParent, ServicesConfigurator $parent, Definition $definition, $id, array $defaultTags, string $path = null)
{ {
@ -58,6 +59,11 @@ class ServiceConfigurator extends AbstractServiceConfigurator
public function __destruct() public function __destruct()
{ {
if ($this->destructed) {
return;
}
$this->destructed = true;
parent::__destruct(); parent::__destruct();
$this->container->removeBindings($this->id); $this->container->removeBindings($this->id);

View File

@ -96,6 +96,17 @@ class ServicesConfigurator extends AbstractConfigurator
return null !== $class ? $configurator->class($class) : $configurator; return null !== $class ? $configurator->class($class) : $configurator;
} }
/**
* Removes an already defined service definition or alias.
*/
final public function remove(string $id): self
{
$this->container->removeDefinition($id);
$this->container->removeAlias($id);
return $this;
}
/** /**
* Creates an alias. * Creates an alias.
*/ */

View File

@ -0,0 +1,9 @@
services:
service_container:
class: Symfony\Component\DependencyInjection\ContainerInterface
public: true
synthetic: true
baz:
class: Symfony\Component\DependencyInjection\Loader\Configurator\BazService
public: true

View File

@ -0,0 +1,17 @@
<?php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
return function (ContainerConfigurator $c) {
$services = $c->services()->defaults()->public();
$services
->set('foo', FooService::class)
->remove('foo')
->set('baz', BazService::class)
->alias('baz-alias', 'baz')
->remove('baz-alias')
->remove('bat'); // noop
};

View File

@ -80,6 +80,7 @@ class PhpFileLoaderTest extends TestCase
yield ['php7']; yield ['php7'];
yield ['anonymous']; yield ['anonymous'];
yield ['lazy_fqcn']; yield ['lazy_fqcn'];
yield ['remove'];
} }
public function testAutoConfigureAndChildDefinition() public function testAutoConfigureAndChildDefinition()