Skip to content

Commit dcf75fa

Browse files
dunglasnicolas-grekas
authored andcommitted
[DependencyInjection] Add a remove() method to the PHP configurator
1 parent 2941951 commit dcf75fa

File tree

7 files changed

+59
-0
lines changed

7 files changed

+59
-0
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3
5+
---
6+
7+
* Add `ServicesConfigurator::remove()` in the PHP-DSL
8+
49
5.2.0
510
-----
611

src/Symfony/Component/DependencyInjection/Loader/Configurator/AbstractServiceConfigurator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ final public function get(string $id): ServiceConfigurator
8181
return $this->parent->get($id);
8282
}
8383

84+
/**
85+
* Removes an already defined service definition or alias.
86+
*/
87+
final public function remove(string $id): ServicesConfigurator
88+
{
89+
$this->__destruct();
90+
91+
return $this->parent->remove($id);
92+
}
93+
8494
/**
8595
* Registers a stack of decorator services.
8696
*

src/Symfony/Component/DependencyInjection/Loader/Configurator/ServiceConfigurator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class ServiceConfigurator extends AbstractServiceConfigurator
4545
private $instanceof;
4646
private $allowParent;
4747
private $path;
48+
private $destructed = false;
4849

4950
public function __construct(ContainerBuilder $container, array $instanceof, bool $allowParent, ServicesConfigurator $parent, Definition $definition, $id, array $defaultTags, string $path = null)
5051
{
@@ -58,6 +59,11 @@ public function __construct(ContainerBuilder $container, array $instanceof, bool
5859

5960
public function __destruct()
6061
{
62+
if ($this->destructed) {
63+
return;
64+
}
65+
$this->destructed = true;
66+
6167
parent::__destruct();
6268

6369
$this->container->removeBindings($this->id);

src/Symfony/Component/DependencyInjection/Loader/Configurator/ServicesConfigurator.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ final public function set(?string $id, string $class = null): ServiceConfigurato
9696
return null !== $class ? $configurator->class($class) : $configurator;
9797
}
9898

99+
/**
100+
* Removes an already defined service definition or alias.
101+
*/
102+
final public function remove(string $id): self
103+
{
104+
$this->container->removeDefinition($id);
105+
$this->container->removeAlias($id);
106+
107+
return $this;
108+
}
109+
99110
/**
100111
* Creates an alias.
101112
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
services:
3+
service_container:
4+
class: Symfony\Component\DependencyInjection\ContainerInterface
5+
public: true
6+
synthetic: true
7+
baz:
8+
class: Symfony\Component\DependencyInjection\Loader\Configurator\BazService
9+
public: true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
4+
5+
return function (ContainerConfigurator $c) {
6+
$services = $c->services()->defaults()->public();
7+
8+
$services
9+
->set('foo', FooService::class)
10+
->remove('foo')
11+
12+
->set('baz', BazService::class)
13+
->alias('baz-alias', 'baz')
14+
->remove('baz-alias')
15+
16+
->remove('bat'); // noop
17+
};

src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function provideConfig()
8080
yield ['php7'];
8181
yield ['anonymous'];
8282
yield ['lazy_fqcn'];
83+
yield ['remove'];
8384
}
8485

8586
public function testAutoConfigureAndChildDefinition()

0 commit comments

Comments
 (0)
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