Skip to content

Commit 85a28f1

Browse files
[DI] Fixes: #28326 - Overriding services autowired by name under _defaults bind not working
1 parent 5e9373b commit 85a28f1

File tree

6 files changed

+70
-0
lines changed

6 files changed

+70
-0
lines changed

src/Symfony/Component/DependencyInjection/Compiler/ResolveBindingsPass.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class ResolveBindingsPass extends AbstractRecursivePass
3434
*/
3535
public function process(ContainerBuilder $container)
3636
{
37+
$this->usedBindings = $container->getRemovedBindingIds();
38+
3739
try {
3840
parent::process($container);
3941

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
123123

124124
private $removedIds = [];
125125

126+
private $removedBindingIds = [];
127+
126128
private static $internalTypes = [
127129
'int' => true,
128130
'float' => true,
@@ -1504,6 +1506,30 @@ public function normalizeId($id)
15041506
return isset($this->definitions[$id]) || isset($this->aliasDefinitions[$id]) || isset($this->removedIds[$id]) ? $id : parent::normalizeId($id);
15051507
}
15061508

1509+
/**
1510+
* Gets removed binding ids.
1511+
*
1512+
* @return array
1513+
*
1514+
* @internal
1515+
*/
1516+
public function getRemovedBindingIds()
1517+
{
1518+
return $this->removedBindingIds;
1519+
}
1520+
1521+
/**
1522+
* Adds a removed binding id.
1523+
*
1524+
* @param int $id
1525+
*
1526+
* @internal
1527+
*/
1528+
public function addRemovedBindingId($id)
1529+
{
1530+
$this->removedBindingIds[(int) $id] = true;
1531+
}
1532+
15071533
/**
15081534
* Returns the Service Conditionals.
15091535
*

src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ public function registerClasses(Definition $prototype, $namespace, $resource, $e
9191
*/
9292
protected function setDefinition($id, Definition $definition)
9393
{
94+
if ($this->container->hasDefinition($id)) {
95+
foreach ($this->container->getDefinition($id)->getBindings() as $key => $binding) {
96+
list(, $bindingId) = $binding->getValues();
97+
$this->container->addRemovedBindingId($bindingId);
98+
}
99+
}
100+
94101
if ($this->isLoadingInstanceof) {
95102
if (!$definition instanceof ChildDefinition) {
96103
throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, \get_class($definition)));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
_defaults:
3+
bind:
4+
$quz: value
5+
$foo: [value]
6+
7+
bar:
8+
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Bar
9+
10+
foo:
11+
class: Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryDummy
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
_defaults:
3+
bind:
4+
$quz: ~
5+
6+
bar:
7+
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Bar

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Config\Resource\FileResource;
1818
use Symfony\Component\Config\Resource\GlobResource;
1919
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
20+
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
2021
use Symfony\Component\DependencyInjection\ContainerBuilder;
2122
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
2223
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
@@ -732,4 +733,20 @@ public function testBindings()
732733
'$factory' => 'factory',
733734
], array_map(function ($v) { return $v->getValues()[0]; }, $definition->getBindings()));
734735
}
736+
737+
/**
738+
* The pass may throw an exception, which will cause the test to fail.
739+
*/
740+
public function testOverriddenDefaultsBindings()
741+
{
742+
$container = new ContainerBuilder();
743+
744+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
745+
$loader->load('defaults_bindings.yml');
746+
$loader->load('defaults_bindings2.yml');
747+
748+
(new ResolveBindingsPass())->process($container);
749+
750+
$this->assertTrue(true);
751+
}
735752
}

0 commit comments

Comments
 (0)