Skip to content

Commit

Permalink
feature #21313 [DI] Add Yaml syntax for short services definition (og…
Browse files Browse the repository at this point in the history
…izanagi)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DI] Add Yaml syntax for short services definition

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | todo

In my experience, most (at least, a lot) of the service definitions in an end-application only require the class and constructor arguments.

#21133 allows to get rid of the `class` attribute by using the service id.
Which means only arguments remain for most use-cases. Hence, we could support this syntax:

#### Before:

```yml
services:
    App\Foo\Bar:
        arguments: ['@baz', 'foo', '%qux%']
```

#### After:

```yml
services:
    App\Foo\Bar: ['@baz', 'foo', '%qux%']
```

It works especially well along with services `_defaults` introduced in #21071 :

```yml
services:
    _defaults:
        public: false
        autowire: ['set*']
        tags: ['serializer.normalizer']

    App\Serializer\FooNormalizer: ['@baz', 'foo', '%qux%']
```

Commits
-------

83b599c [DI] Add Yaml syntax for short services definition
  • Loading branch information
fabpot committed Jan 23, 2017
2 parents 9d8c6c6 + 83b599c commit 4e66554
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
Expand Up @@ -245,6 +245,10 @@ private function parseDefinition($id, $service, $file, array $defaults)
return;
}

if (is_array($service) && array_values($service) === $service) {
$service = array('arguments' => $service);
}

if (null === $service) {
$service = array();
}
Expand Down
Expand Up @@ -39,3 +39,5 @@ services:
alias: with_defaults

with_defaults_aliased_short: '@with_defaults'

with_shortcut_args: [foo]
Expand Up @@ -39,3 +39,4 @@ services:
new_factory1: { class: FooBarClass, factory: factory}
new_factory2: { class: FooBarClass, factory: ['@baz', getClass]}
new_factory3: { class: FooBarClass, factory: [BazClass, getInstance]}
with_shortcut_args: [foo, '@baz']
Expand Up @@ -153,6 +153,7 @@ public function testLoadServices()
$this->assertEquals('factory', $services['new_factory1']->getFactory(), '->load() parses the factory tag');
$this->assertEquals(array(new Reference('baz'), 'getClass'), $services['new_factory2']->getFactory(), '->load() parses the factory tag');
$this->assertEquals(array('BazClass', 'getInstance'), $services['new_factory3']->getFactory(), '->load() parses the factory tag');
$this->assertEquals(array('foo', new Reference('baz')), $services['with_shortcut_args']->getArguments(), '->load() parses short service definition');

$aliases = $container->getAliases();
$this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases');
Expand Down Expand Up @@ -383,6 +384,10 @@ public function testDefaults()
$this->assertArrayNotHasKey('public', $container->getDefinition('no_defaults_child')->getChanges());
$this->assertArrayNotHasKey('autowire', $container->getDefinition('no_defaults_child')->getChanges());

$this->assertFalse($container->getDefinition('with_shortcut_args')->isPublic());
$this->assertSame(array('foo' => array(array())), $container->getDefinition('with_shortcut_args')->getTags());
$this->assertTrue($container->getDefinition('with_shortcut_args')->isAutowired());

$container->compile();

$this->assertTrue($container->getDefinition('with_null')->isPublic());
Expand Down

0 comments on commit 4e66554

Please sign in to comment.