Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 49eb65c

Browse filesBrowse files
GuilhemNfabpot
authored andcommitted
[DependencyInjection] Deprecate unsupported attributes/elements for alias
1 parent 307ad07 commit 49eb65c
Copy full SHA for 49eb65c

File tree

6 files changed

+98
-0
lines changed
Filter options

6 files changed

+98
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ private function parseDefinitions(\DOMDocument $xml, $file)
132132
private function parseDefinition(\DOMElement $service, $file)
133133
{
134134
if ($alias = $service->getAttribute('alias')) {
135+
$this->validateAlias($service, $file);
136+
135137
$public = true;
136138
if ($publicAttr = $service->getAttribute('public')) {
137139
$public = XmlUtils::phpize($publicAttr);
@@ -490,6 +492,27 @@ public function validateSchema(\DOMDocument $dom)
490492
return $valid;
491493
}
492494

495+
/**
496+
* Validates an alias.
497+
*
498+
* @param \DOMElement $alias
499+
* @param string $file
500+
*/
501+
private function validateAlias(\DOMElement $alias, $file)
502+
{
503+
foreach ($alias->attributes as $name => $node) {
504+
if (!in_array($name, array('alias', 'id', 'public'))) {
505+
@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);
506+
}
507+
}
508+
509+
foreach ($alias->childNodes as $child) {
510+
if ($child instanceof \DOMElement && $child->namespaceURI === self::NS) {
511+
@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);
512+
}
513+
}
514+
}
515+
493516
/**
494517
* Validates an extension.
495518
*

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ private function parseDefinition($id, $service, $file)
177177
$public = !array_key_exists('public', $service) || (bool) $service['public'];
178178
$this->container->setAlias($id, new Alias($service['alias'], $public));
179179

180+
foreach ($service as $key => $value) {
181+
if (!in_array($key, array('alias', 'public'))) {
182+
@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);
183+
}
184+
}
185+
180186
return;
181187
}
182188

+11Lines 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>
+5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php
+27Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php
+26Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.