Skip to content

Commit 01344e3

Browse files
bug #49077 [DependencyInjection] Fix named arguments when using ContainerBuilder before compilation (nicolas-grekas)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Fix named arguments when using ContainerBuilder before compilation | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #49074 | License | MIT | Doc PR | - Commits ------- 0d2f828 [DependencyInjection] Fix named arguments when using ContainerBuilder before compilation
2 parents 39cd93a + 0d2f828 commit 01344e3

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,10 @@ private function createService(Definition $definition, array &$inlineServices, b
10891089
return $this->services[$id] ?? $this->privates[$id];
10901090
}
10911091

1092+
if (!array_is_list($arguments)) {
1093+
$arguments = array_combine(array_map(function ($k) { return preg_replace('/^.*\\$/', '', $k); }, array_keys($arguments)), $arguments);
1094+
}
1095+
10921096
if (null !== $factory) {
10931097
$service = $factory(...$arguments);
10941098

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,7 @@ public function testFindTags()
17771777
/**
17781778
* @requires PHP 8
17791779
*/
1780-
public function testNamedArgument()
1780+
public function testNamedArgumentAfterCompile()
17811781
{
17821782
$container = new ContainerBuilder();
17831783
$container->register(E::class)
@@ -1791,6 +1791,21 @@ public function testNamedArgument()
17911791
$this->assertSame('', $e->first);
17921792
$this->assertSame(2, $e->second);
17931793
}
1794+
1795+
/**
1796+
* @requires PHP 8
1797+
*/
1798+
public function testNamedArgumentBeforeCompile()
1799+
{
1800+
$container = new ContainerBuilder();
1801+
$container->register(E::class, E::class)
1802+
->setPublic(true)
1803+
->setArguments(['$first' => 1]);
1804+
1805+
$e = $container->get(E::class);
1806+
1807+
$this->assertSame(1, $e->first);
1808+
}
17941809
}
17951810

17961811
class FooClass

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