[DI] Fixes: #28326 - Overriding services autowired by name under _defaults bind not working
This commit is contained in:
parent
dc8c506f00
commit
35a40ace6f
@ -34,6 +34,8 @@ class ResolveBindingsPass extends AbstractRecursivePass
|
|||||||
*/
|
*/
|
||||||
public function process(ContainerBuilder $container)
|
public function process(ContainerBuilder $container)
|
||||||
{
|
{
|
||||||
|
$this->usedBindings = $container->getRemovedBindingIds();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
parent::process($container);
|
parent::process($container);
|
||||||
|
|
||||||
|
@ -123,6 +123,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
|||||||
|
|
||||||
private $removedIds = [];
|
private $removedIds = [];
|
||||||
|
|
||||||
|
private $removedBindingIds = [];
|
||||||
|
|
||||||
private static $internalTypes = [
|
private static $internalTypes = [
|
||||||
'int' => true,
|
'int' => true,
|
||||||
'float' => true,
|
'float' => true,
|
||||||
@ -1504,6 +1506,35 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
|||||||
return isset($this->definitions[$id]) || isset($this->aliasDefinitions[$id]) || isset($this->removedIds[$id]) ? $id : parent::normalizeId($id);
|
return isset($this->definitions[$id]) || isset($this->aliasDefinitions[$id]) || isset($this->removedIds[$id]) ? $id : parent::normalizeId($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets removed binding ids.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
public function getRemovedBindingIds()
|
||||||
|
{
|
||||||
|
return $this->removedBindingIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a removed binding id.
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
public function addRemovedBindingIds($id)
|
||||||
|
{
|
||||||
|
if ($this->hasDefinition($id)) {
|
||||||
|
foreach ($this->getDefinition($id)->getBindings() as $key => $binding) {
|
||||||
|
list(, $bindingId) = $binding->getValues();
|
||||||
|
$this->removedBindingIds[(int) $bindingId] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the Service Conditionals.
|
* Returns the Service Conditionals.
|
||||||
*
|
*
|
||||||
|
@ -59,6 +59,8 @@ class ServiceConfigurator extends AbstractServiceConfigurator
|
|||||||
{
|
{
|
||||||
parent::__destruct();
|
parent::__destruct();
|
||||||
|
|
||||||
|
$this->container->addRemovedBindingIds($this->id);
|
||||||
|
|
||||||
if (!$this->definition instanceof ChildDefinition) {
|
if (!$this->definition instanceof ChildDefinition) {
|
||||||
$this->container->setDefinition($this->id, $this->definition->setInstanceofConditionals($this->instanceof));
|
$this->container->setDefinition($this->id, $this->definition->setInstanceofConditionals($this->instanceof));
|
||||||
} else {
|
} else {
|
||||||
|
@ -91,6 +91,8 @@ abstract class FileLoader extends BaseFileLoader
|
|||||||
*/
|
*/
|
||||||
protected function setDefinition($id, Definition $definition)
|
protected function setDefinition($id, Definition $definition)
|
||||||
{
|
{
|
||||||
|
$this->container->addRemovedBindingIds($id);
|
||||||
|
|
||||||
if ($this->isLoadingInstanceof) {
|
if ($this->isLoadingInstanceof) {
|
||||||
if (!$definition instanceof ChildDefinition) {
|
if (!$definition instanceof ChildDefinition) {
|
||||||
throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, \get_class($definition)));
|
throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, \get_class($definition)));
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
services:
|
||||||
|
_defaults:
|
||||||
|
bind:
|
||||||
|
$quz: value
|
||||||
|
$foo: [value]
|
||||||
|
|
||||||
|
bar:
|
||||||
|
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Bar
|
||||||
|
|
||||||
|
foo:
|
||||||
|
class: Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryDummy
|
@ -0,0 +1,7 @@
|
|||||||
|
services:
|
||||||
|
_defaults:
|
||||||
|
bind:
|
||||||
|
$quz: ~
|
||||||
|
|
||||||
|
bar:
|
||||||
|
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Bar
|
@ -17,6 +17,7 @@ use Symfony\Component\Config\Loader\LoaderResolver;
|
|||||||
use Symfony\Component\Config\Resource\FileResource;
|
use Symfony\Component\Config\Resource\FileResource;
|
||||||
use Symfony\Component\Config\Resource\GlobResource;
|
use Symfony\Component\Config\Resource\GlobResource;
|
||||||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
|
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
|
||||||
|
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
|
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
|
||||||
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
|
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
|
||||||
@ -732,4 +733,20 @@ class YamlFileLoaderTest extends TestCase
|
|||||||
'$factory' => 'factory',
|
'$factory' => 'factory',
|
||||||
], array_map(function ($v) { return $v->getValues()[0]; }, $definition->getBindings()));
|
], array_map(function ($v) { return $v->getValues()[0]; }, $definition->getBindings()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The pass may throw an exception, which will cause the test to fail.
|
||||||
|
*/
|
||||||
|
public function testOverriddenDefaultsBindings()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
|
||||||
|
$loader->load('defaults_bindings.yml');
|
||||||
|
$loader->load('defaults_bindings2.yml');
|
||||||
|
|
||||||
|
(new ResolveBindingsPass())->process($container);
|
||||||
|
|
||||||
|
$this->assertTrue(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user