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

This commit is contained in:
Kévin Dunglas 2021-01-12 21:26:35 +01:00 committed by Nicolas Grekas
parent 294195157c
commit dcf75fab3e
7 changed files with 59 additions and 0 deletions

View File

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

View File

@ -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.
*

View File

@ -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);

View File

@ -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.
*/

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 ['anonymous'];
yield ['lazy_fqcn'];
yield ['remove'];
}
public function testAutoConfigureAndChildDefinition()