Skip to content

Commit caa124e

Browse files
bug #36387 [DI] fix detecting short service syntax in yaml (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [DI] fix detecting short service syntax in yaml | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - We do allow interfaces/classes as keys in arguments, yet the short syntax fails to know about those. This fixes it, allowing one to use: ``` services: App\Foo: App\BarInterface: '@app\BarClass' ``` As a reminder, by-name is also allowed: ``` services: App\Foo: { $bar: '@app\BarClass' } ``` Commits ------- bf17165fb1 [DI] fix detecting short service syntax in yaml
2 parents 32bf529 + 2b91cff commit caa124e

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Loader/YamlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private function parseDefaults(array &$content, string $file): array
299299
private function isUsingShortSyntax(array $service): bool
300300
{
301301
foreach ($service as $key => $value) {
302-
if (\is_string($key) && ('' === $key || '$' !== $key[0])) {
302+
if (\is_string($key) && ('' === $key || ('$' !== $key[0] && false === strpos($key, '\\')))) {
303303
return false;
304304
}
305305
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
foo_bar: [1, 2]
3+
4+
bar_foo:
5+
$a: 'a'
6+
App\Foo: 'foo'

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ public function testLoadServices()
205205
$this->assertEquals(['decorated', 'decorated.pif-pouf', 5, ContainerInterface::IGNORE_ON_INVALID_REFERENCE], $services['decorator_service_with_name_and_priority_and_on_invalid']->getDecoratedService());
206206
}
207207

208+
public function testLoadShortSyntax()
209+
{
210+
$container = new ContainerBuilder();
211+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
212+
$loader->load('services_short_syntax.yml');
213+
$services = $container->getDefinitions();
214+
215+
$this->assertSame([1, 2], $services['foo_bar']->getArguments());
216+
$this->assertSame(['$a' => 'a', 'App\Foo' => 'foo'], $services['bar_foo']->getArguments());
217+
}
218+
208219
public function testDeprecatedAliases()
209220
{
210221
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)