Skip to content

Commit

Permalink
bug #36387 [DI] fix detecting short service syntax in yaml (nicolas-g…
Browse files Browse the repository at this point in the history
…rekas)

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
-------

bf17165 [DI] fix detecting short service syntax in yaml
  • Loading branch information
nicolas-grekas committed Apr 10, 2020
2 parents ba58c6f + bf17165 commit 6f19746
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Expand Up @@ -299,7 +299,7 @@ private function parseDefaults(array &$content, string $file): array
private function isUsingShortSyntax(array $service): bool
{
foreach ($service as $key => $value) {
if (\is_string($key) && ('' === $key || '$' !== $key[0])) {
if (\is_string($key) && ('' === $key || ('$' !== $key[0] && false === strpos($key, '\\')))) {
return false;
}
}
Expand Down
@@ -0,0 +1,6 @@
services:
foo_bar: [1, 2]

bar_foo:
$a: 'a'
App\Foo: 'foo'
Expand Up @@ -205,6 +205,17 @@ public function testLoadServices()
$this->assertEquals(['decorated', 'decorated.pif-pouf', 5, ContainerInterface::IGNORE_ON_INVALID_REFERENCE], $services['decorator_service_with_name_and_priority_and_on_invalid']->getDecoratedService());
}

public function testLoadShortSyntax()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services_short_syntax.yml');
$services = $container->getDefinitions();

$this->assertSame([1, 2], $services['foo_bar']->getArguments());
$this->assertSame(['$a' => 'a', 'App\Foo' => 'foo'], $services['bar_foo']->getArguments());
}

public function testDeprecatedAliases()
{
$container = new ContainerBuilder();
Expand Down

0 comments on commit 6f19746

Please sign in to comment.