diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index 4da43e0a6a..05681ad54e 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +5.3 +--- + + * Add `ServicesConfigurator::remove()` in the PHP-DSL + 5.2.0 ----- diff --git a/src/Symfony/Component/DependencyInjection/Loader/Configurator/AbstractServiceConfigurator.php b/src/Symfony/Component/DependencyInjection/Loader/Configurator/AbstractServiceConfigurator.php index 68b3cb5e94..f4f61ab46a 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/Configurator/AbstractServiceConfigurator.php +++ b/src/Symfony/Component/DependencyInjection/Loader/Configurator/AbstractServiceConfigurator.php @@ -81,6 +81,16 @@ abstract class AbstractServiceConfigurator extends AbstractConfigurator 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. * diff --git a/src/Symfony/Component/DependencyInjection/Loader/Configurator/ServiceConfigurator.php b/src/Symfony/Component/DependencyInjection/Loader/Configurator/ServiceConfigurator.php index 05b55d95e8..12d08420ef 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/Configurator/ServiceConfigurator.php +++ b/src/Symfony/Component/DependencyInjection/Loader/Configurator/ServiceConfigurator.php @@ -45,6 +45,7 @@ class ServiceConfigurator extends AbstractServiceConfigurator private $instanceof; private $allowParent; private $path; + private $destructed = false; 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() { + if ($this->destructed) { + return; + } + $this->destructed = true; + parent::__destruct(); $this->container->removeBindings($this->id); diff --git a/src/Symfony/Component/DependencyInjection/Loader/Configurator/ServicesConfigurator.php b/src/Symfony/Component/DependencyInjection/Loader/Configurator/ServicesConfigurator.php index 38d002a486..d5cdf6a314 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/Configurator/ServicesConfigurator.php +++ b/src/Symfony/Component/DependencyInjection/Loader/Configurator/ServicesConfigurator.php @@ -96,6 +96,17 @@ class ServicesConfigurator extends AbstractConfigurator 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. */ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/remove.expected.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/remove.expected.yml new file mode 100644 index 0000000000..176c61d5b8 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/remove.expected.yml @@ -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 diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/remove.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/remove.php new file mode 100644 index 0000000000..3781be5a20 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/remove.php @@ -0,0 +1,17 @@ +services()->defaults()->public(); + + $services + ->set('foo', FooService::class) + ->remove('foo') + + ->set('baz', BazService::class) + ->alias('baz-alias', 'baz') + ->remove('baz-alias') + + ->remove('bat'); // noop +}; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php index f3fa913761..d241fcf023 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php @@ -80,6 +80,7 @@ class PhpFileLoaderTest extends TestCase yield ['php7']; yield ['anonymous']; yield ['lazy_fqcn']; + yield ['remove']; } public function testAutoConfigureAndChildDefinition()