From 4e0383a4d15ac093f0d49f366c855b1de21c7b1d Mon Sep 17 00:00:00 2001 From: Joost van Driel Date: Fri, 27 Oct 2017 15:58:53 +0200 Subject: [PATCH 1/8] Added support for deprecating an alias --- .../Component/DependencyInjection/Alias.php | 59 ++++++++++ .../DependencyInjection/Tests/AliasTest.php | 108 ++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/AliasTest.php diff --git a/src/Symfony/Component/DependencyInjection/Alias.php b/src/Symfony/Component/DependencyInjection/Alias.php index 55e385f674e48..caeb5ed150945 100644 --- a/src/Symfony/Component/DependencyInjection/Alias.php +++ b/src/Symfony/Component/DependencyInjection/Alias.php @@ -11,17 +11,24 @@ namespace Symfony\Component\DependencyInjection; +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; + class Alias { private $id; private $public; private $private; + private $deprecated; + private $deprecationTemplate; + + private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.'; public function __construct(string $id, bool $public = true) { $this->id = $id; $this->public = $public; $this->private = 2 > func_num_args(); + $this->deprecated = false; } /** @@ -78,6 +85,58 @@ public function isPrivate() return $this->private; } + /** + * Whether this alias is deprecated, that means it should not be called + * anymore. + * + * @param bool $status Defaults to true + * @param string $template Optional template message to use if the alias is deprecated + * + * @return $this + * + * @throws InvalidArgumentException when the message template is invalid + */ + public function setDeprecated($status = true, $template = null) + { + if (null !== $template) { + if (preg_match('#[\r\n]|\*/#', $template)) { + throw new InvalidArgumentException('Invalid characters found in deprecation template.'); + } + + if (false === strpos($template, '%service_id%')) { + throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.'); + } + + $this->deprecationTemplate = $template; + } + + $this->deprecated = (bool) $status; + + return $this; + } + + /** + * Returns whether this alias is deprecated + * + * @return bool + */ + public function isDeprecated() + { + return $this->deprecated; + } + + /** + * Message to use if this alias is deprecated. + * + * @param string $id Service id relying on this alias + * + * @return string + */ + public function getDeprecationMessage($id) + { + return str_replace('%service_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate); + } + /** * Returns the Id of this alias. * diff --git a/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php b/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php new file mode 100644 index 0000000000000..57730f959b163 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php @@ -0,0 +1,108 @@ +assertEquals('foo', (string)$alias); + $this->assertTrue($alias->isPublic()); + } + + public function testCanConstructANonPublicAlias() + { + $alias = new Alias('foo', false); + + $this->assertEquals('foo', (string)$alias); + $this->assertFalse($alias->isPublic()); + } + + public function testCanConstructAPrivateAlias() + { + $alias = new Alias('foo', false, false); + + $this->assertEquals('foo', (string)$alias); + $this->assertFalse($alias->isPublic()); + $this->assertFalse($alias->isPrivate()); + } + + public function testCanSetPublic() + { + $alias = new Alias('foo', false); + $alias->setPublic(true); + + $this->assertTrue($alias->isPublic()); + } + + public function testCanDeprecateAnAlias() + { + $alias = new Alias('foo', false); + $alias->setDeprecated(true, 'The %service_id% service is deprecated.'); + + $this->assertTrue($alias->isDeprecated()); + } + + public function testItHasADefaultDeprecationMessage() + { + $alias = new Alias('foo', false); + $alias->setDeprecated(); + + $expectedMessage = 'The "foo" service is deprecated. You should stop using it, as it will soon be removed.'; + $this->assertEquals($expectedMessage, $alias->getDeprecationMessage('foo')); + } + + public function testReturnsCorrectDeprecationMessage() + { + $alias = new Alias('foo', false); + $alias->setDeprecated(true, 'The "%service_id%" is deprecated.'); + + $expectedMessage = 'The "foo" is deprecated.'; + $this->assertEquals($expectedMessage, $alias->getDeprecationMessage('foo')); + } + + public function testCanOverrideDeprecation() + { + $alias = new Alias('foo', false); + $alias->setDeprecated(); + + $initial = $alias->isDeprecated(); + $alias->setDeprecated(false); + $final = $alias->isDeprecated(); + + $this->assertTrue($initial); + $this->assertFalse($final); + } + + /** + * @dataProvider invalidDeprecationMessageProvider + * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException + */ + public function testCannotDeprecateWithAnInvalidTemplate(string $message) + { + $def = new Alias('foo'); + $def->setDeprecated(true, $message); + } + + public function invalidDeprecationMessageProvider() + { + return array( + "With \rs" => array("invalid \r message %service_id%"), + "With \ns" => array("invalid \n message %service_id%"), + 'With */s' => array('invalid */ message %service_id%'), + 'message not containing require %service_id% variable' => array('this is deprecated'), + ); + } +} From dc3fb879a0811cb765aab26a93e6cda0902b1c99 Mon Sep 17 00:00:00 2001 From: Joost van Driel Date: Fri, 27 Oct 2017 16:00:57 +0200 Subject: [PATCH 2/8] Added support for deprecating an alias in the container builder --- .../DependencyInjection/ContainerBuilder.php | 4 ++++ .../Tests/ContainerBuilderTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index aecf7cb6f8a50..bb3bb2ed3f38c 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -543,6 +543,10 @@ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_ } if (!isset($this->definitions[$id]) && isset($this->aliasDefinitions[$id])) { + $aliasDefinition = $this->aliasDefinitions[$id]; + if ($aliasDefinition->isDeprecated()) { + @trigger_error($aliasDefinition->getDeprecationMessage($id), E_USER_DEPRECATED); + } return $this->doGet((string) $this->aliasDefinitions[$id], $invalidBehavior, $inlineServices); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index 1b719938a9d0f..ee04f5dc8ce26 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -259,6 +259,22 @@ public function testAliases() } } + /** + * @group legacy + * @expectedDeprecation The "foobar" service is deprecated. You should stop using it, as it will soon be removed. + */ + public function testDeprecatedAlias() + { + $builder = new ContainerBuilder(); + $builder->register('foo', 'stdClass'); + + $alias = new Alias('foo'); + $alias->setDeprecated(); + $builder->setAlias('foobar', $alias); + + $builder->get('foobar'); + } + public function testGetAliases() { $builder = new ContainerBuilder(); From 2d6181d0f0bea6e04eca278845db6d15e2c359ab Mon Sep 17 00:00:00 2001 From: Joost van Driel Date: Fri, 27 Oct 2017 16:01:22 +0200 Subject: [PATCH 3/8] Added deprecation of aliases to the xml file loader --- .../DependencyInjection/Loader/XmlFileLoader.php | 10 +++++++++- .../Fixtures/xml/deprecated_alias_definitions.xml | 13 +++++++++++++ .../Tests/Loader/XmlFileLoaderTest.php | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/deprecated_alias_definitions.xml diff --git a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php index 71ddd2227b605..81ff38e711348 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php @@ -211,6 +211,10 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults) $alias->setPublic($defaults['public']); } + if ($deprecated = $this->getChildren($service, 'deprecated')) { + $alias->setDeprecated(true, $deprecated[0]->nodeValue ?: null); + } + return; } @@ -637,8 +641,12 @@ private function validateAlias(\DOMElement $alias, $file) } } + $allowedTags = array('deprecated'); foreach ($alias->childNodes as $child) { - if ($child instanceof \DOMElement && self::NS === $child->namespaceURI) { + if (!$child instanceof \DOMElement && self::NS !== $child->namespaceURI) { + continue; + } + if (!in_array($child->localName, $allowedTags, true)) { throw new InvalidArgumentException(sprintf('Invalid child element "%s" defined for alias "%s" in "%s".', $child->localName, $alias->getAttribute('id'), $file)); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/deprecated_alias_definitions.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/deprecated_alias_definitions.xml new file mode 100644 index 0000000000000..17d6eb927bd88 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/deprecated_alias_definitions.xml @@ -0,0 +1,13 @@ + + + + + + + + + + The "%service_id%" service is deprecated. + + + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php index fb80cd49360fe..979b357feb00f 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php @@ -349,6 +349,21 @@ public function testDeprecated() $this->assertSame($message, $container->getDefinition('bar')->getDeprecationMessage('bar')); } + public function testDeprecatedAliases() + { + $container = new ContainerBuilder(); + $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath . '/xml')); + $loader->load('deprecated_alias_definitions.xml'); + + $this->assertTrue($container->getAlias('alias_for_foo')->isDeprecated()); + $message = 'The "alias_for_foo" service is deprecated. You should stop using it, as it will soon be removed.'; + $this->assertSame($message, $container->getAlias('alias_for_foo')->getDeprecationMessage('alias_for_foo')); + + $this->assertTrue($container->getAlias('alias_for_foobar')->isDeprecated()); + $message = 'The "alias_for_foobar" service is deprecated.'; + $this->assertSame($message, $container->getAlias('alias_for_foobar')->getDeprecationMessage('alias_for_foobar')); + } + public function testConvertDomElementToArray() { $doc = new \DOMDocument('1.0'); From d2781faf747e9eb4f7cab03e3180169bf5025c28 Mon Sep 17 00:00:00 2001 From: Joost van Driel Date: Fri, 27 Oct 2017 18:50:39 +0200 Subject: [PATCH 4/8] Removed string typehint --- src/Symfony/Component/DependencyInjection/Tests/AliasTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php b/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php index 57730f959b163..0a44b60da75b7 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php @@ -90,7 +90,7 @@ public function testCanOverrideDeprecation() * @dataProvider invalidDeprecationMessageProvider * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException */ - public function testCannotDeprecateWithAnInvalidTemplate(string $message) + public function testCannotDeprecateWithAnInvalidTemplate($message) { $def = new Alias('foo'); $def->setDeprecated(true, $message); From 70721a5573a608d41f52f841e2995568735d2b86 Mon Sep 17 00:00:00 2001 From: Joost van Driel Date: Fri, 27 Oct 2017 18:55:01 +0200 Subject: [PATCH 5/8] Applied coding standard review --- .../Component/DependencyInjection/Alias.php | 4 ++-- .../DependencyInjection/ContainerBuilder.php | 3 ++- .../DependencyInjection/Tests/AliasTest.php | 16 +++++++++------- .../Tests/Loader/XmlFileLoaderTest.php | 2 +- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Alias.php b/src/Symfony/Component/DependencyInjection/Alias.php index caeb5ed150945..85995d226df86 100644 --- a/src/Symfony/Component/DependencyInjection/Alias.php +++ b/src/Symfony/Component/DependencyInjection/Alias.php @@ -89,7 +89,7 @@ public function isPrivate() * Whether this alias is deprecated, that means it should not be called * anymore. * - * @param bool $status Defaults to true + * @param bool $status Defaults to true * @param string $template Optional template message to use if the alias is deprecated * * @return $this @@ -116,7 +116,7 @@ public function setDeprecated($status = true, $template = null) } /** - * Returns whether this alias is deprecated + * Returns whether this alias is deprecated. * * @return bool */ diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index bb3bb2ed3f38c..463774a03a16c 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -547,7 +547,8 @@ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_ if ($aliasDefinition->isDeprecated()) { @trigger_error($aliasDefinition->getDeprecationMessage($id), E_USER_DEPRECATED); } - return $this->doGet((string) $this->aliasDefinitions[$id], $invalidBehavior, $inlineServices); + + return $this->doGet((string) $aliasDefinition, $invalidBehavior, $inlineServices); } try { diff --git a/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php b/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php index 0a44b60da75b7..b17807b176539 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php @@ -1,11 +1,13 @@ * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -declare(strict_types=1); namespace Symfony\Component\DependencyInjection\Tests; @@ -18,7 +20,7 @@ public function testConstructor() { $alias = new Alias('foo'); - $this->assertEquals('foo', (string)$alias); + $this->assertEquals('foo', (string) $alias); $this->assertTrue($alias->isPublic()); } @@ -26,7 +28,7 @@ public function testCanConstructANonPublicAlias() { $alias = new Alias('foo', false); - $this->assertEquals('foo', (string)$alias); + $this->assertEquals('foo', (string) $alias); $this->assertFalse($alias->isPublic()); } @@ -34,7 +36,7 @@ public function testCanConstructAPrivateAlias() { $alias = new Alias('foo', false, false); - $this->assertEquals('foo', (string)$alias); + $this->assertEquals('foo', (string) $alias); $this->assertFalse($alias->isPublic()); $this->assertFalse($alias->isPrivate()); } @@ -54,7 +56,7 @@ public function testCanDeprecateAnAlias() $this->assertTrue($alias->isDeprecated()); } - + public function testItHasADefaultDeprecationMessage() { $alias = new Alias('foo', false); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php index 979b357feb00f..d404f0db5f6ee 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php @@ -352,7 +352,7 @@ public function testDeprecated() public function testDeprecatedAliases() { $container = new ContainerBuilder(); - $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath . '/xml')); + $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')); $loader->load('deprecated_alias_definitions.xml'); $this->assertTrue($container->getAlias('alias_for_foo')->isDeprecated()); From 34ce7bfa815b6621d6e42b7cc2ef8a8f7f8f0072 Mon Sep 17 00:00:00 2001 From: Joost van Driel Date: Sat, 28 Oct 2017 08:17:44 +0200 Subject: [PATCH 6/8] Fixed a type and made the deprecation template clearer --- src/Symfony/Component/DependencyInjection/Alias.php | 2 +- src/Symfony/Component/DependencyInjection/Tests/AliasTest.php | 4 ++-- .../DependencyInjection/Tests/ContainerBuilderTest.php | 2 +- .../Tests/Fixtures/xml/deprecated_alias_definitions.xml | 2 +- .../DependencyInjection/Tests/Loader/XmlFileLoaderTest.php | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Alias.php b/src/Symfony/Component/DependencyInjection/Alias.php index 85995d226df86..86fbf33e0ad69 100644 --- a/src/Symfony/Component/DependencyInjection/Alias.php +++ b/src/Symfony/Component/DependencyInjection/Alias.php @@ -21,7 +21,7 @@ class Alias private $deprecated; private $deprecationTemplate; - private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.'; + private static $defaultDeprecationTemplate = 'The "%service_id%" service alias is deprecated. You should stop using it, as it will soon be removed.'; public function __construct(string $id, bool $public = true) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php b/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php index b17807b176539..eb61a9df7d595 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/AliasTest.php @@ -62,7 +62,7 @@ public function testItHasADefaultDeprecationMessage() $alias = new Alias('foo', false); $alias->setDeprecated(); - $expectedMessage = 'The "foo" service is deprecated. You should stop using it, as it will soon be removed.'; + $expectedMessage = 'The "foo" service alias is deprecated. You should stop using it, as it will soon be removed.'; $this->assertEquals($expectedMessage, $alias->getDeprecationMessage('foo')); } @@ -104,7 +104,7 @@ public function invalidDeprecationMessageProvider() "With \rs" => array("invalid \r message %service_id%"), "With \ns" => array("invalid \n message %service_id%"), 'With */s' => array('invalid */ message %service_id%'), - 'message not containing require %service_id% variable' => array('this is deprecated'), + 'message not containing required %service_id% variable' => array('this is deprecated'), ); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index ee04f5dc8ce26..57240c4585d4e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -261,7 +261,7 @@ public function testAliases() /** * @group legacy - * @expectedDeprecation The "foobar" service is deprecated. You should stop using it, as it will soon be removed. + * @expectedDeprecation The "foobar" service alias is deprecated. You should stop using it, as it will soon be removed. */ public function testDeprecatedAlias() { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/deprecated_alias_definitions.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/deprecated_alias_definitions.xml index 17d6eb927bd88..83ceeefa9c163 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/deprecated_alias_definitions.xml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/deprecated_alias_definitions.xml @@ -7,7 +7,7 @@ - The "%service_id%" service is deprecated. + The "%service_id%" service alias is deprecated. diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php index d404f0db5f6ee..ee512bdd0c8d5 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php @@ -356,11 +356,11 @@ public function testDeprecatedAliases() $loader->load('deprecated_alias_definitions.xml'); $this->assertTrue($container->getAlias('alias_for_foo')->isDeprecated()); - $message = 'The "alias_for_foo" service is deprecated. You should stop using it, as it will soon be removed.'; + $message = 'The "alias_for_foo" service alias is deprecated. You should stop using it, as it will soon be removed.'; $this->assertSame($message, $container->getAlias('alias_for_foo')->getDeprecationMessage('alias_for_foo')); $this->assertTrue($container->getAlias('alias_for_foobar')->isDeprecated()); - $message = 'The "alias_for_foobar" service is deprecated.'; + $message = 'The "alias_for_foobar" service alias is deprecated.'; $this->assertSame($message, $container->getAlias('alias_for_foobar')->getDeprecationMessage('alias_for_foobar')); } From 16a8676d8a2b87890bd5b2dae019038ccf4bb1fc Mon Sep 17 00:00:00 2001 From: Joost van Driel Date: Sat, 28 Oct 2017 08:18:07 +0200 Subject: [PATCH 7/8] Added deprecation of aliases to the yaml file loader --- .../DependencyInjection/Loader/YamlFileLoader.php | 7 ++++++- .../Fixtures/yaml/deprecated_alias_definitions.yml | 4 ++++ .../Tests/Loader/YamlFileLoaderTest.php | 11 +++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/deprecated_alias_definitions.yml diff --git a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php index 7a0cccb9c9923..cc896a5cd3c7e 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php @@ -346,8 +346,13 @@ private function parseDefinition($id, $service, $file, array $defaults) } foreach ($service as $key => $value) { - if (!in_array($key, array('alias', 'public'))) { + if (!in_array($key, array('alias', 'public', 'deprecated'))) { throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for the service "%s" which is defined as an alias in "%s". Allowed configuration keys for service aliases are "alias" and "public".', $key, $id, $file)); + continue; + } + + if ('deprecated' === $key) { + $alias->setDeprecated(true, $value); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/deprecated_alias_definitions.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/deprecated_alias_definitions.yml new file mode 100644 index 0000000000000..2223597815f6d --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/deprecated_alias_definitions.yml @@ -0,0 +1,4 @@ +services: + alias_for_foobar: + alias: foobar + deprecated: The "%service_id%" service alias is deprecated. diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php index 73074aed88037..cf59f7b319c48 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php @@ -174,6 +174,17 @@ public function testLoadServices() $this->assertEquals(array('decorated', 'decorated.pif-pouf', 5), $services['decorator_service_with_name_and_priority']->getDecoratedService()); } + public function testDeprecatedAliases() + { + $container = new ContainerBuilder(); + $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')); + $loader->load('deprecated_alias_definitions.yml'); + + $this->assertTrue($container->getAlias('alias_for_foobar')->isDeprecated()); + $message = 'The "alias_for_foobar" service alias is deprecated.'; + $this->assertSame($message, $container->getAlias('alias_for_foobar')->getDeprecationMessage('alias_for_foobar')); + } + public function testLoadFactoryShortSyntax() { $container = new ContainerBuilder(); From 3bffe611a0bb6820601996b07a871b66b92b0396 Mon Sep 17 00:00:00 2001 From: Joost van Driel Date: Sat, 28 Oct 2017 08:48:07 +0200 Subject: [PATCH 8/8] Added deprecating aliases to the various container files --- .../Tests/Fixtures/config/services9.php | 3 +++ .../Tests/Fixtures/containers/container9.php | 5 +++++ .../Tests/Fixtures/graphviz/services9.dot | 1 + .../Tests/Fixtures/php/services9_as_files.txt | 12 ++++++++++++ .../Tests/Fixtures/php/services9_compiled.php | 15 +++++++++++++++ .../Tests/Fixtures/xml/services9.xml | 3 +++ .../Tests/Fixtures/yaml/services9.yml | 4 ++++ 7 files changed, 43 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services9.php index 4bf3b89d8e3e0..a89851723982d 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services9.php @@ -88,6 +88,9 @@ $s->set('deprecated_service', 'stdClass') ->deprecate(); + $s->set('deprecated_service_alias', 'stdClass') + ->deprecate('The "%service_id%" service alias is deprecated.'); + $s->set('new_factory', 'FactoryClass') ->property('foo', 'bar') ->private(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php index 06789bd350fe1..7122cc13c1263 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php @@ -118,6 +118,11 @@ ->setDeprecated(true) ->setPublic(true) ; +$container + ->register('deprecated_service_alias', 'stdClass') + ->setDeprecated(true, 'The "%service_id%" service alias is deprecated.') + ->setPublic(true) +; $container ->register('new_factory', 'FactoryClass') ->setProperty('foo', 'bar') diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot index 2c116979e4b6f..b8ff10e6a60ed 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot @@ -21,6 +21,7 @@ digraph sc { node_decorator_service [label="decorator_service\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"]; node_decorator_service_with_name [label="decorator_service_with_name\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"]; node_deprecated_service [label="deprecated_service\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"]; + node_deprecated_service_alias [label="deprecated_service_alias\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"]; node_new_factory [label="new_factory\nFactoryClass\n", shape=record, fillcolor="#eeeeee", style="filled"]; node_factory_service [label="factory_service\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"]; node_new_factory_service [label="new_factory_service\nFooBarBaz\n", shape=record, fillcolor="#eeeeee", style="filled"]; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt index 5a3b5c888fb37..c456c38f31612 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt @@ -117,6 +117,17 @@ use Symfony\Component\DependencyInjection\Argument\RewindableGenerator; return $this->services['deprecated_service'] = new \stdClass(); + [Container%s/getDeprecatedServiceAliasService.php] => services['deprecated_service_alias'] = new \stdClass(); + [Container%s/getFactoryServiceService.php] => __DIR__.'/getDecoratorServiceService.php', 'decorator_service_with_name' => __DIR__.'/getDecoratorServiceWithNameService.php', 'deprecated_service' => __DIR__.'/getDeprecatedServiceService.php', + 'deprecated_service_alias' => __DIR__.'/getDeprecatedServiceAliasService.php', 'factory_service' => __DIR__.'/getFactoryServiceService.php', 'factory_service_simple' => __DIR__.'/getFactoryServiceSimpleService.php', 'foo' => __DIR__.'/getFooService.php', diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index a45456163f796..a99d322f7bf80 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -43,6 +43,7 @@ public function __construct() 'decorator_service' => 'getDecoratorServiceService', 'decorator_service_with_name' => 'getDecoratorServiceWithNameService', 'deprecated_service' => 'getDeprecatedServiceService', + 'deprecated_service_alias' => 'getDeprecatedServiceAliasService', 'factory_service' => 'getFactoryServiceService', 'factory_service_simple' => 'getFactoryServiceSimpleService', 'foo' => 'getFooService', @@ -224,6 +225,20 @@ protected function getDeprecatedServiceService() return $this->services['deprecated_service'] = new \stdClass(); } + /** + * Gets the public 'deprecated_service_alias' shared service. + * + * @return \stdClass + * + * @deprecated The "deprecated_service_alias" service alias is deprecated. + */ + protected function getDeprecatedServiceAliasService() + { + @trigger_error('The "deprecated_service_alias" service alias is deprecated.', E_USER_DEPRECATED); + + return $this->services['deprecated_service_alias'] = new \stdClass(); + } + /** * Gets the public 'factory_service' shared service. * diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml index 3848a83dbd463..2c5225b33e10c 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml @@ -99,6 +99,9 @@ The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed. + + The "%service_id%" service alias is deprecated. + bar diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml index dbe59ed9e56a2..c2b19d5ec97f4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml @@ -105,6 +105,10 @@ services: class: stdClass deprecated: The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed. public: true + deprecated_service_alias: + class: stdClass + deprecated: The "%service_id%" service alias is deprecated. + public: true new_factory: class: FactoryClass public: false