-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[DependencyInjection] Add #[AutowireIterator]
attribute and improve #[AutowireLocator]
#51832
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
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev
Previous commit
[DependencyInjection] Add tests for
AutowireLocator
/AutowireIterator
- Loading branch information
commit a87f2e0c248e5e22c9445ba2f371ab667e49faf8
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/DependencyInjection/Tests/Fixtures/AutowireIteratorConsumer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?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\Fixtures; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator; | ||
use Symfony\Contracts\Service\Attribute\SubscribedService; | ||
|
||
final class AutowireIteratorConsumer | ||
{ | ||
public function __construct( | ||
#[AutowireIterator([ | ||
BarTagClass::class, | ||
'with_key' => FooTagClass::class, | ||
'nullable' => '?invalid', | ||
'subscribed' => new SubscribedService(type: 'string', attributes: new Autowire('%some.parameter%')), | ||
])] | ||
public readonly iterable $iterator, | ||
) { | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
use Symfony\Component\DependencyInjection\Attribute\AutowireCallable; | ||
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator; | ||
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator; | ||
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; | ||
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator; | ||
9E88
|
@@ -31,6 +32,7 @@ | |
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass; | ||
use Symfony\Component\HttpKernel\Tests\Fixtures\Suit; | ||
use Symfony\Contracts\Service\Attribute\SubscribedService; | ||
|
||
class RegisterControllerArgumentLocatorsPassTest extends TestCase | ||
{ | ||
|
@@ -499,6 +501,7 @@ public function testAutowireAttribute() | |
public function testTaggedIteratorAndTaggedLocatorAttributes() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->setParameter('some.parameter', 'bar'); | ||
$resolver = $container->register('argument_resolver.service', \stdClass::class)->addArgument([]); | ||
|
||
$container->register('bar', \stdClass::class)->addTag('foobar'); | ||
|
@@ -517,25 +520,48 @@ public function testTaggedIteratorAndTaggedLocatorAttributes() | |
/** @var ServiceLocator $locator */ | ||
$locator = $container->get($locatorId)->get('foo::fooAction'); | ||
|
||
$this->assertCount(3, $locator->getProvidedServices()); | ||
$this->assertCount(7, $locator->getProvidedServices()); | ||
|
||
$this->assertTrue($locator->has('iterator')); | ||
$this->assertInstanceOf(RewindableGenerator::class, $argIterator = $locator->get('iterator')); | ||
$this->assertTrue($locator->has('iterator1')); | ||
$this->assertInstanceOf(RewindableGenerator::class, $argIterator = $locator->get('iterator1')); | ||
$this->assertCount(2, $argIterator); | ||
|
||
$this->assertTrue($locator->has('locator')); | ||
$this->assertInstanceOf(ServiceLocator::class, $argLocator = $locator->get('locator')); | ||
$this->assertTrue($locator->has('iterator2')); | ||
$this->assertInstanceOf(RewindableGenerator::class, $argIterator = $locator->get('iterator2')); | ||
$this->assertCount(2, $argIterator); | ||
|
||
$this->assertTrue($locator->has('locator1')); | ||
$this->assertInstanceOf(ServiceLocator::class, $argLocator = $locator->get('locator1')); | ||
$this->assertCount(2, $argLocator); | ||
$this->assertTrue($argLocator->has('bar')); | ||
$this->assertTrue($argLocator->has('baz')); | ||
|
||
$this->assertSame(iterator_to_array($argIterator), [$argLocator->get('bar'), $argLocator->get('baz')]); | ||
|
||
$this->assertTrue($locator->has('locator2')); | ||
$this->assertInstanceOf(ServiceLocator::class, $argLocator = $locator->get('locator2')); | ||
$this->assertCount(2, $argLocator); | ||
$this->assertTrue($argLocator->has('bar')); | ||
$this->assertTrue($argLocator->has('baz')); | ||
|
||
$this->assertSame(iterator_to_array($argIterator), [$argLocator->get('bar'), $argLocator->get('baz')]); | ||
|
||
$this->assertTrue($locator->has('container')); | ||
$this->assertInstanceOf(ServiceLocator::class, $argLocator = $locator->get('container')); | ||
$this->assertTrue($locator->has('container1')); | ||
$this->assertInstanceOf(ServiceLocator::class, $argLocator = F438 $locator->get('container1')); | ||
$this->assertCount(2, $argLocator); | ||
$this->assertTrue($argLocator->has('bar')); | ||
$this->assertTrue($argLocator->has('baz')); | ||
|
||
$this->assertTrue($locator->has('container2')); | ||
$this->assertInstanceOf(ServiceLocator::class, $argLocator = $locator->get('container2')); | ||
$this->assertCount(1, $argLocator); | ||
$this->assertTrue($argLocator->has('foo')); | ||
$this->assertSame('bar', $argLocator->get('foo')); | ||
|
||
$this->assertTrue($locator->has('iterator3')); | ||
$this->assertInstanceOf(RewindableGenerator::class, $argIterator = $locator->get('iterator3')); | ||
$this->assertCount(1, $argIterator); | ||
$this->assertSame('bar', iterator_to_array($argIterator)['foo']); | ||
} | ||
} | ||
|
||
|
@@ -674,9 +700,13 @@ public function fooAction( | |
class WithTaggedIteratorAndTaggedLocator | ||
{ | ||
public function fooAction( | ||
#[TaggedIterator('foobar')] iterable $iterator, | ||
#[TaggedLocator('foobar')] ServiceLocator $locator, | ||
#[AutowireLocator(['bar', 'baz'])] ContainerInterface $container, | ||
#[TaggedIterator('foobar')] iterable $iterator1, | ||
#[AutowireIterator('foobar')] iterable $iterator2, | ||
#[TaggedLocator('foobar')] ServiceLocator $locator1, | ||
#[AutowireLocator('foobar')] ServiceLocator $locator2, | ||
#[AutowireLocator(['bar', 'baz'])] ContainerInterface $container1, | ||
#[AutowireLocator(['foo' => new SubscribedService(type: 'string', attributes: new Autowire('%some.parameter%'))])] ContainerInterface $container2, | ||
#[AutowireIterator(['foo' => new SubscribedService(type: 'string', attributes: new Autowire('%some.parameter%'))])] iterable $iterator3, | ||
) { | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kbond why was this added? is there a way to remove this conflict?
this makes it not possible for us to upgrade to symfony 6.4 because this forces a requirement for
psr/container ^2.0
, and we are still on v1 because the latest version oflaminas/laminas-servicemanager
requires v1:https://github.com/laminas/laminas-servicemanager/blob/3.23.x/composer.json#L26
this is sort of the same issue as api-platform/core#5811
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/cc @nicolas-grekas
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @kbond, tagged you as it looked like your commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed because the test case uses the $type argument of SubscribedService, which exists since 3.2.
PR welcome to change the test case and relax this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks