From f0922c70d6559f2fd0e8caf8a88b4bc4cabcd540 Mon Sep 17 00:00:00 2001 From: maranqz Date: Mon, 1 Mar 2021 20:45:45 +0300 Subject: [PATCH] [DependencyInjection] Add support an integer return for default_index_method --- .../Component/DependencyInjection/CHANGELOG.md | 1 + .../Compiler/PriorityTaggedServiceTrait.php | 6 +++++- .../PriorityTaggedServiceTraitTest.php | 4 ++++ .../Tests/Fixtures/IntTagClass.php | 18 ++++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100755 src/Symfony/Component/DependencyInjection/Tests/Fixtures/IntTagClass.php diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index 1e45c669e8..82837aa0e5 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -10,6 +10,7 @@ CHANGELOG * Add autoconfigurable attributes * Add support for per-env configuration in loaders * Add `ContainerBuilder::willBeAvailable()` to help with conditional configuration + * Add support an integer return value for default_index_method 5.2.0 ----- diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php index 806545bee9..30b31eed16 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php @@ -139,8 +139,12 @@ class PriorityTaggedServiceUtil $defaultIndex = $rm->invoke(null); + if (\is_int($defaultIndex)) { + $defaultIndex = (string) $defaultIndex; + } + if (!\is_string($defaultIndex)) { - throw new InvalidArgumentException(implode(sprintf('return a string (got "%s")', get_debug_type($defaultIndex)), $message)); + throw new InvalidArgumentException(implode(sprintf('return string|int (got "%s")', get_debug_type($defaultIndex)), $message)); } return $defaultIndex; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php index 2674ea4dde..5c150a26d0 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php @@ -20,6 +20,7 @@ use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Tests\Fixtures\BarTagClass; use Symfony\Component\DependencyInjection\Tests\Fixtures\FooTagClass; use Symfony\Component\DependencyInjection\Tests\Fixtures\FooTaggedForInvalidDefaultMethodClass; +use Symfony\Component\DependencyInjection\Tests\Fixtures\IntTagClass; use Symfony\Component\DependencyInjection\TypedReference; class PriorityTaggedServiceTraitTest extends TestCase @@ -145,12 +146,15 @@ class PriorityTaggedServiceTraitTest extends TestCase $definition->addTag('my_custom_tag', ['priority' => 100]); $definition->addTag('my_custom_tag', []); + $container->register('service3', IntTagClass::class)->addTag('my_custom_tag'); + $priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation(); $tag = new TaggedIteratorArgument('my_custom_tag', 'foo', 'getFooBar'); $expected = [ 'bar_tab_class_with_defaultmethod' => new TypedReference('service2', BarTagClass::class), 'service1' => new TypedReference('service1', FooTagClass::class), + '10' => new TypedReference('service3', IntTagClass::class), ]; $services = $priorityTaggedServiceTraitImplementation->test($tag, $container); $this->assertSame(array_keys($expected), array_keys($services)); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/IntTagClass.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/IntTagClass.php new file mode 100755 index 0000000000..a3114a10b5 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/IntTagClass.php @@ -0,0 +1,18 @@ +