Skip to content

Commit

Permalink
feature #13892 [DependencyInjection] Improved yaml syntax (hason)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Mar 23, 2015
2 parents 89a6b95 + 0eb34f3 commit 89cbafd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Expand Up @@ -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);
}
}

Expand Down
@@ -0,0 +1,15 @@
services:
manager:
class: UserManager
arguments:
- true
calls:
- method: setLogger
arguments:
- @logger
- method: setClass
arguments:
- User
tags:
- name: manager
alias: user
Expand Up @@ -270,4 +270,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());
}
}

0 comments on commit 89cbafd

Please sign in to comment.