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
Add support for XML
  • Loading branch information
GaryPEGEOT committed Mar 23, 2019
commit a8af3e6146f198c1fa432fcf4d72c1e6177a74cc
22 changes: 22 additions & 0 deletions 8000 src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\DependencyInjection\Loader;

use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Util\XmlUtils;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
Expand All @@ -26,6 +27,7 @@
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\Yaml\Yaml;

/**
* XmlFileLoader loads XML files service definitions.
Expand Down Expand Up @@ -560,6 +562,26 @@ private function getArgumentsAsPhp(\DOMElement $node, $name, $file, $lowercase =
case 'constant':
$arguments[$key] = \constant(trim($arg->nodeValue));
break;
case 'yaml_file':
if (!\class_exists('Symfony\Component\Yaml\Yaml')) {
throw new \InvalidArgumentException('You need to install the YAML component to parse YAML files.');
}

$filePath = $this->container->getParameterBag()->resolveValue((string) $arg->nodeValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!\is_file($filePath = $this->container->getParameterBag()->resolveValue((string) $arg->nodeValue))) {
    $rootDir = \dirname($file);

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shorter indeed

$rootDir = \dirname($file);

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

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

$this->container->addResource(new FileResource($filePath));

$arguments[$key] = Yaml::parseFile($filePath, Yaml::PARSE_CONSTANT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about

$arguments[$key] = $this->container->getParameterBag()->resolveValue(
    Yaml::parseFile($filePath, Yaml::PARSE_CONSTANT)
);

?
That would allow using nested parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed. Parameters in arrays are already resolved, and at a better time (late enough that parameter overrides are already applied)

break;
default:
$arguments[$key] = XmlUtils::phpize($arg->nodeValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="on-invalid" type="invalid_sequence" />
<xsd:attribute name="tag" type="xsd:string" />
<xsd:attribute name="yaml_file" type="xsd:string" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In XML it should be yaml-file (same below)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!

</xsd:complexType>

<xsd:complexType name="bind" mixed="true">
Expand Down Expand Up @@ -236,6 +237,7 @@
<xsd:attribute name="tag" type="xsd:string" />
<xsd:attribute name="index-by" type="xsd:string" />
<xsd:attribute name="default-index-method" type="xsd:string" />
<xsd:attribute name="yaml_file" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="call">
Expand Down Expand Up @@ -265,6 +267,7 @@
<xsd:enumeration value="service_locator" />
<xsd:enumeration value="tagged" />
<xsd:enumeration value="tagged_locator" />
<xsd:enumeration value="yaml_file" />
</xsd:restriction>
</xsd:simpleType>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service class="foo" id="my_awesome_service_with_yaml_inside">
<argument type="yaml_file">%fixture_path%/test_yaml_file.yaml</argument>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -897,4 +897,16 @@ public function testTsantosContainer()
$dump = $dumper->dump();
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_tsantos.php', $dumper->dump());
}

public function testYamlFileTag()
{
$container = new ContainerBuilder();
$container->setParameter('fixture_path', realpath(self::$fixturesPath.'/yaml'));

$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services_with_yaml_file_argument.xml');

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