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
This commit is contained in:
commit
89cbafdcd1
@ -237,8 +237,15 @@ class YamlFileLoader extends FileLoader
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,15 @@
|
||||
services:
|
||||
manager:
|
||||
class: UserManager
|
||||
arguments:
|
||||
- true
|
||||
calls:
|
||||
- method: setLogger
|
||||
arguments:
|
||||
- @logger
|
||||
- method: setClass
|
||||
arguments:
|
||||
- User
|
||||
tags:
|
||||
- name: manager
|
||||
alias: user
|
@ -270,4 +270,16 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user