From 83b599c6ad2ae3737b312b9fd85d5156ef0147db Mon Sep 17 00:00:00 2001 From: Maxime Steinhausser Date: Mon, 16 Jan 2017 22:45:06 +0100 Subject: [PATCH] [DI] Add Yaml syntax for short services definition --- .../Component/DependencyInjection/Loader/YamlFileLoader.php | 4 ++++ .../DependencyInjection/Tests/Fixtures/yaml/services28.yml | 2 ++ .../DependencyInjection/Tests/Fixtures/yaml/services6.yml | 1 + .../DependencyInjection/Tests/Loader/YamlFileLoaderTest.php | 5 +++++ 4 files changed, 12 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php index 1329d22a6ff7..9a997870d136 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php @@ -229,6 +229,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(); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services28.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services28.yml index 6543a101ed29..beb34c1a11d5 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services28.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services28.yml @@ -39,3 +39,5 @@ services: alias: with_defaults with_defaults_aliased_short: '@with_defaults' + + with_shortcut_args: [foo] diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services6.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services6.yml index 20ed7e315a99..79810050ca31 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services6.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services6.yml @@ -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'] diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php index 52223442cfd6..1cf02f7d405c 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php @@ -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'); @@ -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());