Skip to content

Commit 5ce52c4

Browse files
committed
feature #13892 [DependencyInjection] Improved yaml syntax (hason)
This PR was merged into the 2.7 branch. Discussion ---------- [DependencyInjection] Improved yaml syntax | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | This PR adds support for this: ```yaml services: manager: class: UserManager arguments: - true calls: - method: setLogger arguments: - @logger - method: setClass arguments: - User tags: - name: manager alias: user ``` Commits ------- 0eb34f3 [DependencyInjection] Added support for keys "method" and "arguments" in "calls" statement for yaml format
2 parents 025f792 + a422d7a commit 5ce52c4

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

Loader/YamlFileLoader.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,15 @@ private function parseDefinition($id, $service, $file)
237237
}
238238

239239
foreach ($service['calls'] as $call) {
240-
$args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
241-
$definition->addMethodCall($call[0], $args);
240+
if (isset($call['method'])) {
241+
$method = $call['method'];
242+
$args = isset($call['arguments']) ? $this->resolveServices($call['arguments']) : array();
243+
} else {
244+
$method = $call[0];
245+
$args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
246+
}
247+
248+
$definition->addMethodCall($method, $args);
242249
}
243250
}
244251

Tests/Fixtures/yaml/services21.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
services:
2+
manager:
3+
class: UserManager
4+
arguments:
5+
- true
6+
calls:
7+
- method: setLogger
8+
arguments:
9+
- @logger
10+
- method: setClass
11+
arguments:
12+
- User
13+
tags:
14+
- name: manager
15+
alias: user

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,16 @@ public function testTagWithAttributeArrayThrowsException()
270270
$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');
271271
}
272272
}
273+
274+
public function testLoadYamlOnlyWithKeys()
275+
{
276+
$container = new ContainerBuilder();
277+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
278+
$loader->load('services21.yml');
279+
280+
$definition = $container->getDefinition('manager');
281+
$this->assertEquals(array(array('setLogger', array(new Reference('logger'))), array('setClass', array('User'))), $definition->getMethodCalls());
282+
$this->assertEquals(array(true), $definition->getArguments());
283+
$this->assertEquals(array('manager' => array(array('alias' => 'user'))), $definition->getTags());
284+
}
273285
}

0 commit comments

Comments
 (0)