Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Add Parameter attribute to bind container parameters to services. #44780

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

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 20 additions & 0 deletions src/Symfony/Component/DependencyInjection/Attribute/Parameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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] ?? '')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@
*/
class AutowirePassTest extends TestCase
{
/**
* @requires PHP 8.0
Copy link
Member

Choose a reason for hiding this comment

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

not needed anymore

*/
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use Symfony\Component\DependencyInjection\Attribute\Parameter;
use Symfony\Contracts\Service\Attribute\Required;

class AutowireSetter
Expand All @@ -26,3 +27,10 @@ class AutowireProperty
#[Required]
public Foo $foo;
}

class AutowiredParameterTest
{
public function __construct(#[Parameter("parameter.test")] public string $testParam)
Copy link
Member

Choose a reason for hiding this comment

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

please use single-quoted strings

{
}
}