10000 [DependencyInjection] Add support an integer return for default_index_method by maranqz · Pull Request #40337 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Add support an integer return for default_index_method #40337

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
Mar 10, 2021
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
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,12 @@ public static function getDefaultIndex(ContainerBuilder $container, string $serv

$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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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
Expand Down Expand Up @@ -145,12 +146,15 @@ public function testTheIndexedTagsByDefaultIndexMethod()
$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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
0