8000 feature #17323 [DependencyInjection] Deprecate unsupported attributes… · symfony/symfony@caae21c · GitHub
[go: up one dir, main page]

Skip to content

Commit caae21c

Browse files
committed
feature #17323 [DependencyInjection] Deprecate unsupported attributes/elements for alias (Ener-Getick)
This PR was squashed before being merged into the 3.1-dev branch (closes #17323). Discussion ---------- [DependencyInjection] Deprecate unsupported attributes/elements for alias | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #17256 | License | MIT Following #17133, this PR deprecates using unsupported keywords on alias definition. For example the following examples will trigger deprecations: ```xml <?xml version="1.0" encoding="utf-8"?> <container> <services> <service id="bar" alias="foo" class="Foo"> <tag name="foo.bar" /> </service> </services> </container> ``` ```yml services: bar: alias: foo parent: quz ``` Commits ------- 49eb65c [DependencyInjection] Deprecate unsupported attributes/elements for alias
2 parents a4f3baa + 49eb65c commit caae21c

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ private function parseDefinitions(\DOMDocument $xml, $file)
133133
private function parseDefinition(\DOMElement $service, $file)
134134
{
135135
if ($alias = $service->getAttribute('alias')) {
136+
$this->validateAlias($service, $file);
137+
136138
$public = true;
137139
if ($publicAttr = $service->getAttribute('public')) {
138140
$public = XmlUtils::phpize($publicAttr);
@@ -491,6 +493,27 @@ public function validateSchema(\DOMDocument $dom)
491493
return $valid;
492494
}
493495

496+
/**
497+
* Validates an alias.
498+
*
499+
* @param \DOMElement $alias
500+
* @param string $file
501+
*/
502+
private function validateAlias(\DOMElement $alias, $file)
503+
{
504+
foreach ($alias->attributes as $name => $node) {
505+
if (!in_array($name, array('alias', 'id', 'public'))) {
506+
@trigger_error(sprintf('Using the attribute "%s" is deprecated for alias definition "%s" in "%s". Allowed attributes are "alias", "id" and "public". The XmlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported attributes.', $name, $alias->getAttribute('id'), $file), E_USER_DEPRECATED);
507+
}
508+
}
509+
510+
foreach ($alias->childNodes as $child) {
511+
if ($child instanceof \DOMElement && $child->namespaceURI === self::NS) {
512+
@trigger_error(sprintf('Using the element "%s" is deprecated for alias definition "%s" in "%s". The XmlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported elements.', $child->localName, $alias->getAttribute('id'), $file), E_USER_DEPRECATED);
513+
}
514+
}
515+
}
516+
494517
/**
495518
* Validates an extension.
496519
*

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ private function parseDefinition($id, $service, $file)
178178
$public = !array_key_exists('public', $service) || (bool) $service['public'];
179179
$this->container->setAlias($id, new Alias($service['alias'], $public));
180180

181+
foreach ($service as $key => $value) {
182+
if (!in_array($key, array('alias', 'public'))) {
183+
@trigger_error(sprintf('The configuration key "%s" is unsupported for alias definition "%s" in "%s". Allowed configuration keys are "alias" and "public". The YamlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported attributes.', $key, $id, $file), E_USER_DEPRECATED);
184+
}
185+
}
186+
181187
return;
182188
}
183189

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 http://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<service id="foo" class="Foo" />
5+
6+
<service id="bar" alias="foo" class="Foo">
7+
<tag name="foo.bar" />
8+
<factory service="foobar" method="getBar" />
9+
</service>
10+
</services>
11+
</container>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
foo:
3+
alias: bar
4+
factory: foo
5+
parent: quz

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,4 +486,31 @@ public function testAutowire()
486486

487487
$this->assertTrue($container->getDefinition('bar')->isAutowired());
488488
}
489+
490+
/**
491+
* @group legacy
492+
*/
493+
public function testAliasDefinitionContainsUnsupportedElements()
494+
{
495+
$container = new ContainerBuilder();
496+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
497+
498+
$deprecations = array();
499+
set_error_handler(function ($type, $msg) use (&$deprecations) {
500+
if (E_USER_DEPRECATED === $type) {
501+
$deprecations[] = $msg;
502+
}
503+
});
504+
505+
$loader->load('legacy_invalid_alias_definition.xml');
506+
507+
$this->assertTrue($container->has('bar'));
508+
509+
$this->assertCount(3, $deprecations);
510+
$this->assertContains('Using the attribute "class" is deprecated for alias definition "bar"', $deprecations[0]);
511+
$this->assertContains('Using the element "tag" is deprecated for alias definition "bar"', $deprecations[1]);
512+
$this->assertContains('Using the element "factory" is deprecated for alias definition "bar"', $deprecations[2]);
513+
514+
restore_error_handler();
515+
}
489516
}

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,30 @@ public function testServiceDefinitionContainsUnsupportedKeywords()
303303
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
304304
$loader->load('legacy_invalid_definition.yml');
305305
}
306+
307+
/**
308+
* @group legacy
309+
*/
310+
public function testAliasDefinitionContainsUnsupportedKeywords()
311+
{
312+
$container = new ContainerBuilder();
313+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
314+
315+
$deprecations = array();
316+
set_error_handler(function ($type, $msg) use (&$deprecations) {
317+
if (E_USER_DEPRECATED === $type) {
318+
$deprecations[] = $msg;
319+
}
320+
});
321+
322+
$loader->load('legacy_invalid_alias_definition.yml');
323+
324+
$this->assertTrue($container->has('foo'));
325+
326+
$this->assertCount(2, $deprecations);
327+
$this->assertContains('The configuration key "factory" is unsupported for alias definition "foo"', $deprecations[0]);
328+
$this->assertContains('The configuration key "parent" is unsupported for alias definition "foo"', $deprecations[1]);
329+
330+
restore_error_handler();
331+
}
306332
}

0 commit comments

Comments
 (0)
0