Skip to content

Commit c32d749

Browse files
committed
[DependencyInjection][HttpKernel] Fix enum typed bindings
1 parent 0f96dd0 commit c32d749

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

src/Symfony/Component/DependencyInjection/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
}

src/Symfony/Component/DependencyInjection/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 <fabien@symfony.com>
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+
}

src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@ public function process(ContainerBuilder $container)
127127
$type = ltrim($target = (string) ProxyHelper::getTypeHint($r, $p), '\\');
128128
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
129129

130-
if (is_subclass_of($type, \UnitEnum::class)) {
131-
// do not attempt to register enum typed arguments
132-
continue;
133-
}
134-
135130
if (isset($arguments[$r->name][$p->name])) {
136131
$target = $arguments[$r->name][$p->name];
137132
if ('?' !== $target[0]) {
@@ -156,6 +151,9 @@ public function process(ContainerBuilder $container)
156151
$args[$p->name] = $bindingValue;
157152
}
158153

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy