-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
Changes from 1 commit
15b9fb4
6930973
952b1db
aa82afc
0c99163
903eaac
cc2ac58
a8af3e6
bfb4aa9
34dc1e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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. | ||
|
@@ -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); | ||
$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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
); ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice idea! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In XML it should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed! |
||
</xsd:complexType> | ||
|
||
<xsd:complexType name="bind" mixed="true"> | ||
|
@@ -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"> | ||
|
@@ -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> | ||
|
||
|
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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shorter indeed