Skip to content

[DependencyInjection] Add support for key-type in XmlFileLoader #58035

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

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Deprecate `!tagged` tag, use `!tagged_iterator` instead
* Add a `ContainerBuilder::registerChild()` shortcut method for registering child definitions
* Add support for `key-type` in `XmlFileLoader`

7.1
---
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,21 @@ private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file
$key = $arg->getAttribute('key');
}

switch ($arg->getAttribute('key-type')) {
case 'binary':
if (false === $key = base64_decode($key, true)) {
throw new InvalidArgumentException(\sprintf('Tag "<%s>" with key-type="binary" does not have a valid base64 encoded key in "%s".', $name, $file));
}
break;
case 'constant':
try {
$key = \constant(trim($key));
} catch (\Error) {
throw new InvalidArgumentException(\sprintf('The key "%s" is not a valid constant in "%s".', $key, $file));
}
break;
}

$trim = $arg->hasAttribute('trim') && XmlUtils::phpize($arg->getAttribute('trim'));
$onInvalid = $arg->getAttribute('on-invalid');
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
<xsd:element name="property" type="property" maxOccurs="unbounded" />
<xsd:element name="service" type="service" />
</xsd:choice>
<xsd:attribute name="key-type" type="key_type" />
<xsd:attribute name="type" type="argument_type" />
<xsd:attribute name="id" type="xsd:string" />
<xsd:attribute name="key" type="xsd:string" />
Expand Down Expand Up @@ -315,6 +316,7 @@
<xsd:element name="service" type="service" />
<xsd:element name="exclude" type="xsd:string" maxOccurs="unbounded" />
</xsd:choice>
<xsd:attribute name="key-type" type="key_type" />
<xsd:attribute name="type" type="argument_type" />
<xsd:attribute name="id" type="xsd:string" />
<xsd:attribute name="key" type="xsd:string" />
Expand Down Expand Up @@ -365,6 +367,13 @@
</xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="key_type">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="binary" />
<xsd:enumeration value="constant" />
</xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="ignore_errors">
<xsd:restriction base="xsd:string">
<xsd:pattern value="(true|false|not_found)" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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 id="foo" class="Symfony\Component\DependencyInjection\Tests\Fixtures\Bar">
<argument type="collection">
<argument key-type="constant" key="PHP_INT_MAX">Value 1</argument>
<argument key="PHP_INT_MAX">Value 2</argument>
<argument key-type="binary" key="AQID">Value 3</argument>
</argument>
</service>
</services>
</container>
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 id="foo" class="Symfony\Component\DependencyInjection\Tests\Fixtures\Bar">
<property key-type="binary" key="My_Key">Value 3</property>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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 id="foo" class="Symfony\Component\DependencyInjection\Tests\Fixtures\Bar">
<property type="collection" key="quz">
<property key-type="constant" key="PHP_INT_MAX">Value 1</property>
<property key="PHP_INT_MAX">Value 2</property>
<property key-type="binary" key="AQID">Value 3</property>
</property>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?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 id="foo" class="Symfony\Component\DependencyInjection\Tests\Fixtures\Bar">
<property type="collection" key="quz">
<property key-type="constant" key="PHP_Unknown_CONST">Value 1</property>
</property>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,54 @@ public function testStaticConstructorWithFactoryThrows()
$loader->load('static_constructor_and_factory.xml');
}

public function testArgumentKeyType()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('key_type_argument.xml');

$definition = $container->getDefinition('foo');
$this->assertSame([
\PHP_INT_MAX => 'Value 1',
'PHP_INT_MAX' => 'Value 2',
"\x01\x02\x03" => 'Value 3',
], $definition->getArgument(0));
}

public function testPropertyKeyType()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('key_type_property.xml');

$definition = $container->getDefinition('foo');
$this->assertSame([
\PHP_INT_MAX => 'Value 1',
'PHP_INT_MAX' => 'Value 2',
"\x01\x02\x03" => 'Value 3',
], $definition->getProperties()['quz']);
}

public function testInvalidBinaryKeyType()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf('Tag "<property>" with key-type="binary" does not have a valid base64 encoded key in "%s".', self::$fixturesPath.'/xml/key_type_incorrect_bin.xml'));
$loader->load('key_type_incorrect_bin.xml');
}

public function testUnknownConstantAsKey()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf('The key "PHP_Unknown_CONST" is not a valid constant in "%s".', self::$fixturesPath.'/xml/key_type_wrong_constant.xml'));
$loader->load('key_type_wrong_constant.xml');
}

/**
* @group legacy
*/
Expand Down
Loading