From 94d681c6a72244dbe314b0310b545bd07f8eb83b Mon Sep 17 00:00:00 2001 From: Emirald Mateli Date: Thu, 23 Dec 2021 20:17:34 +0100 Subject: [PATCH] Add Parameter attribute to bind container parameters to services. --- .../Attribute/Parameter.php | 20 ++++++++++++++++++ .../Compiler/AutowirePass.php | 7 +++++++ .../Tests/Compiler/AutowirePassTest.php | 21 +++++++++++++++++++ .../includes/autowiring_classes_80.php | 8 +++++++ 4 files changed, 56 insertions(+) create mode 100644 src/Symfony/Component/DependencyInjection/Attribute/Parameter.php diff --git a/src/Symfony/Component/DependencyInjection/Attribute/Parameter.php b/src/Symfony/Component/DependencyInjection/Attribute/Parameter.php new file mode 100644 index 0000000000000..f5b65ed9f42f9 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Attribute/Parameter.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Attribute; + +#[\Attribute(\Attribute::TARGET_PARAMETER)] +class Parameter +{ + public function __construct(public string $name) + { + } +} diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php index bd85168a21af2..894f5c8141cb8 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php @@ -14,6 +14,7 @@ use Symfony\Component\Config\Resource\ClassExistenceResource; use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; +use Symfony\Component\DependencyInjection\Attribute\Parameter; use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; use Symfony\Component\DependencyInjection\Attribute\TaggedLocator; use Symfony\Component\DependencyInjection\Attribute\Target; @@ -256,6 +257,12 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a $arguments[$index] = new ServiceLocatorArgument(new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute, $attribute->defaultIndexMethod, true, $attribute->defaultPriorityMethod)); break; } + + if (Parameter::class === $attribute->getName()) { + $attribute = $attribute->newInstance(); + $arguments[$index] = $this->container->getParameter($attribute->name); + break; + } } if ('' !== ($arguments[$index] ?? '')) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index 66082a9814127..a3d9916e753ff 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -38,6 +38,27 @@ */ class AutowirePassTest extends TestCase { + /** + * @requires PHP 8.0 + */ + public function testParameterBindingsSuccess() + { + $container = new ContainerBuilder(); + + $testDefinition = $container->register(AutowiredParameterTest::class); + $testDefinition->setAutowired(true); + + $container->setParameter('parameter.test', 'hello world'); + + (new ResolveClassPass())->process($container); + (new AutowirePass())->process($container); + $definition = $container->getDefinition(AutowiredParameterTest::class); + + $this->assertCount(1, $definition->getArguments()); + $this->assertEquals('hello world', $definition->getArgument(0)); + $this->assertEquals('hello world', $container->get(AutowiredParameterTest::class)->testParam); + } + public function testProcess() { $container = new ContainerBuilder(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php index e22ae85169e5a..9c23769b2c6a2 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php @@ -2,6 +2,7 @@ namespace Symfony\Component\DependencyInjection\Tests\Compiler; +use Symfony\Component\DependencyInjection\Attribute\Parameter; use Symfony\Contracts\Service\Attribute\Required; class AutowireSetter @@ -26,3 +27,10 @@ class AutowireProperty #[Required] public Foo $foo; } + +class AutowiredParameterTest +{ + public function __construct(#[Parameter("parameter.test")] public string $testParam) + { + } +}