8000 [DI][Yaml] Add the !yaml_file YAML tag to inject parsed YAML in a service by GaryPEGEOT · Pull Request #30446 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI][Yaml] Add the !yaml_file YAML tag to inject parsed YAML in a service #30446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow parameters resolution
  • Loading branch information
GaryPEGEOT committed Mar 23, 2019
commit 903eaacd05d16304750ae4be22d09b6a86238079
Original file line number Diff line number Diff line change
Expand Up @@ -759,14 +759,18 @@ private function resolveServices($value, $file, $isParameter = false)
return new Reference($id);
}
if ('yaml_file' === $value->getTag()) {
$filePath = $this->container->getParameterBag()->resolveValue((string) $argument);
$rootDir = \dirname($file);
$file = \is_file($argument) ? $argument : "$rootDir/$argument";

if (!\is_file($file)) {
if (!\is_file($filePath)) {
$filePath = "$rootDir/$filePath";
}

if (!\is_file($filePath)) {
throw new InvalidArgumentException("Unable to locate file \"$argument\". Please provide a path relative to \"$rootDir\" or an absolute path.");
}

return $this->yamlParser->parseFile($file, Yaml::PARSE_CONSTANT);
return $this->yamlParser->parseFile($filePath, Yaml::PARSE_CONSTANT);
}

throw new InvalidArgumentException(sprintf('Unsupported tag "!%s".', $value->getTag()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo:
bar: true
baz: 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
my_awesome_service_with_yaml_inside:
arguments:
- !yaml_file '%fixture_path%/nested_test_yaml_file.yaml'
Original file line number Diff line number Diff line change
Expand Up @@ -803,17 +803,30 @@ public function testFqcnLazyProxy()
$this->assertSame([['interface' => 'SomeInterface']], $definition->getTag('proxy'));
}

public function testYamlFileTag()
/**
* @dataProvider provideYamlFileResources
*/
public function testYamlFileTag(string $resource)
{
$path = self::$fixturesPath.'/yaml';
$container = new ContainerBuilder();
$container->setParameter('fixture_path', "$path/baz");

$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services_with_yaml_file_tag.yml');
$loader = new YamlFileLoader($container, new FileLocator($path));
$loader->load($resource);

$definition = $container->getDefinition('my_awesome_service_with_yaml_inside');
$this->assertSame(['foo' => ['bar' => true, 'baz' => 42]], $definition->getArgument(0));
}

public function provideYamlFileResources(): array
{
return [
'Relative path' => ['services_with_yaml_file_tag.yml'],
'Path with parameter' => ['services_with_yaml_file_tag_with_params.yml'],
];
}

public function testYamlFileTagNotFound()
{
$rootDir = self::$fixturesPath.'/yaml';
Expand Down
0