[DependencyInjection] Add support an integer return for default_index_method

This commit is contained in:
maranqz 2021-03-01 20:45:45 +03:00 committed by Nicolas Grekas
parent 252f85c2c2
commit f0922c70d6
4 changed files with 28 additions and 1 deletions

View File

@ -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
-----

View File

@ -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;

View File

@ -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));

View File

@ -0,0 +1,18 @@
<?php
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
class IntTagClass
{
public static function getFooBar()
{
return 10;
}
public static function getPriority(): int
{
// Should be more than FooTagClass. More because this class is after
// FooTagClass (order by name). So we want to ensure it will be before it
return 30;
}
}