8000 [DependencyInjection][HttpKernel] Fix enum typed bindings by ogizanagi · Pull Request #44838 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000 Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ protected function processValue($value, $isRoot = false)
continue;
}

if (is_subclass_of($m[1], \UnitEnum::class)) {
$bindingNames[substr($key, \strlen($m[0]))] = $binding;
continue;
}

if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition && !$bindingValue instanceof TaggedIteratorArgument && !$bindingValue instanceof ServiceLocatorArgument) {
throw new InvalidArgumentException(sprintf('Invalid value for binding key "%s" for service "%s": expected "%s", "%s", "%s", "%s" or null, "%s" given.', $key, $this->currentId, Reference::class, Definition::class, TaggedIteratorArgument::class, ServiceLocatorArgument::class, \gettype($bindingValue)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedEnumArgumentDummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\ParentNotExists;
use Symfony\Component\DependencyInjection\TypedReference;

Expand Down Expand Up @@ -63,6 +65,27 @@ public function testProcess()
$this->assertEquals([['setSensitiveClass', [new Reference('foo')]]], $definition->getMethodCalls());
}

/**
* @requires PHP 8.1
*/
public function testProcessEnum()
{
$container = new ContainerBuilder();

$bindings = [
FooUnitEnum::class.' $bar' => new BoundArgument(FooUnitEnum::BAR),
];

$definition = $container->register(NamedEnumArgumentDummy::class, NamedEnumArgumentDummy::class);
$definition->setBindings($bindings);

$pass = new ResolveBindingsPass();
$pass->process($container);

$expected = [FooUnitEnum::BAR];
$this->assertEquals($expected, $definition->getArguments());
}

public function testUnusedBinding()
{
$this->expectException(InvalidArgumentException::class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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;

class NamedEnumArgumentDummy
{
public function __construct(FooUnitEnum $bar)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ public function process(ContainerBuilder $container)
$type = ltrim($target = (string) ProxyHelper::getTypeHint($r, $p), '\\');
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;

if (is_subclass_of($type, \UnitEnum::class)) {
// do not attempt to register enum typed arguments
continue;
}

if (isset($arguments[$r->name][$p->name])) {
$target = $arguments[$r->name][$p->name];
if ('?' !== $target[0]) {
Expand All @@ -156,6 +151,9 @@ public function process(ContainerBuilder $container)
$args[$p->name] = $bindingValue;
}

continue;
} elseif (is_subclass_of($type, \UnitEnum::class)) {
// do not attempt to register enum typed arguments if not already present in bindings
continue;
} elseif (!$type || !$autowire || '\\' !== $target[0]) {
continue;
Expand Down
0