Skip to content

Commit 41629bb

Browse files
author
Anthony MARTIN
committed
[DependencyInjection] Added information about deprecated aliases in debug:autowiring
| Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | n/a Fix and improves a bit PR #29968 and #29995
1 parent a86130a commit 41629bb

File tree

7 files changed

+22
-10
lines changed

7 files changed

+22
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added `%env(trim:...)%` processor to trim a string value
88
* added `%env(default:...)%` processor to fallback to a default value
9+
* added support for deprecating aliases
910

1011
4.2.0
1112
-----

Dumper/XmlDumper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ private function addServiceAlias($alias, Alias $id, \DOMElement $parent)
227227
if (!$id->isPrivate()) {
228228
$service->setAttribute('public', $id->isPublic() ? 'true' : 'false');
229229
}
230+
231+
if ($id->isDeprecated()) {
232+
$deprecated = $this->document->createElement('deprecated');
233+
$deprecated->appendChild($this->document->createTextNode($id->getDeprecationMessage('%alias_id%')));
234+
235+
$service->appendChild($deprecated);
236+
}
237+
230238
$parent->appendChild($service);
231239
}
232240

Dumper/YamlDumper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,13 @@ private function addService(string $id, Definition $definition): string
155155

156156
private function addServiceAlias(string $alias, Alias $id): string
157157
{
158+
$deprecated = $id->isDeprecated() ? sprintf(" deprecated: %s\n", $id->getDeprecationMessage('%alias_id%')) : '';
159+
158160
if ($id->isPrivate()) {
159-
return sprintf(" %s: '@%s'\n", $alias, $id);
161+
return sprintf(" %s: '@%s'\n%s", $alias, $id, $deprecated);
160162
}
161163

162-
return sprintf(" %s:\n alias: %s\n public: %s\n", $alias, $id, $id->isPublic() ? 'true' : 'false');
164+
return sprintf(" %s:\n alias: %s\n public: %s\n%s", $alias, $id, $id->isPublic() ? 'true' : 'false', $deprecated);
163165
}
164166

165167
private function addServices(): string

Loader/YamlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ private function parseDefinition($id, $service, $file, array $defaults)
350350

351351
foreach ($service as $key => $value) {
352352
if (!\in_array($key, ['alias', 'public', 'deprecated'])) {
353-
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));
353+
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", "public" and "deprecated".', $key, $id, $file));
354354
}
355355

356356
if ('deprecated' === $key) {

Tests/AliasTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,10 @@ public function testCanOverrideDeprecation()
7979
{
8080
$alias = new Alias('foo', false);
8181
$alias->setDeprecated();
82+
$this->assertTrue($alias->isDeprecated());
8283

83-
$initial = $alias->isDeprecated();
8484
$alias->setDeprecated(false);
85-
$final = $alias->isDeprecated();
86-
87-
$this->assertTrue($initial);
88-
$this->assertFalse($final);
85+
$this->assertFalse($alias->isDeprecated());
8986
}
9087

9188
/**
@@ -105,6 +102,7 @@ public function invalidDeprecationMessageProvider()
105102
"With \ns" => ["invalid \n message %alias_id%"],
106103
'With */s' => ['invalid */ message %alias_id%'],
107104
'message not containing required %alias_id% variable' => ['this is deprecated'],
105+
'template not containing required %alias_id% variable' => [true],
108106
];
109107
}
110108
}

Tests/DefinitionTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ 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 be removed in the future.', $def->getDeprecationMessage('deprecated_service'), '->getDeprecationMessage() should return a formatted message template');
167+
168+
$def->setDeprecated(true, '%service_id%');
169+
$this->assertSame('deprecated_service', $def->getDeprecationMessage('deprecated_service'), '->getDeprecationMessage() should return given formatted message template');
168170
}
169171

170172
/**
@@ -184,6 +186,7 @@ public function invalidDeprecationMessageProvider()
184186
"With \ns" => ["invalid \n message %service_id%"],
185187
'With */s' => ['invalid */ message %service_id%'],
186188
'message not containing require %service_id% variable' => ['this is deprecated'],
189+
'template not containing require %service_id% variable' => [true],
187190
];
188191
}
189192

Tests/Fixtures/xml/deprecated_alias_definitions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<service id="foo" class="Foo">
55
</service>
66
<service id="alias_for_foo" alias="foo">
7-
<deprecated />
7+
<deprecated>The "%alias_id%" service alias is deprecated. You should stop using it, as it will be removed in the future.</deprecated>
88
</service>
99
<service id="alias_for_foobar" alias="foobar">
1010
<deprecated>The "%alias_id%" service alias is deprecated.</deprecated>

0 commit comments

Comments
 (0)