Skip to content

[DependencyInjection] add #[AutowireLocator] attribute #51392

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

Merged
merged 1 commit into from
Aug 21, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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;

use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;

#[\Attribute(\Attribute::TARGET_PARAMETER)]
class AutowireLocator extends Autowire
{
public function __construct(string ...$serviceIds)
{
$values = [];

foreach ($serviceIds as $key => $serviceId) {
if ($nullable = str_starts_with($serviceId, '?')) {
$serviceId = substr($serviceId, 1);
}

if (is_numeric($key)) {
$key = $serviceId;
}

$values[$key] = new Reference(
$serviceId,
$nullable ? ContainerInterface::IGNORE_ON_INVALID_REFERENCE : ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
);
}

parent::__construct(new ServiceLocatorArgument($values));
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Allow using `#[Target]` with no arguments to state that a parameter must match a named autowiring alias
* Deprecate `ContainerAwareInterface` and `ContainerAwareTrait`, use dependency injection instead
* Add `defined` env var processor that returns `true` for defined and neither null nor empty env vars
* Add `#[AutowireLocator]` attribute

6.3
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Tests\Attribute;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;

class AutowireLocatorTest extends TestCase
{
public function testSimpleLocator()
{
$locator = new AutowireLocator('foo', 'bar');

$this->assertEquals(
new ServiceLocatorArgument(['foo' => new Reference('foo'), 'bar' => new Reference('bar')]),
$locator->value,
);
}

public function testComplexLocator()
{
$locator = new AutowireLocator(
'?qux',
foo: 'bar',
bar: '?baz',
);

$this->assertEquals(
new ServiceLocatorArgument([
'qux' => new Reference('qux', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
'foo' => new Reference('bar'),
'bar' => new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
]),
$locator->value,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,24 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfiguredInterface2;
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfiguredService1;
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfiguredService2;
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutowireLocatorConsumer;
use Symfony\Component\DependencyInjection\Tests\Fixtures\BarTagClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedForDefaultPriorityClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooTagClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\IteratorConsumer;
use Symfony\Component\DependencyInjection\Tests\Fixtures\IteratorConsumerWithDefaultIndexMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\IteratorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\IteratorConsumerWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumer;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerConsumer;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerFactory;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithDefaultIndexMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithoutIndex;
use Symfony\Component\DependencyInjection\Tests\Fixtures\StaticMethodTag;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedConsumerWithExclude;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedIteratorConsumer;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedIteratorConsumerWithDefaultIndexMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedIteratorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedIteratorConsumerWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedLocatorConsumer;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedLocatorConsumerConsumer;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedLocatorConsumerFactory;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedLocatorConsumerWithDefaultIndexMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedLocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedLocatorConsumerWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedLocatorConsumerWithoutIndex;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService1;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService2;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3;
Expand Down Expand Up @@ -388,6 +389,30 @@ public function testTaggedServiceWithIndexAttributeAndDefaultMethod()
$this->assertSame(['bar_tab_class_with_defaultmethod' => $container->get(BarTagClass::class), 'foo' => $container->get(FooTagClass::class)], $param);
}

public function testLocatorConfiguredViaAttribute()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class)
->setPublic(true)
;
$container->register(FooTagClass::class)
->setPublic(true)
;
$container->register(AutowireLocatorConsumer::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

/** @var AutowireLocatorConsumer $s */
$s = $container->get(AutowireLocatorConsumer::class);

self::assertSame($container->get(BarTagClass::class), $s->locator->get(BarTagClass::class));
self::assertSame($container->get(FooTagClass::class), $s->locator->get('with_key'));
self::assertFalse($s->locator->has('nullable'));
}

public function testTaggedServiceWithIndexAttributeAndDefaultMethodConfiguredViaAttribute()
{
$container = new ContainerBuilder();
Expand All @@ -399,14 +424,14 @@ public function testTaggedServiceWithIndexAttributeAndDefaultMethodConfiguredVia
->setPublic(true)
->addTag('foo_bar', ['foo' => 'foo'])
;
$container->register(IteratorConsumer::class)
$container->register(TaggedIteratorConsumer::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

$s = $container->get(IteratorConsumer::class);
$s = $container->get(TaggedIteratorConsumer::class);

$param = iterator_to_array($s->getParam()->getIterator());
$this->assertSame(['bar_tab_class_with_defaultmethod' => $container->get(BarTagClass::class), 'foo' => $container->get(FooTagClass::class)], $param);
Expand All @@ -423,14 +448,14 @@ public function testTaggedIteratorWithDefaultIndexMethodConfiguredViaAttribute()
->setPublic(true)
->addTag('foo_bar')
;
$container->register(IteratorConsumerWithDefaultIndexMethod::class)
$container->register(TaggedIteratorConsumerWithDefaultIndexMethod::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

$s = $container->get(IteratorConsumerWithDefaultIndexMethod::class);
$s = $container->get(TaggedIteratorConsumerWithDefaultIndexMethod::class);

$param = iterator_to_array($s->getParam()->getIterator());
$this->assertSame(['bar_tag_class' => $container->get(BarTagClass::class), 'foo_tag_class' => $container->get(FooTagClass::class)], $param);
Expand All @@ -447,14 +472,14 @@ public function testTaggedIteratorWithDefaultPriorityMethodConfiguredViaAttribut
->setPublic(true)
->addTag('foo_bar')
;
$container->register(IteratorConsumerWithDefaultPriorityMethod::class)
$container->register(TaggedIteratorConsumerWithDefaultPriorityMethod::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

$s = $container->get(IteratorConsumerWithDefaultPriorityMethod::class);
$s = $container->get(TaggedIteratorConsumerWithDefaultPriorityMethod::class);

$param = iterator_to_array($s->getParam()->getIterator());
$this->assertSame([0 => $container->get(FooTagClass::class), 1 => $container->get(BarTagClass::class)], $param);
Expand All @@ -471,14 +496,14 @@ public function testTaggedIteratorWithDefaultIndexMethodAndWithDefaultPriorityMe
->setPublic(true)
->addTag('foo_bar')
;
$container->register(IteratorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod::class)
$container->register(TaggedIteratorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

$s = $container->get(IteratorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod::class);
$s = $container->get(TaggedIteratorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod::class);

$param = iterator_to_array($s->getParam()->getIterator());
$this->assertSame(['foo_tag_class' => $container->get(FooTagClass::class), 'bar_tag_class' => $container->get(BarTagClass::class)], $param);
Expand All @@ -495,15 +520,15 @@ public function testTaggedLocatorConfiguredViaAttribute()
->setPublic(true)
->addTag('foo_bar', ['foo' => 'foo'])
;
$container->register(LocatorConsumer::class)
$container->register(TaggedLocatorConsumer::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

/** @var LocatorConsumer $s */
$s = $container->get(LocatorConsumer::class);
/** @var TaggedLocatorConsumer $s */
$s = $container->get(TaggedLocatorConsumer::class);

$locator = $s->getLocator();
self::assertSame($container->get(BarTagClass::class), $locator->get('bar_tab_class_with_defaultmethod'));
Expand All @@ -521,15 +546,15 @@ public function testTaggedLocatorConfiguredViaAttributeWithoutIndex()
->setPublic(true)
->addTag('foo_bar')
;
$container->register(LocatorConsumerWithoutIndex::class)
$container->register(TaggedLocatorConsumerWithoutIndex::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

/** @var LocatorConsumerWithoutIndex $s */
$s = $container->get(LocatorConsumerWithoutIndex::class);
/** @var TaggedLocatorConsumerWithoutIndex $s */
$s = $container->get(TaggedLocatorConsumerWithoutIndex::class);

$locator = $s->getLocator();
self::assertSame($container->get(BarTagClass::class), $locator->get(BarTagClass::class));
Expand All @@ -547,15 +572,15 @@ public function testTaggedLocatorWithDefaultIndexMethodConfiguredViaAttribute()
->setPublic(true)
->addTag('foo_bar')
;
$container->register(LocatorConsumerWithDefaultIndexMethod::class)
$container->register(TaggedLocatorConsumerWithDefaultIndexMethod::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

/** @var LocatorConsumerWithoutIndex $s */
$s = $container->get(LocatorConsumerWithDefaultIndexMethod::class);
/** @var TaggedLocatorConsumerWithoutIndex $s */
$s = $container->get(TaggedLocatorConsumerWithDefaultIndexMethod::class);

$locator = $s->getLocator();
self::assertSame($container->get(BarTagClass::class), $locator->get('bar_tag_class'));
Expand All @@ -573,15 +598,15 @@ public function testTaggedLocatorWithDefaultPriorityMethodConfiguredViaAttribute
->setPublic(true)
->addTag('foo_bar')
;
$container->register(LocatorConsumerWithDefaultPriorityMethod::class)
$container->register(TaggedLocatorConsumerWithDefaultPriorityMethod::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

/** @var LocatorConsumerWithoutIndex $s */
$s = $container->get(LocatorConsumerWithDefaultPriorityMethod::class);
/** @var TaggedLocatorConsumerWithoutIndex $s */
$s = $container->get(TaggedLocatorConsumerWithDefaultPriorityMethod::class);

$locator = $s->getLocator();

Expand All @@ -602,15 +627,15 @@ public function testTaggedLocatorWithDefaultIndexMethodAndWithDefaultPriorityMet
->setPublic(true)
->addTag('foo_bar')
;
$container->register(LocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod::class)
$container->register(TaggedLocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

/** @var LocatorConsumerWithoutIndex $s */
$s = $container->get(LocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod::class);
/** @var TaggedLocatorConsumerWithoutIndex $s */
$s = $container->get(TaggedLocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod::class);

$locator = $s->getLocator();

Expand All @@ -629,18 +654,18 @@ public function testNestedDefinitionWithAutoconfiguredConstructorArgument()
->setPublic(true)
->addTag('foo_bar', ['foo' => 'foo'])
;
$container->register(LocatorConsumerConsumer::class)
$container->register(TaggedLocatorConsumerConsumer::class)
->setPublic(true)
->setArguments([
(new Definition(LocatorConsumer::class))
(new Definition(TaggedLocatorConsumer::class))
->setAutowired(true),
])
;

$container->compile();

/** @var LocatorConsumerConsumer $s */
$s = $container->get(LocatorConsumerConsumer::class);
/** @var TaggedLocatorConsumerConsumer $s */
$s = $container->get(TaggedLocatorConsumerConsumer::class);

$locator = $s->getLocatorConsumer()->getLocator();
self::assertSame($container->get(FooTagClass::class), $locator->get('foo'));
Expand All @@ -653,17 +678,17 @@ public function testFactoryWithAutoconfiguredArgument()
->setPublic(true)
->addTag('foo_bar', ['key' => 'my_service'])
;
$container->register(LocatorConsumerFactory::class);
$container->register(LocatorConsumer::class)
$container->register(TaggedLocatorConsumerFactory::class);
$container->register(TaggedLocatorConsumer::class)
->setPublic(true)
->setAutowired(true)
->setFactory(new Reference(LocatorConsumerFactory::class))
->setFactory(new Reference(TaggedLocatorConsumerFactory::class))
;

$container->compile();

/** @var LocatorConsumer $s */
$s = $container->get(LocatorConsumer::class);
/** @var TaggedLocatorConsumer $s */
$s = $container->get(TaggedLocatorConsumer::class);

$locator = $s->getLocator();
self::assertSame($container->get(FooTagClass::class), $locator->get('my_service'));
Expand Down
Loading