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

Skip to content

Commit 4e66554

Browse files
committed
feature #21313 [DI] Add Yaml syntax for short services definition (ogizanagi)
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
2 parents 9d8c6c6 + 83b599c commit 4e66554

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ private function parseDefinition($id, $service, $file, array $defaults)
245245
return;
246246
}
247247

248+
if (is_array($service) && array_values($service) === $service) {
249+
$service = array('arguments' => $service);
250+
}
251+
248252
if (null === $service) {
249253
$service = array();
250254
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services28.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ services:
3939
alias: with_defaults
4040

4141
with_defaults_aliased_short: '@with_defaults'
42+
43+
with_shortcut_args: [foo]

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services6.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ services:
3939
new_factory1: { class: FooBarClass, factory: factory}
4040
new_factory2: { class: FooBarClass, factory: ['@baz', getClass]}
4141
new_factory3: { class: FooBarClass, factory: [BazClass, getInstance]}
42+
with_shortcut_args: [foo, '@baz']

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public function testLoadServices()
153153
$this->assertEquals('factory', $services['new_factory1']->getFactory(), '->load() parses the factory tag');
154154
$this->assertEquals(array(new Reference('baz'), 'getClass'), $services['new_factory2']->getFactory(), '->load() parses the factory tag');
155155
$this->assertEquals(array('BazClass', 'getInstance'), $services['new_factory3']->getFactory(), '->load() parses the factory tag');
156+
$this->assertEquals(array('foo', new Reference('baz')), $services['with_shortcut_args']->getArguments(), '->load() parses short service definition');
156157

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

387+
$this->assertFalse($container->getDefinition('with_shortcut_args')->isPublic());
388+
$this->assertSame(array('foo' => array(array())), $container->getDefinition('with_shortcut_args')->getTags());
389+
$this->assertTrue($container->getDefinition('with_shortcut_args')->isAutowired());
390+
386391
$container->compile();
387392

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

0 commit comments

Comments
 (0)