[DI] Allow dumping inline services in Yaml
This commit is contained in:
parent
fd16993a37
commit
e5bbf3f4f3
@ -12,7 +12,9 @@
|
|||||||
namespace Symfony\Component\DependencyInjection\Dumper;
|
namespace Symfony\Component\DependencyInjection\Dumper;
|
||||||
|
|
||||||
use Symfony\Component\Yaml\Dumper as YmlDumper;
|
use Symfony\Component\Yaml\Dumper as YmlDumper;
|
||||||
|
use Symfony\Component\Yaml\Parser;
|
||||||
use Symfony\Component\Yaml\Tag\TaggedValue;
|
use Symfony\Component\Yaml\Tag\TaggedValue;
|
||||||
|
use Symfony\Component\Yaml\Yaml;
|
||||||
use Symfony\Component\DependencyInjection\Alias;
|
use Symfony\Component\DependencyInjection\Alias;
|
||||||
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
|
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
|
||||||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
|
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
|
||||||
@ -275,6 +277,8 @@ class YamlDumper extends Dumper
|
|||||||
return $this->getParameterCall((string) $value);
|
return $this->getParameterCall((string) $value);
|
||||||
} elseif ($value instanceof Expression) {
|
} elseif ($value instanceof Expression) {
|
||||||
return $this->getExpressionCall((string) $value);
|
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)) {
|
} 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.');
|
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ namespace Symfony\Component\DependencyInjection\Tests\Dumper;
|
|||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
use Symfony\Component\DependencyInjection\Definition;
|
||||||
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
|
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
|
||||||
use Symfony\Component\Yaml\Yaml;
|
use Symfony\Component\Yaml\Yaml;
|
||||||
use Symfony\Component\Yaml\Parser;
|
use Symfony\Component\Yaml\Parser;
|
||||||
@ -64,6 +65,19 @@ class YamlDumperTest extends TestCase
|
|||||||
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
|
$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 = '')
|
private function assertEqualYamlStructure($expected, $yaml, $message = '')
|
||||||
{
|
{
|
||||||
$parser = new Parser();
|
$parser = new Parser();
|
||||||
|
@ -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
|
Reference in New Issue
Block a user