Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DependencyInjection] add a Service attribute to explicitly choose the injected service id #45573

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
23 changes: 23 additions & 0 deletions 23 src/Symfony/Component/DependencyInjection/Attribute/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Attribute;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_PARAMETER)]
final class Service
{
public function __construct(public string $id)
{
}
}
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add `$exclude` to `TaggedIterator` and `TaggedLocator` attributes
* Add `$exclude` to `tagged_iterator` and `tagged_locator` configurator
* Add an `env` function to the expression language provider
* Add a `Service` attribute to explicitly choose the injected service id

6.0
---
Expand Down
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\Service;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Component\DependencyInjection\Attribute\Target;
Expand All @@ -22,6 +23,7 @@
use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\TypedReference;

/**
Expand Down Expand Up @@ -256,6 +258,12 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
$arguments[$index] = new ServiceLocatorArgument(new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute, $attribute->defaultIndexMethod, true, $attribute->defaultPriorityMethod, (array) $attribute->exclude));
break;
}

if (Service::class === $attribute->getName()) {
$attribute = $attribute->newInstance();
$arguments[$index] = new Reference($attribute->id);
break;
}
}

if ('' !== ($arguments[$index] ?? '')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1121,4 +1121,23 @@ public function testDecorationWithServiceAndAliasedInterface()
static::assertInstanceOf(DecoratedDecorator::class, $container->get(DecoratorInterface::class));
static::assertInstanceOf(DecoratedDecorator::class, $container->get(DecoratorImpl::class));
}

public function testServiceAttributeParameter()
{
$container = new ContainerBuilder();

$container->register(AutowireServiceParameter::class)
->setAutowired(true)
;

$container->register('some.id', \stdClass::class);

(new ResolveClassPass())->process($container);
(new AutowirePass())->process($container);

$definition = $container->getDefinition(AutowireServiceParameter::class);

$this->assertCount(1, $definition->getArguments());
$this->assertSame('some.id', (string) $definition->getArgument(0));
}
}
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\Service;
use Symfony\Contracts\Service\Attribute\Required;

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

class AutowireServiceParameter
{
public function __construct(#[Service('some.id')] $service)
{
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.