Skip to content

Commit 3d2378d

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 2cad97b commit 3d2378d

File tree

10 files changed

+30
-12
lines changed

10 files changed

+30
-12
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
PHP's native `serialize()` and `unserialize()` functions. To use the
1313
original serialization method, set the `framework.messenger.serializer.id`
1414
config option to `messenger.transport.symfony_serializer`.
15+
* Added information about deprecated aliases in `debug:autowiring`
1516

1617
4.2.0
1718
-----

src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
104104
$serviceLine = sprintf('<fg=yellow>%s</>', $serviceId);
105105
if ($builder->hasAlias($serviceId)) {
106106
$hasAlias[$serviceId] = true;
107-
$serviceLine .= ' <fg=cyan>('.$builder->getAlias($serviceId).')</>';
107+
$serviceAlias = $builder->getAlias($serviceId);
108+
$serviceLine .= ' <fg=cyan>('.$serviceAlias.')</>';
109+
110+
if ($serviceAlias->isDeprecated()) {
111+
$serviceLine .= ' - <fg=magenta>deprecated</>';
112+
}
108113
} elseif (!$all) {
109114
continue;
110115
}

src/Symfony/Bundle/FrameworkBundle/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"symfony/cache": "~4.3",
2222
"symfony/config": "~4.2",
2323
"symfony/contracts": "^1.0.2",
24-
"symfony/dependency-injection": "^4.2",
24+
"symfony/dependency-injection": "^4.3",
2525
"symfony/event-dispatcher": "^4.1",
2626
"symfony/http-foundation": "^4.3",
2727
"symfony/http-kernel": "^4.2",

src/Symfony/Component/DependencyInjection/CHANGELOG.md

+1
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
-----

src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php

+8
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

src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php

+4-2
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

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

+1-1
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) {

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

+3-5
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
}

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

+4-1
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

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

+1-1
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)