From 0eb34f326bd3173229848ac0d9dbe7e9ac19d671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Tue, 10 Mar 2015 16:26:38 +0100 Subject: [PATCH] [DependencyInjection] Added support for keys "method" and "arguments" in "calls" statement for yaml format --- .../DependencyInjection/Loader/YamlFileLoader.php | 11 +++++++++-- .../Tests/Fixtures/yaml/services21.yml | 15 +++++++++++++++ .../Tests/Loader/YamlFileLoaderTest.php | 12 ++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services21.yml diff --git a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php index 54ad8be7c8c9..e903a4263474 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php @@ -237,8 +237,15 @@ private function parseDefinition($id, $service, $file) } foreach ($service['calls'] as $call) { - $args = isset($call[1]) ? $this->resolveServices($call[1]) : array(); - $definition->addMethodCall($call[0], $args); + if (isset($call['method'])) { + $method = $call['method']; + $args = isset($call['arguments']) ? $this->resolveServices($call['arguments']) : array(); + } else { + $method = $call[0]; + $args = isset($call[1]) ? $this->resolveServices($call[1]) : array(); + } + + $definition->addMethodCall($method, $args); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services21.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services21.yml new file mode 100644 index 000000000000..da717a8bb296 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services21.yml @@ -0,0 +1,15 @@ +services: + manager: + class: UserManager + arguments: + - true + calls: + - method: setLogger + arguments: + - @logger + - method: setClass + arguments: + - User + tags: + - name: manager + alias: user diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php index d40886884b4c..38765c004770 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php @@ -267,4 +267,16 @@ public function testTagWithAttributeArrayThrowsException() $this->assertStringStartsWith('A "tags" attribute must be of a scalar-type for service "foo_service", tag "foo", attribute "bar"', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag-attribute is not a scalar'); } } + + public function testLoadYamlOnlyWithKeys() + { + $container = new ContainerBuilder(); + $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')); + $loader->load('services21.yml'); + + $definition = $container->getDefinition('manager'); + $this->assertEquals(array(array('setLogger', array(new Reference('logger'))), array('setClass', array('User'))), $definition->getMethodCalls()); + $this->assertEquals(array(true), $definition->getArguments()); + $this->assertEquals(array('manager' => array(array('alias' => 'user'))), $definition->getTags()); + } }