Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.3
---

* Add `ServicesConfigurator::remove()` in the PHP-DSL

5.2.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -58,6 +59,11 @@ public function __construct(ContainerBuilder $container, array $instanceof, bool

public function __destruct()
{
if ($this->destructed) {
Copy link
Member Author

@dunglas dunglas Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This trick prevents the destructor to re-add a service when it has been removed just after being added (see tests).

return;
}
$this->destructed = true;

parent::__destruct();

$this->container->removeBindings($this->id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
};
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function provideConfig()
yield ['php7'];
yield ['anonymous'];
yield ['lazy_fqcn'];
yield ['remove'];
}

public function testAutoConfigureAndChildDefinition()
Expand Down