Skip to content

Commit 24e802b

Browse files
committed
[DependencyInjection][HttpKernel] Fix enum typed bindings
1 parent 25771ea commit 24e802b

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

Compiler/ResolveBindingsPass.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ protected function processValue($value, $isRoot = false)
133133
continue;
134134
}
135135

136+
if (is_subclass_of($m[1], \UnitEnum::class)) {
137+
$bindingNames[substr($key, \strlen($m[0]))] = $binding;
138+
continue;
139+
}
140+
136141
if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition && !$bindingValue instanceof TaggedIteratorArgument && !$bindingValue instanceof ServiceLocatorArgument) {
137142
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)));
138143
}

Tests/Compiler/ResolveBindingsPassTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2525
use Symfony\Component\DependencyInjection\Reference;
2626
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
27+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
2728
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
29+
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedEnumArgumentDummy;
2830
use Symfony\Component\DependencyInjection\Tests\Fixtures\ParentNotExists;
2931
use Symfony\Component\DependencyInjection\TypedReference;
3032

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

68+
/**
69+
* @requires PHP 8.1
70+
*/
71+
public function testProcessEnum()
72+
{
73+
$container = new ContainerBuilder();
74+
75+
$bindings = [
76+
FooUnitEnum::class.' $bar' => new BoundArgument(FooUnitEnum::BAR),
77+
];
78+
79+
$definition = $container->register(NamedEnumArgumentDummy::class, NamedEnumArgumentDummy::class);
80+
$definition->setBindings($bindings);
81+
82+
$pass = new ResolveBindingsPass();
83+
$pass->process($container);
84+
85+
$expected = [FooUnitEnum::BAR];
86+
$this->assertEquals($expected, $definition->getArguments());
87+
}
88+
6689
public function testUnusedBinding()
6790
{
6891
$this->expectException(InvalidArgumentException::class);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
class NamedEnumArgumentDummy
15+
{
16+
public function __construct(FooUnitEnum $bar)
17+
{
18+
}
19+
}

0 commit comments

Comments
 (0)