Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30594b2

Browse files
bug #44838 [DependencyInjection][HttpKernel] Fix enum typed bindings (ogizanagi)
This PR was merged into the 4.4 branch. Discussion ---------- [DependencyInjection][HttpKernel] Fix enum typed bindings | Q | A | ------------- | --- | Branch? | 4.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | N/A <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Relates to #44834 While: ```yml # services.yaml services: _defaults: autowire: true autoconfigure: true bind: $myParam: !php/const App\Status::Deleted ``` is working for me, the following isn't: ```diff # services.yaml services: _defaults: autowire: true autoconfigure: true bind: - $myParam: !php/const App\Status::Deleted + App\Status $myParam: !php/const App\Status::Deleted ``` -> > Invalid value for binding key "App\Status $myParam" for service […]: expected "Symfony\Component\DependencyInjection\Reference", "Symfony\Component\DependencyInjection\Definition", "Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument", "Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument" or null, "App\Status" given. This attempts to fix it by accounting for `\UnitEnum` in the `ResolveBindingPass`, as well as re-allowing enum types already present in bindings after #44826. Commits ------- c32d749 [DependencyInjection][HttpKernel] Fix enum typed bindings
2 parents c836db8 + c32d749 commit 30594b2

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 <[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+
}

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)