Skip to content

Commit 0b3d0a0

Browse files
committed
[DI] Allow to change the deprecation message in Definition
1 parent 954247d commit 0b3d0a0

File tree

14 files changed

+85
-21
lines changed

14 files changed

+85
-21
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ public function createService(Definition $definition, $id, $tryProxy = true)
932932
}
933933

934934
if ($definition->isDeprecated()) {
935-
@trigger_error(sprintf('The service %s relies on a deprecated definition. You should avoid using it.', $id), E_USER_DEPRECATED);
935+
@trigger_error($definition->getDeprecationMessage($id), E_USER_DEPRECATED);
936936
}
937937

938938
if ($tryProxy && $definition->isLazy()) {

src/Symfony/Component/DependencyInjection/Definition.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class Definition
3030
private $factoryMethod;
3131
private $factoryService;
3232
private $shared = true;
33+
private $deprecated = false;
34+
private $deprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
3335
private $scope = ContainerInterface::SCOPE_CONTAINER;
3436
private $properties = array();
3537
private $calls = array();
@@ -38,7 +40,6 @@ class Definition
3840
private $public = true;
3941
private $synthetic = false;
4042
private $abstract = false;
41-
private $deprecated = false;
4243
private $synchronized = false;
4344
private $lazy = false;
4445
private $decoratedService;
@@ -834,14 +835,29 @@ public function isAbstract()
834835
* Whether this definition is deprecated, that means it should not be called
835836
* anymore.
836837
*
837-
* @param bool $status
838+
* @param bool $status
839+
* @param string $template Template message to use if the definition is deprecated
838840
*
839841
* @return Definition the current instance
840842
*
843+
* @throws InvalidArgumentException When the message template is invalid.
844+
*
841845
* @api
842846
*/
843-
public function setDeprecated($status = true)
847+
public function setDeprecated($status = true, $template = null)
844848
{
849+
if (null !== $template) {
850+
if (preg_match('#[\r\n]|\*/#', $template)) {
851+
throw new InvalidArgumentException('Invalid characters found in deprecation template.');
852+
}
853+
854+
if (false === strpos($template, '%service_id%')) {
855+
throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
856+
}
857+
858+
$this->deprecationTemplate = $template;
859+
}
860+
845861
$this->deprecated = (bool) $status;
846862

847863
return $this;
@@ -860,6 +876,20 @@ public function isDeprecated()
860876
return $this->deprecated;
861877
}
862878

879+
/**
880+
* Message to use if this definition is deprecated.
881+
*
882+
* @param string $id Service id relying on this definition
883+
*
884+
* @return string
885+
*
886+
* @api
887+
*/
888+
public function getDeprecationMessage($id)
889+
{
890+
return str_replace('%service_id%', $id, $this->deprecationTemplate);
891+
}
892+
863893
/**
864894
* Sets a configurator to call after the service is fully initialized.
865895
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ private function addService($id, $definition)
597597
$return[] = '';
598598
}
599599

600-
$return[] = '@deprecated';
600+
$return[] = sprintf('@deprecated %s', $definition->getDeprecationMessage($id));
601601
}
602602

603603
$return = str_replace("\n * \n", "\n *\n", implode("\n * ", $return));
@@ -661,7 +661,7 @@ private function addService($id, $definition)
661661
$code .= sprintf(" throw new RuntimeException('You have requested a synthetic service (\"%s\"). The DIC does not know how to construct this service.');\n }\n", $id);
662662
} else {
663663
if ($definition->isDeprecated()) {
664-
$code .= sprintf(" @trigger_error('The service %s has been marked as deprecated. You should stop using it.', E_USER_DEPRECATED);\n\n", $id);
664+
$code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", var_export($definition->getDeprecationMessage($id), true));
665665
}
666666

667667
$code .=

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ private function addService($definition, $id, \DOMElement $parent)
148148
if ($definition->isLazy()) {
149149
$service->setAttribute('lazy', 'true');
150150
}
151-
if ($definition->isDeprecated()) {
152-
$service->setAttribute('deprecated', 'true');
153-
}
154151
if (null !== $decorated = $definition->getDecoratedService()) {
155152
list($decorated, $renamedId, $priority) = $decorated;
156153
$service->setAttribute('decorates', $decorated);
@@ -204,6 +201,13 @@ private function addService($definition, $id, \DOMElement $parent)
204201
$service->appendChild($factory);
205202
}
206203

204+
if ($definition->isDeprecated()) {
205+
$deprecated = $this->document->createElement('deprecated');
206+
$deprecated->appendChild($this->document->createTextNode($definition->getDeprecationMessage('%service_id%')));
207+
208+
$service->appendChild($deprecated);
209+
}
210+
207211
if ($callable = $definition->getConfigurator()) {
208212
$configurator = $this->document->createElement('configurator');
209213

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private function addService($id, $definition)
105105
}
106106

107107
if ($definition->isDeprecated()) {
108-
$code .= " deprecated: true\n";
108+
$code .= sprintf(" deprecated: %s\n", $definition->getDeprecationMessage('%service_id%'));
109109
}
110110

111111
if ($definition->getFactoryClass(false)) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private function parseDefinition(\DOMElement $service, $file)
147147
$definition = new Definition();
148148
}
149149

150-
foreach (array('class', 'shared', 'public', 'factory-class', 'factory-method', 'factory-service', 'synthetic', 'lazy', 'abstract', 'deprecated') as $key) {
150+
foreach (array('class', 'shared', 'public', 'factory-class', 'factory-method', 'factory-service', 'synthetic', 'lazy', 'abstract') as $key) {
151151
if ($value = $service->getAttribute($key)) {
152152
if (in_array($key, array('factory-class', 'factory-method', 'factory-service'))) {
153153
@trigger_error(sprintf('The "%s" attribute of service "%s" in file "%s" is deprecated since version 2.6 and will be removed in 3.0. Use the "factory" element instead.', $key, (string) $service->getAttribute('id'), $file), E_USER_DEPRECATED);
@@ -181,6 +181,10 @@ private function parseDefinition(\DOMElement $service, $file)
181181
$definition->setFile($files[0]->nodeValue);
182182
}
183183

184+
if ($deprecated = $this->getChildren($service, 'deprecated')) {
185+
$definition->setDeprecated(true, $deprecated[0]->nodeValue);
186+
}
187+
184188
$definition->setArguments($this->getArgumentsAsPhp($service, 'argument'));
185189
$definition->setProperties($this->getArgumentsAsPhp($service, 'property'));
186190

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ private function parseDefinition($id, $service, $file)
196196
$definition->setAbstract($service['abstract']);
197197
}
198198

199-
if (isset($service['deprecated'])) {
200-
$definition->setDeprecated($service['deprecated']);
199+
if (array_key_exists('deprecated', $service)) {
200+
$definition->setDeprecated(true, $service['deprecated']);
201201
}
202202

203203
if (isset($service['factory'])) {

src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
<xsd:element name="argument" type="argument" minOccurs="0" maxOccurs="unbounded" />
8282
<xsd:element name="configurator" type="callable" minOccurs="0" maxOccurs="1" />
8383
<xsd:element name="factory" type="callable" minOccurs="0" maxOccurs="1" />
84+
<xsd:element name="deprecated" type="xsd:string" minOccurs="0" maxOccurs="1" />
8485
<xsd:element name="call" type="call" minOccurs="0" maxOccurs="unbounded" />
8586
<xsd:element name="tag" type="tag" minOccurs="0" maxOccurs="unbounded" />
8687
<xsd:element name="property" type="property" minOccurs="0" maxOccurs="unbounded" />
@@ -94,7 +95,6 @@
9495
<xsd:attribute name="synchronized" type="boolean" />
9596
<xsd:attribute name="lazy" type="boolean" />
9697
<xsd:attribute name="abstract" type="boolean" />
97-
<xsd:attribute name="deprecated" type="boolean" />
9898
<xsd:attribute name="factory-class" type="xsd:string" />
9999
<xsd:attribute name="factory-method" type="xsd:string" />
100100
<xsd:attribute name="factory-service" type="xsd:string" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function testCreateDeprecatedService()
7373

7474
set_error_handler(function ($errno, $errstr) use ($that, &$wasTriggered) {
7575
$that->assertSame(E_USER_DEPRECATED, $errno);
76-
$that->assertSame('The service deprecated_foo relies on a deprecated definition. You should avoid using it.', $errstr);
76+
$that->assertSame('The "deprecated_foo" service is deprecated. You should stop using it, as it will soon be removed.', $errstr);
7777
$wasTriggered = true;
7878
});
7979

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,37 @@ public function testSetIsAbstract()
236236
/**
237237
* @covers Symfony\Component\DependencyInjection\Definition::setDeprecated
238238
* @covers Symfony\Component\DependencyInjection\Definition::isDeprecated
239+
* @covers Symfony\Component\DependencyInjection\Definition::hasCustomDeprecationTemplate
240+
* @covers Symfony\Component\DependencyInjection\Definition::getDeprecationMessage
239241
*/
240242
public function testSetIsDeprecated()
241243
{
242244
$def = new Definition('stdClass');
243245
$this->assertFalse($def->isDeprecated(), '->isDeprecated() returns false by default');
244246
$this->assertSame($def, $def->setDeprecated(true), '->setDeprecated() implements a fluent interface');
245247
$this->assertTrue($def->isDeprecated(), '->isDeprecated() returns true if the instance should not be used anymore.');
248+
$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');
249+
}
250+
251+
/**
252+
* @dataProvider invalidDeprecationMessageProvider
253+
* @covers Symfony\Component\DependencyInjection\Definition::setDeprecated
254+
* @expectedException Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
255+
*/
256+
public function testSetDeprecatedWithInvalidDeprecationTemplate($message)
257+
{
258+
$def = new Definition('stdClass');
259+
$def->setDeprecated(false, $message);
260+
}
261+
262+
public function invalidDeprecationMessageProvider()
263+
{
264+
return array(
265+
"With \rs" => array("invalid \r message %service_id%"),
266+
"With \ns" => array("invalid \n message %service_id%"),
267+
'With */s' => array('invalid */ message %service_id%'),
268+
'message not containing require %service_id% variable' => array('this is deprecated'),
269+
);
246270
}
247271

248272
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy