8000 feature #23855 [DI] Allow dumping inline services in Yaml (nicolas-gr… · symfony/symfony@00897c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 00897c0

Browse files
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
2 parents d8584e5 + e5bbf3f commit 00897c0

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespace Symfony\Component\DependencyInjection\Dumper;
1313

1414
use Symfony\Component\Yaml\Dumper as YmlDumper;
15+
use Symfony\Component\Yaml\Parser;
1516
use Symfony\Component\Yaml\Tag\TaggedValue;
17+
use Symfony\Component\Yaml\Yaml;
1618
use Symfony\Component\DependencyInjection\Alias;
1719
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
1820
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
@@ -275,6 +277,8 @@ private function dumpValue($value)
275277
return $this->getParameterCall((string) $value);
276278
} elseif ($value instanceof Expression) {
277279
return $this->getExpressionCall((string) $value);
280+
} elseif ($value instanceof Definition) {
281+
return new TaggedValue('service', (new Parser())->parse("_:\n".$this->addService('_', $value), Yaml::PARSE_CUSTOM_TAGS)['_']['_']);
278282
} elseif (is_object($value) || is_resource($value)) {
279283
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
280284
}

src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
1617
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
1718
use Symfony\Component\Yaml\Yaml;
1819
use Symfony\Component\Yaml\Parser;
@@ -64,6 +65,19 @@ public function testDumpAutowireData()
6465
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
6566
}
6667

68+
public function testInlineServices()
69+
{
70+
$container = new ContainerBuilder();
71+
$container->register('foo', 'Class1')
72+
->addArgument((new Definition('Class2'))
73+
->addArgument(new Definition('Class2'))
74+
)
75+
;
76+
77+
$dumper = new YamlDumper($container);
78+
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_inline.yml', $dumper->dump());
79+
}
80+
6781
private function assertEqualYamlStructure($expected, $yaml, $message = '')
6882
{
6983
$parser = new Parser();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
services:
3+
service_container:
4+
class: Symfony\Component\DependencyInjection\ContainerInterface
5+
synthetic: true
6+
foo:
7+
class: Class1
8+
arguments: [!service { class: Class2, arguments: [!service { class: Class2 }] }]
9+
Psr\Container\ContainerInterface:
10+
alias: service_container
11+
public: false
12+
Symfony\Component\DependencyInjection\ContainerInterface:
13+
alias: service_container
14+
public: false

0 commit comments

Comments
 (0)
0