Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DI] Added support for deprecating aliases #29968

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 2 commits into from
Jan 25, 2019
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
47 changes: 47 additions & 0 deletions src/Symfony/Component/DependencyInjection/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 alias is deprecated. You should stop using it, as it will soon be removed.';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this PR is merged, but I've just reviewed its code and I wondered if the %service_id% placeholder name is confusing to other people too. To me, this placeholder would be easier to understand if it was called %alias_id%.

Also, this -> ... as it will soon be removed. could be -> ... as it will be removed in the future.

Thanks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in #29995, please review


public function __construct(string $id, bool $public = true)
{
$this->id = $id;
$this->public = $public;
$this->private = 2 > \func_num_args();
$this->deprecated = false;
}

/**
Expand Down Expand Up @@ -78,6 +85,46 @@ public function isPrivate()
return $this->private;
}

/**
* Whether this alias is deprecated, that means it should not be referenced
* anymore.
*
* @param bool $status Whether this alias is deprecated, 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;
}

public function isDeprecated(): bool
{
return $this->deprecated;
}

public function getDeprecationMessage(string $id): string
{
return str_replace('%service_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
}

/**
* Returns the Id of this alias.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function process(ContainerBuilder $container)

foreach ($container->getAliases() as $id => $alias) {
$aliasId = (string) $alias;

if ($aliasId !== $defId = $this->getDefinitionId($aliasId, $container)) {
$container->setAlias($id, $defId)->setPublic($alias->isPublic())->setPrivate($alias->isPrivate());
}
Expand Down Expand Up @@ -60,8 +61,15 @@ private function getDefinitionId(string $id, ContainerBuilder $container): strin
if (isset($seen[$id])) {
throw new ServiceCircularReferenceException($id, array_merge(array_keys($seen), [$id]));
}

$seen[$id] = true;
$id = (string) $container->getAlias($id);
$alias = $container->getAlias($id);

if ($alias->isDeprecated()) {
@trigger_error($alias->getDeprecationMessage($id), E_USER_DEPRECATED);
}

$id = (string) $alias;
}

return $id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,13 @@ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_
}

if (!isset($this->definitions[$id]) && isset($this->aliasDefinitions[$id])) {
return $this->doGet((string) $this->aliasDefinitions[$id], $invalidBehavior, $inlineServices, $isConstructorArgument);
$alias = $this->aliasDefinitions[$id];

if ($alias->isDeprecated()) {
@trigger_error($alias->getDeprecationMessage($id), E_USER_DEPRECATED);
}

return $this->doGet((string) $alias, $invalidBehavior, $inlineServices, $isConstructorArgument);
}

try {
Expand Down
47 changes: 47 additions & 0 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public function dump(array $options = [])
$code =
$this->startClass($options['class'], $baseClass, $baseClassWithNamespace).
$this->addServices($services).
$this->addDeprecatedAliases().
$this->addDefaultParametersMethod()
;

Expand Down Expand Up @@ -1115,6 +1116,15 @@ private function addMethodMap(): string
}
}

$aliases = $this->container->getAliases();
foreach ($aliases as $alias => $id) {
if (!$id->isDeprecated()) {
continue;
}
$id = (string) $id;
$code .= ' '.$this->doExport($alias).' => '.$this->doExport($this->generateMethodName($alias)).",\n";
}

return $code ? " \$this->methodMap = [\n{$code} ];\n" : '';
}

Expand All @@ -1141,6 +1151,10 @@ private function addAliases(): string
$code = " \$this->aliases = [\n";
ksort($aliases);
foreach ($aliases as $alias => $id) {
if ($id->isDeprecated()) {
continue;
}

$id = (string) $id;
while (isset($aliases[$id])) {
$id = (string) $aliases[$id];
Expand All @@ -1151,6 +1165,39 @@ private function addAliases(): string
return $code." ];\n";
}

private function addDeprecatedAliases(): string
{
$code = '';
$aliases = $this->container->getAliases();
foreach ($aliases as $alias => $definition) {
if (!$definition->isDeprecated()) {
continue;
}
$public = $definition->isPublic() ? 'public' : 'private';
$id = (string) $definition;
$methodNameAlias = $this->generateMethodName($alias);
$idExported = $this->export($id);
$messageExported = $this->export($definition->getDeprecationMessage($alias));
$code = <<<EOF

/*{$this->docStar}
* Gets the $public '$alias' alias.
*
* @return object The "$id" service.
*/
protected function {$methodNameAlias}()
{
@trigger_error($messageExported, E_USER_DEPRECATED);

return \$this->get($idExported);
}

EOF;
}

return $code;
}

private function addInlineRequires(): string
{
if (!$this->hotPathTag || !$this->inlineRequires) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,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;
}

Expand Down Expand Up @@ -668,7 +672,10 @@ private function validateAlias(\DOMElement $alias, $file)
}

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, ['deprecated'], true)) {
throw new InvalidArgumentException(sprintf('Invalid child element "%s" defined for alias "%s" in "%s".', $child->localName, $alias->getAttribute('id'), $file));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,13 @@ private function parseDefinition($id, $service, $file, array $defaults)
}

foreach ($service as $key => $value) {
if (!\in_array($key, ['alias', 'public'])) {
if (!\in_array($key, ['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));
}

if ('deprecated' === $key) {
$alias->setDeprecated(true, $value);
}
}

return;
Expand Down
110 changes: 110 additions & 0 deletions src/Symfony/Component/DependencyInjection/Tests/AliasTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Alias;

class AliasTest extends TestCase
{
public function testConstructor()
{
$alias = new Alias('foo');

$this->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 alias 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($message)
{
$def = new Alias('foo');
$def->setDeprecated(true, $message);
}

public function invalidDeprecationMessageProvider()
{
return [
"With \rs" => ["invalid \r message %service_id%"],
"With \ns" => ["invalid \n message %service_id%"],
'With */s' => ['invalid */ message %service_id%'],
'message not containing required %service_id% variable' => ['this is deprecated'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,48 @@ public function testResolveFactory()
$this->assertSame('Factory', (string) $resolvedBarFactory[0]);
}

/**
* @group legacy
* @expectedDeprecation The "deprecated_foo_alias" service alias is deprecated. You should stop using it, as it will soon be removed.
*/
public function testDeprecationNoticeWhenReferencedByAlias()
{
$container = new ContainerBuilder();

$container->register('foo', 'stdClass');

$aliasDeprecated = new Alias('foo');
$aliasDeprecated->setDeprecated(true);
$container->setAlias('deprecated_foo_alias', $aliasDeprecated);

$alias = new Alias('deprecated_foo_alias');
$container->setAlias('alias', $alias);

$this->process($container);
}

/**
* @group legacy
* @expectedDeprecation The "foo_aliased" service alias is deprecated. You should stop using it, as it will soon be removed.
*/
public function testDeprecationNoticeWhenReferencedByDefinition()
{
$container = new ContainerBuilder();

$container->register('foo', 'stdClass');

$aliasDeprecated = new Alias('foo');
$aliasDeprecated->setDeprecated(true);
$container->setAlias('foo_aliased', $aliasDeprecated);

$container
->register('definition')
->setArguments([new Reference('foo_aliased')])
;

$this->process($container);
}

protected function process(ContainerBuilder $container)
{
$pass = new ResolveReferencesToAliasesPass();
Expand Down
Loading