diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index 4da43e0a6a309..05681ad54e27d 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 68b3cb5e94689..f4f61ab46a8b2 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/Configurator/AbstractServiceConfigurator.php +++ b/src/Symfony/Component/DependencyInjection/Loader/Configurator/AbstractServiceConfigurator.php @@ -81,6 +81,16 @@ final public function get(string $id): ServiceConfigurator 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 05b55d95e8a54..12d08420ef93d 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 @@ public function __construct(ContainerBuilder $container, array $instanceof, bool 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 38d002a486577..d5cdf6a31490d 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/Configurator/ServicesConfigurator.php +++ b/src/Symfony/Component/DependencyInjection/Loader/Configurator/ServicesConfigurator.php @@ -96,6 +96,17 @@ final public function set(?string $id, string $class = null): ServiceConfigurato 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 0000000000000..176c61d5b80cb --- /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 0000000000000..3781be5a2045c --- /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 f3fa913761759..d241fcf023d58 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php @@ -80,6 +80,7 @@ public function provideConfig() yield ['php7']; yield ['anonymous']; yield ['lazy_fqcn']; + yield ['remove']; } public function testAutoConfigureAndChildDefinition() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy