feature #23855 [DI] Allow dumping inline services in Yaml (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Allow dumping inline services in Yaml

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

e5bbf3f [DI] Allow dumping inline services in Yaml
This commit is contained in:
Nicolas Grekas 2017-08-10 16:19:11 +02:00
commit 00897c0222
3 changed files with 32 additions and 0 deletions

View File

@ -12,7 +12,9 @@
namespace Symfony\Component\DependencyInjection\Dumper;
use Symfony\Component\Yaml\Dumper as YmlDumper;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Tag\TaggedValue;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
@ -275,6 +277,8 @@ class YamlDumper extends Dumper
return $this->getParameterCall((string) $value);
} elseif ($value instanceof Expression) {
return $this->getExpressionCall((string) $value);
} elseif ($value instanceof Definition) {
return new TaggedValue('service', (new Parser())->parse("_:\n".$this->addService('_', $value), Yaml::PARSE_CUSTOM_TAGS)['_']['_']);
} elseif (is_object($value) || is_resource($value)) {
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\DependencyInjection\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Parser;
@ -64,6 +65,19 @@ class YamlDumperTest extends TestCase
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
}
public function testInlineServices()
{
$container = new ContainerBuilder();
$container->register('foo', 'Class1')
->addArgument((new Definition('Class2'))
->addArgument(new Definition('Class2'))
)
;
$dumper = new YamlDumper($container);
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_inline.yml', $dumper->dump());
}
private function assertEqualYamlStructure($expected, $yaml, $message = '')
{
$parser = new Parser();

View File

@ -0,0 +1,14 @@
services:
service_container:
class: Symfony\Component\DependencyInjection\ContainerInterface
synthetic: true
foo:
class: Class1
arguments: [!service { class: Class2, arguments: [!service { class: Class2 }] }]
Psr\Container\ContainerInterface:
alias: service_container
public: false
Symfony\Component\DependencyInjection\ContainerInterface:
alias: service_container
public: false