From 02d1febf5a7f1a946428b247d44143b7d756e7b6 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 18 Jan 2022 16:56:38 +0100 Subject: [PATCH 1/2] [DependencyInjection] remove arbitratry limitation to exclude inline services from bindings --- Loader/Configurator/Traits/BindTrait.php | 5 ----- Tests/Fixtures/Prototype/Foo.php | 2 +- Tests/Fixtures/config/defaults.expected.yml | 4 ++-- Tests/Fixtures/config/defaults.php | 1 + 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Loader/Configurator/Traits/BindTrait.php b/Loader/Configurator/Traits/BindTrait.php index 573b6f53a..3021e0708 100644 --- a/Loader/Configurator/Traits/BindTrait.php +++ b/Loader/Configurator/Traits/BindTrait.php @@ -12,10 +12,8 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits; use Symfony\Component\DependencyInjection\Argument\BoundArgument; -use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\DependencyInjection\Loader\Configurator\DefaultsConfigurator; use Symfony\Component\DependencyInjection\Loader\Configurator\InstanceofConfigurator; -use Symfony\Component\DependencyInjection\Reference; trait BindTrait { @@ -34,9 +32,6 @@ trait BindTrait final public function bind(string $nameOrFqcn, $valueOrRef): self { $valueOrRef = static::processValue($valueOrRef, true); - if (!preg_match('/^(?:(?:array|bool|float|int|string|iterable)[ \t]*+)?\$/', $nameOrFqcn) && !$valueOrRef instanceof Reference) { - throw new InvalidArgumentException(sprintf('Invalid binding for service "%s": named arguments must start with a "$", and FQCN must map to references. Neither applies to binding "%s".', $this->id, $nameOrFqcn)); - } $bindings = $this->definition->getBindings(); $type = $this instanceof DefaultsConfigurator ? BoundArgument::DEFAULTS_BINDING : ($this instanceof InstanceofConfigurator ? BoundArgument::INSTANCEOF_BINDING : BoundArgument::SERVICE_BINDING); $bindings[$nameOrFqcn] = new BoundArgument($valueOrRef, true, $type, $this->path ?? null); diff --git a/Tests/Fixtures/Prototype/Foo.php b/Tests/Fixtures/Prototype/Foo.php index 1544dfd12..a7d7fe5f0 100644 --- a/Tests/Fixtures/Prototype/Foo.php +++ b/Tests/Fixtures/Prototype/Foo.php @@ -4,7 +4,7 @@ class Foo implements FooInterface, Sub\BarInterface { - public function __construct($bar = null, iterable $foo = null) + public function __construct($bar = null, iterable $foo = null, object $baz = null) { } diff --git a/Tests/Fixtures/config/defaults.expected.yml b/Tests/Fixtures/config/defaults.expected.yml index 13321967e..efa54cb88 100644 --- a/Tests/Fixtures/config/defaults.expected.yml +++ b/Tests/Fixtures/config/defaults.expected.yml @@ -15,14 +15,14 @@ services: - { name: t, a: b } autowire: true autoconfigure: true - arguments: ['@bar', !tagged_iterator foo] + arguments: ['@bar', !tagged_iterator foo, !service { class: Baz }] bar: class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo public: true tags: - { name: t, a: b } autowire: true - arguments: [null, !tagged_iterator foo] + arguments: [null, !tagged_iterator foo, !service { class: Baz }] calls: - [setFoo, ['@bar']] diff --git a/Tests/Fixtures/config/defaults.php b/Tests/Fixtures/config/defaults.php index 612340339..cdaee76e5 100644 --- a/Tests/Fixtures/config/defaults.php +++ b/Tests/Fixtures/config/defaults.php @@ -15,6 +15,7 @@ ->tag('t', ['a' => 'b']) ->bind(Foo::class, ref('bar')) ->bind('iterable $foo', tagged_iterator('foo')) + ->bind('object $baz', inline('Baz')) ->public(); $s->set(Foo::class)->args([ref('bar')])->public(); From 879913b7a488bb3c85f86373707089c1a1fadc1a Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Tue, 11 Jan 2022 14:08:20 -0500 Subject: [PATCH 2/2] [DependencyInjection] copy synthetic status when resolving child definitions --- Compiler/ResolveChildDefinitionsPass.php | 2 ++ .../ResolveChildDefinitionsPassTest.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/Compiler/ResolveChildDefinitionsPass.php b/Compiler/ResolveChildDefinitionsPass.php index 99c374ee4..333480d66 100644 --- a/Compiler/ResolveChildDefinitionsPass.php +++ b/Compiler/ResolveChildDefinitionsPass.php @@ -114,6 +114,8 @@ private function doResolveDefinition(ChildDefinition $definition): Definition $def->setBindings($definition->getBindings() + $parentDef->getBindings()); + $def->setSynthetic($definition->isSynthetic()); + // overwrite with values specified in the decorator $changes = $definition->getChanges(); if (isset($changes['class'])) { diff --git a/Tests/Compiler/ResolveChildDefinitionsPassTest.php b/Tests/Compiler/ResolveChildDefinitionsPassTest.php index f14cbb019..7a89feb9b 100644 --- a/Tests/Compiler/ResolveChildDefinitionsPassTest.php +++ b/Tests/Compiler/ResolveChildDefinitionsPassTest.php @@ -412,4 +412,21 @@ public function testProcessDetectsChildDefinitionIndirectCircularReference() $this->process($container); } + + public function testProcessCopiesSyntheticStatus() + { + $container = new ContainerBuilder(); + + $container->register('parent'); + + $container + ->setDefinition('child', new ChildDefinition('parent')) + ->setSynthetic(true) + ; + + $this->process($container); + + $def = $container->getDefinition('child'); + $this->assertTrue($def->isSynthetic()); + } }