Skip to content

Commit b124fb7

Browse files
[DI] add id of referencing service when a deprecated alias is found
1 parent 1aa652e commit b124fb7

18 files changed

+58
-55
lines changed

src/Symfony/Component/DependencyInjection/Alias.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Alias
2121
private $deprecated;
2222
private $deprecationTemplate;
2323

24-
private static $defaultDeprecationTemplate = 'The "%service_id%" service alias is deprecated. You should stop using it, as it will soon be removed.';
24+
private static $defaultDeprecationTemplate = 'The "%alias_id%" service alias is deprecated. You should stop using it, as it will be removed in the future.';
2525

2626
public function __construct(string $id, bool $public = true)
2727
{
@@ -103,8 +103,8 @@ public function setDeprecated($status = true, $template = null)
103103
throw new InvalidArgumentException('Invalid characters found in deprecation template.');
104104
}
105105

106-
if (false === strpos($template, '%service_id%')) {
107-
throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
106+
if (false === strpos($template, '%alias_id%')) {
107+
throw new InvalidArgumentException('The deprecation template must contain the "%alias_id%" placeholder.');
108108
}
109109

110110
$this->deprecationTemplate = $template;
@@ -122,7 +122,7 @@ public function isDeprecated(): bool
122122

123123
public function getDeprecationMessage(string $id): string
124124
{
125-
return str_replace('%service_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
125+
return str_replace('%alias_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
126126
}
127127

128128
/**

src/Symfony/Component/DependencyInjection/Compiler/RemoveUnusedDefinitionsPass.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function process(ContainerBuilder $container)
8686
protected function processValue($value, $isRoot = false)
8787
{
8888
if (!$value instanceof Reference) {
89-
return parent::processValue($value);
89+
return parent::processValue($value, $isRoot);
9090
}
9191

9292
if (ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE !== $value->getInvalidBehavior()) {

src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php

+19-16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function process(ContainerBuilder $container)
3131

3232
foreach ($container->getAliases() as $id => $alias) {
3333
$aliasId = (string) $alias;
34+
$this->currentId = $id;
3435

3536
if ($aliasId !== $defId = $this->getDefinitionId($aliasId, $container)) {
3637
$container->setAlias($id, $defId)->setPublic($alias->isPublic())->setPrivate($alias->isPrivate());
@@ -43,34 +44,36 @@ public function process(ContainerBuilder $container)
4344
*/
4445
protected function processValue($value, $isRoot = false)
4546
{
46-
if ($value instanceof Reference) {
47-
$defId = $this->getDefinitionId($id = (string) $value, $this->container);
48-
49-
if ($defId !== $id) {
50-
return new Reference($defId, $value->getInvalidBehavior());
51-
}
47+
if (!$value instanceof Reference) {
48+
return parent::processValue($value, $isRoot);
5249
}
5350

54-
return parent::processValue($value);
51+
$defId = $this->getDefinitionId($id = (string) $value, $this->container);
52+
53+
return $defId !== $id ? new Reference($defId, $value->getInvalidBehavior()) : $value;
5554
}
5655

5756
private function getDefinitionId(string $id, ContainerBuilder $container): string
5857
{
58+
if (!$container->hasAlias($id)) {
59+
return $id;
60+
}
61+
62+
$alias = $container->getAlias($id);
63+
64+
if ($alias->isDeprecated()) {
65+
@trigger_error(sprintf('%s. It is being referenced by the "%s" %s.', rtrim($alias->getDeprecationMessage($id), '. '), $this->currentId, $container->hasDefinition($this->currentId) ? 'service' : 'alias'), E_USER_DEPRECATED);
66+
}
67+
5968
$seen = [];
60-
while ($container->hasAlias($id)) {
69+
do {
6170
if (isset($seen[$id])) {
6271
throw new ServiceCircularReferenceException($id, array_merge(array_keys($seen), [$id]));
6372
}
6473

6574
$seen[$id] = true;
66-
$alias = $container->getAlias($id);
67-
68-
if ($alias->isDeprecated()) {
69-
@trigger_error($alias->getDeprecationMessage($id), E_USER_DEPRECATED);
70-
}
71-
72-
$id = (string) $alias;
73-
}
75+
$id = (string) $container->getAlias($id);
76+
} while ($container->hasAlias($id));
7477

7578
return $id;
7679
}

src/Symfony/Component/DependencyInjection/Definition.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Definition
4747

4848
protected $arguments = [];
4949

50-
private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
50+
private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will be removed in the future.';
5151

5252
/**
5353
* @internal

src/Symfony/Component/DependencyInjection/Tests/AliasTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testCanSetPublic()
5252
public function testCanDeprecateAnAlias()
5353
{
5454
$alias = new Alias('foo', false);
55-
$alias->setDeprecated(true, 'The %service_id% service is deprecated.');
55+
$alias->setDeprecated(true, 'The %alias_id% service is deprecated.');
5656

5757
$this->assertTrue($alias->isDeprecated());
5858
}
@@ -62,14 +62,14 @@ public function testItHasADefaultDeprecationMessage()
6262
$alias = new Alias('foo', false);
6363
$alias->setDeprecated();
6464

65-
$expectedMessage = 'The "foo" service alias is deprecated. You should stop using it, as it will soon be removed.';
65+
$expectedMessage = 'The "foo" service alias is deprecated. You should stop using it, as it will be removed in the future.';
6666
$this->assertEquals($expectedMessage, $alias->getDeprecationMessage('foo'));
6767
}
6868

6969
public function testReturnsCorrectDeprecationMessage()
7070
{
7171
$alias = new Alias('foo', false);
72-
$alias->setDeprecated(true, 'The "%service_id%" is deprecated.');
72+
$alias->setDeprecated(true, 'The "%alias_id%" is deprecated.');
7373

7474
$expectedMessage = 'The "foo" is deprecated.';
7575
$this->assertEquals($expectedMessage, $alias->getDeprecationMessage('foo'));
@@ -101,10 +101,10 @@ public function testCannotDeprecateWithAnInvalidTemplate($message)
101101
public function invalidDeprecationMessageProvider()
102102
{
103103
return [
104-
"With \rs" => ["invalid \r message %service_id%"],
105-
"With \ns" => ["invalid \n message %service_id%"],
106-
'With */s' => ['invalid */ message %service_id%'],
107-
'message not containing required %service_id% variable' => ['this is deprecated'],
104+
"With \rs" => ["invalid \r message %alias_id%"],
105+
"With \ns" => ["invalid \n message %alias_id%"],
106+
'With */s' => ['invalid */ message %alias_id%'],
107+
'message not containing required %alias_id% variable' => ['this is deprecated'],
108108
];
109109
}
110110
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveReferencesToAliasesPassTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testResolveFactory()
8585

8686
/**
8787
* @group legacy
88-
* @expectedDeprecation The "deprecated_foo_alias" service alias is deprecated. You should stop using it, as it will soon be removed.
88+
* @expectedDeprecation The "deprecated_foo_alias" service alias is deprecated. You should stop using it, as it will be removed in the future. It is being referenced by the "alias" alias.
8989
*/
9090
public function testDeprecationNoticeWhenReferencedByAlias()
9191
{
@@ -105,7 +105,7 @@ public function testDeprecationNoticeWhenReferencedByAlias()
105105

106106
/**
107107
* @group legacy
108-
* @expectedDeprecation The "foo_aliased" service alias is deprecated. You should stop using it, as it will soon be removed.
108+
* @expectedDeprecation The "foo_aliased" service alias is deprecated. You should stop using it, as it will be removed in the future. It is being referenced by the "definition" service.
109109
*/
110110
public function testDeprecationNoticeWhenReferencedByDefinition()
111111
{

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testDefinitions()
8888

8989
/**
9090
* @group legacy
91-
* @expectedDeprecation The "deprecated_foo" service is deprecated. You should stop using it, as it will soon be removed.
91+
* @expectedDeprecation The "deprecated_foo" service is deprecated. You should stop using it, as it will be removed in the future.
9292
*/
9393
public function testCreateDeprecatedService()
9494
{
@@ -261,7 +261,7 @@ public function testAliases()
261261

262262
/**
263263
* @group legacy
264-
* @expectedDeprecation The "foobar" service alias is deprecated. You should stop using it, as it will soon be removed.
264+
* @expectedDeprecation The "foobar" service alias is deprecated. You should stop using it, as it will be removed in the future.
265265
*/
266266
public function testDeprecatedAlias()
267267
{

src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function testSetIsDeprecated()
164164
$this->assertFalse($def->isDeprecated(), '->isDeprecated() returns false by default');
165165
$this->assertSame($def, $def->setDeprecated(true), '->setDeprecated() implements a fluent interface');
166166
$this->assertTrue($def->isDeprecated(), '->isDeprecated() returns true if the instance should not be used anymore.');
167-
$this->assertSame('The "deprecated_service" service is deprecated. You should stop using it, as it will soon be removed.', $def->getDeprecationMessage('deprecated_service'), '->getDeprecationMessage() should return a formatted message template');
167+
$this->assertSame('The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future.', $def->getDeprecationMessage('deprecated_service'), '->getDeprecationMessage() should return a formatted message template');
168168
}
169169

170170
/**

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public function testAliases()
327327

328328
/**
329329
* @group legacy
330-
* @expectedDeprecation The "alias_for_foo_deprecated" service alias is deprecated. You should stop using it, as it will soon be removed.
330+
* @expectedDeprecation The "alias_for_foo_deprecated" service alias is deprecated. You should stop using it, as it will be removed in the future.
331331
*/
332332
public function testAliasesDeprecation()
333333
{
@@ -1067,7 +1067,7 @@ public function testAdawsonContainer()
10671067
* This test checks the trigger of a deprecation note and should not be removed in major releases.
10681068
*
10691069
* @group legacy
1070-
* @expectedDeprecation The "foo" service is deprecated. You should stop using it, as it will soon be removed.
1070+
* @expectedDeprecation The "foo" service is deprecated. You should stop using it, as it will be removed in the future.
10711071
*/
10721072
public function testPrivateServiceTriggersDeprecation()
10731073
{

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/container_alias_deprecation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected function getFooService()
6666
*/
6767
protected function getAliasForFooDeprecatedService()
6868
{
69-
@trigger_error('The "alias_for_foo_deprecated" service alias is deprecated. You should stop using it, as it will soon be removed.', E_USER_DEPRECATED);
69+
@trigger_error('The "alias_for_foo_deprecated" service alias is deprecated. You should stop using it, as it will be removed in the future.', E_USER_DEPRECATED);
7070

7171
return $this->get('foo');
7272
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException;
124124
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
125125
// Returns the public 'deprecated_service' shared service.
126126

127-
@trigger_error('The "deprecated_service" service is deprecated. You should stop using it, as it will soon be removed.', E_USER_DEPRECATED);
127+
@trigger_error('The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future.', E_USER_DEPRECATED);
128128

129129
return $this->services['deprecated_service'] = new \stdClass();
130130

@@ -156,7 +156,7 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException;
156156
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
157157
// Returns the private 'factory_simple' shared service.
158158

159-
@trigger_error('The "factory_simple" service is deprecated. You should stop using it, as it will soon be removed.', E_USER_DEPRECATED);
159+
@trigger_error('The "factory_simple" service is deprecated. You should stop using it, as it will be removed in the future.', E_USER_DEPRECATED);
160160

161161
return new \SimpleFactoryClass('foo');
162162

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ protected function getDecoratorServiceWithNameService()
206206
*
207207
* @return \stdClass
208208
*
209-
* @deprecated The "deprecated_service" service is deprecated. You should stop using it, as it will soon be removed.
209+
* @deprecated The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future.
210210
*/
211211
protected function getDeprecatedServiceService()
212212
{
213-
@trigger_error('The "deprecated_service" service is deprecated. You should stop using it, as it will soon be removed.', E_USER_DEPRECATED);
213+
@trigger_error('The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future.', E_USER_DEPRECATED);
214214

215215
return $this->services['deprecated_service'] = new \stdClass();
216216
}
@@ -400,11 +400,11 @@ protected function getTaggedIteratorService()
400400
*
401401
* @return \SimpleFactoryClass
402402
*
403-
* @deprecated The "factory_simple" service is deprecated. You should stop using it, as it will soon be removed.
403+
* @deprecated The "factory_simple" service is deprecated. You should stop using it, as it will be removed in the future.
404404
*/
405405
protected function getFactorySimpleService()
406406
{
407-
@trigger_error('The "factory_simple" service is deprecated. You should stop using it, as it will soon be removed.', E_USER_DEPRECATED);
407+
@trigger_error('The "factory_simple" service is deprecated. You should stop using it, as it will be removed in the future.', E_USER_DEPRECATED);
408408

409409
return new \SimpleFactoryClass('foo');
410410
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ protected function getDecoratorServiceWithNameService()
206206
*
207207
* @return \stdClass
208208
*
209-
* @deprecated The "deprecated_service" service is deprecated. You should stop using it, as it will soon be removed.
209+
* @deprecated The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future.
210210
*/
211211
protected function getDeprecatedServiceService()
212212
{
213-
@trigger_error('The "deprecated_service" service is deprecated. You should stop using it, as it will soon be removed.', E_USER_DEPRECATED);
213+
@trigger_error('The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future.', E_USER_DEPRECATED);
214214

215215
return $this->services['deprecated_service'] = new \stdClass();
216216
}
@@ -400,11 +400,11 @@ protected function getTaggedIteratorService()
400400
*
401401
* @return \SimpleFactoryClass
402402
*
403-
* @deprecated The "factory_simple" service is deprecated. You should stop using it, as it will soon be removed.
403+
* @deprecated The "factory_simple" service is deprecated. You should stop using it, as it will be removed in the future.
404404
*/
405405
protected function getFactorySimpleService()
406406
{
407-
@trigger_error('The "factory_simple" service is deprecated. You should stop using it, as it will soon be removed.', E_USER_DEPRECATED);
407+
@trigger_error('The "factory_simple" service is deprecated. You should stop using it, as it will be removed in the future.', E_USER_DEPRECATED);
408408

409409
return new \SimpleFactoryClass('foo');
410410
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/deprecated_alias_definitions.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<deprecated />
88
</service>
99
<service id="alias_for_foobar" alias="foobar">
10-
<deprecated>The "%service_id%" service alias is deprecated.</deprecated>
10+
<deprecated>The "%alias_id%" service alias is deprecated.</deprecated>
1111
</service>
1212
</services>
1313
</container>

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
<service id="decorator_service" class="stdClass" public="true" decorates="decorated"/>
9898
<service id="decorator_service_with_name" class="stdClass" public="true" decorates="decorated" decoration-inner-name="decorated.pif-pouf"/>
9999
<service id="deprecated_service" class="stdClass" public="true">
100-
<deprecated>The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.</deprecated>
100+
<deprecated>The "%service_id%" service is deprecated. You should stop using it, as it will be removed in the future.</deprecated>
101101
</service>
102102
<service id="new_factory" class="FactoryClass" public="false">
103103
<property name="foo">bar</property>
@@ -114,7 +114,7 @@
114114
</service>
115115
<service id="factory_simple" class="SimpleFactoryClass" public="false">
116116
<argument>foo</argument>
117-
<deprecated>The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.</deprecated>
117+
<deprecated>The "%service_id%" service is deprecated. You should stop using it, as it will be removed in the future.</deprecated>
118118
</service>
119119
<service id="factory_service_simple" class="Bar" public="true">
120120
<factory service="factory_simple" method="getInstance"/>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
services:
22
alias_for_foobar:
33
alias: foobar
4-
deprecated: The "%service_id%" service alias is deprecated.
4+
deprecated: The "%alias_id%" service alias is deprecated.

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ services:
103103
public: true
104104
deprecated_service:
105105
class: stdClass
106-
deprecated: The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.
106+
deprecated: The "%service_id%" service is deprecated. You should stop using it, as it will be removed in the future.
107107
public: true
108108
new_factory:
109109
class: FactoryClass
@@ -124,7 +124,7 @@ services:
124124
public: true
125125
factory_simple:
126126
class: SimpleFactoryClass
127-
deprecated: The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.
127+
deprecated: The "%service_id%" service is deprecated. You should stop using it, as it will be removed in the future.
128128
public: false
129129
arguments: ['foo']
130130
factory_service_simple:

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public function testDeprecated()
343343
$loader->load('services_deprecated.xml');
344344

345345
$this->assertTrue($container->getDefinition('foo')->isDeprecated());
346-
$message = 'The "foo" service is deprecated. You should stop using it, as it will soon be removed.';
346+
$message = 'The "foo" service is deprecated. You should stop using it, as it will be removed in the future.';
347347
$this->assertSame($message, $container->getDefinition('foo')->getDeprecationMessage('foo'));
348348

349349
$this->assertTrue($container->getDefinition('bar')->isDeprecated());
@@ -358,7 +358,7 @@ public function testDeprecatedAliases()
358358
$loader->load('deprecated_alias_definitions.xml');
359359

360360
$this->assertTrue($container->getAlias('alias_for_foo')->isDeprecated());
361-
$message = 'The "alias_for_foo" service alias is deprecated. You should stop using it, as it will soon be removed.';
361+
$message = 'The "alias_for_foo" service alias is deprecated. You should stop using it, as it will be removed in the future.';
362362
$this->assertSame($message, $container->getAlias('alias_for_foo')->getDeprecationMessage('alias_for_foo'));
363363

364364
$this->assertTrue($container->getAlias('alias_for_foobar')->isDeprecated());

0 commit comments

Comments
 (0)