Skip to content

Commit e362450

Browse files
[DependencyInjection] Add support for key-type in XmlFileLoader
1 parent 3cc6dd9 commit e362450

File tree

8 files changed

+115
-0
lines changed

8 files changed

+115
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

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

1011
7.1
1112
---

Loader/XmlFileLoader.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,21 @@ private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file
534534
$key = $arg->getAttribute('key');
535535
}
536536

537+
switch ($arg->getAttribute('key-type')) {
538+
case 'binary':
539+
if (false === $key = base64_decode($key, true)) {
540+
throw new InvalidArgumentException(\sprintf('Tag "<%s>" with key-type="binary" does not have a valid base64 encoded key in "%s".', $name, $file));
541+
}
542+
break;
543+
case 'constant':
544+
try {
545+
$key = \constant(trim($key));
546+
} catch (\Error) {
547+
throw new InvalidArgumentException(\sprintf('The key "%s" is not a valid constant in "%s".', $key, $file));
548+
}
549+
break;
550+
}
551+
537552
$trim = $arg->hasAttribute('trim') && XmlUtils::phpize($arg->getAttribute('trim'));
538553
$onInvalid = $arg->getAttribute('on-invalid');
539554
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;

Loader/schema/dic/services/services-1.0.xsd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
<xsd:element name="property" type="property" maxOccurs="unbounded" />
271271
<xsd:element name="service" type="service" />
272272
</xsd:choice>
273+
<xsd:attribute name="key-type" type="key_type" />
273274
<xsd:attribute name="type" type="argument_type" />
274275
<xsd:attribute name="id" type="xsd:string" />
275276
<xsd:attribute name="key" type="xsd:string" />
@@ -315,6 +316,7 @@
315316
<xsd:element name="service" type="service" />
316317
<xsd:element name="exclude" type="xsd:string" maxOccurs="unbounded" />
317318
</xsd:choice>
319+
<xsd:attribute name="key-type" type="key_type" />
318320
<xsd:attribute name="type" type="argument_type" />
319321
<xsd:attribute name="id" type="xsd:string" />
320322
<xsd:attribute name="key" type="xsd:string" />
@@ -365,6 +367,13 @@
365367
</xsd:restriction>
366368
</xsd:simpleType>
367369

370+
<xsd:simpleType name="key_type">
371+
<xsd:restriction base="xsd:string">
372+
<xsd:enumeration value="binary" />
373+
<xsd:enumeration value="constant" />
374+
</xsd:restriction>
375+
</xsd:simpleType>
376+
368377
<xsd:simpleType name="ignore_errors">
369378
<xsd:restriction base="xsd:string">
370379
<xsd:pattern value="(true|false|not_found)" />
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<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">
3+
<services>
4+
<service id="foo" class="Symfony\Component\DependencyInjection\Tests\Fixtures\Bar">
5+
<argument type="collection">
6+
<argument key-type="constant" key="PHP_INT_MAX">Value 1</argument>
7+
<argument key="PHP_INT_MAX">Value 2</argument>
8+
<argument key-type="binary" key="AQID">Value 3</argument>
9+
</argument>
10+
</service>
11+
</services>
12+
</container>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<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">
3+
<services>
4+
<service id="foo" class="Symfony\Component\DependencyInjection\Tests\Fixtures\Bar">
5+
<property key-type="binary" key="My_Key">Value 3</property>
6+
</service>
7+
</services>
8+
</container>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<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">
3+
<services>
4+
<service id="foo" class="Symfony\Component\DependencyInjection\Tests\Fixtures\Bar">
5+
<property type="collection" key="quz">
6+
<property key-type="constant" key="PHP_INT_MAX">Value 1</property>
7+
<property key="PHP_INT_MAX">Value 2</property>
8+
<property key-type="binary" key="AQID">Value 3</property>
9+
</property>
10+
</service>
11+
</services>
12+
</container>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<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">
3+
<services>
4+
<service id="foo" class="Symfony\Component\DependencyInjection\Tests\Fixtures\Bar">
5+
<property type="collection" key="quz">
6+
<property key-type="constant" key="PHP_Unknown_CONST">Value 1</property>
7+
</property>
8+
</service>
9+
</services>
10+
</container>

Tests/Loader/XmlFileLoaderTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,54 @@ public function testStaticConstructorWithFactoryThrows()
12801280
$loader->load('static_constructor_and_factory.xml');
12811281
}
12821282

1283+
public function testArgumentKeyType()
1284+
{
1285+
$container = new ContainerBuilder();
1286+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
1287+
$loader->load('key_type_argument.xml');
1288+
1289+
$definition = $container->getDefinition('foo');
1290+
$this->assertSame([
1291+
\PHP_INT_MAX => 'Value 1',
1292+
'PHP_INT_MAX' => 'Value 2',
1293+
"\x01\x02\x03" => 'Value 3',
1294+
], $definition->getArgument(0));
1295+
}
1296+
1297+
public function testPropertyKeyType()
1298+
{
1299+
$container = new ContainerBuilder();
1300+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
1301+
$loader->load('key_type_property.xml');
1302+
1303+
$definition = $container->getDefinition('foo');
1304+
$this->assertSame([
1305+
\PHP_INT_MAX => 'Value 1',
1306+
'PHP_INT_MAX' => 'Value 2',
1307+
"\x01\x02\x03" => 'Value 3',
1308+
], $definition->getProperties()['quz']);
1309+
}
1310+
1311+
public function testInvalidBinaryKeyType()
1312+
{
1313+
$container = new ContainerBuilder();
1314+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
1315+
1316+
$this->expectException(InvalidArgumentException::class);
1317+
$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'));
1318+
$loader->load('key_type_incorrect_bin.xml');
1319+
}
1320+
1321+
public function testUnknownConstantAsKey()
1322+
{
1323+
$container = new ContainerBuilder();
1324+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
1325+
1326+
$this->expectException(InvalidArgumentException::class);
1327+
$this->expectExceptionMessage(\sprintf('The key "PHP_Unknown_CONST" is not a valid constant in "%s".', self::$fixturesPath.'/xml/key_type_wrong_constant.xml'));
1328+
$loader->load('key_type_wrong_constant.xml');
1329+
}
1330+
12831331
/**
12841332
* @group legacy
12851333
*/

0 commit comments

Comments
 (0)