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

Skip to content

Commit a00a4ff

Browse files
sanpiinicolas-grekas
authored andcommitted
[config] Add ability to deprecate a node
1 parent 660fecc commit a00a4ff

File tree

10 files changed

+82
-0
lines changed

10 files changed

+82
-0
lines changed

src/Symfony/Component/Config/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.4.0
5+
-----
6+
7+
* added `setDeprecated()` method to indicate a deprecated node
8+
49
3.3.0
510
-----
611

src/Symfony/Component/Config/Definition/ArrayNode.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ protected function finalizeValue($value)
234234
}
235235

236236
foreach ($this->children as $name => $child) {
237+
if ($child->isDeprecated()) {
238+
@trigger_error($child->getDeprecationMessage($name, $this->getPath()), E_USER_DEPRECATED);
239+
}
240+
237241
if (!array_key_exists($name, $value)) {
238242
if ($child->isRequired()) {
239243
$msg = sprintf('The child node "%s" at path "%s" must be configured.', $name, $this->getPath());

src/Symfony/Component/Config/Definition/BaseNode.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ abstract class BaseNode implements NodeInterface
2929
protected $finalValidationClosures = array();
3030
protected $allowOverwrite = true;
3131
protected $required = false;
32+
protected $deprecationMessage = null;
3233
protected $equivalentValues = array();
3334
protected $attributes = array();
3435

@@ -141,6 +142,19 @@ public function setRequired($boolean)
141142
$this->required = (bool) $boolean;
142143
}
143144

145+
/**
146+
* Sets this node as deprecated.
147+
*
148+
* You can use %node% and %path% placeholders in your message to display,
149+
* respectively, the node name and its complete path.
150+
*
151+
* @param string|null $message Deprecated message
152+
*/
153+
public function setDeprecated($message)
154+
{
155+
$this->deprecationMessage = $message;
156+
}
157+
144158
/**
145159
* Sets if this node can be overridden.
146160
*
@@ -181,6 +195,29 @@ public function isRequired()
181195
return $this->required;
182196
}
183197

198+
/**
199+
* Checks if this node is deprecated.
200+
*
201+
* @return bool
202+
*/
203+
public function isDeprecated()
204+
{
205+
return null !== $this->deprecationMessage;
206+
}
207+
208+
/**
209+
* Returns the deprecated message.
210+
*
211+
* @param string $node the configuration node name
212+
* @param string $path the path of the node
213+
*
214+
* @return string
215+
*/
216+
public function getDeprecationMessage($node, $path)
217+
{
218+
return strtr($this->deprecationMessage, array('%node%' => $node, '%path%' => $path));
219+
}
220+
184221
/**
185222
* Returns the name of this node.
186223
*

src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ abstract class NodeDefinition implements NodeParentInterface
2727
protected $defaultValue;
2828
protected $default = false;
2929
protected $required = false;
30+
protected $deprecationMessage = null;
3031
protected $merge;
3132
protected $allowEmptyValue = true;
3233
protected $nullEquivalent;
@@ -168,6 +169,23 @@ public function isRequired()
168169
return $this;
169170
}
170171

172+
/**
173+
* Sets the node as deprecated.
174+
*
175+
* You can use %node% and %path% placeholders in your message to display,
176+
* respectively, the node name and its complete path.
177+
*
178+
* @param string $message Deprecation message
179+
*
180+
* @return $this
181+
*/
182+
public function setDeprecated($message = 'The child node "%node%" at path "%path%" is deprecated.')
183+
{
184+
$this->deprecationMessage = $message;
185+
186+
return $this;
187+
}
188+
171189
/**
172190
* Sets the equivalent value used when the node contains null.
173191
*

src/Symfony/Component/Config/Definition/Builder/VariableNodeDefinition.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ protected function createNode()
5454
$node->addEquivalentValue(true, $this->trueEquivalent);
5555
$node->addEquivalentValue(false, $this->falseEquivalent);
5656
$node->setRequired($this->required);
57+
$node->setDeprecated($this->deprecationMessage);
5758

5859
if (null !== $this->validation) {
5960
$node->setFinalValidationClosures($this->validation->rules);

src/Symfony/Component/Config/Definition/Dumper/XmlReferenceDumper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ private function writeNode(NodeInterface $node, $depth = 0, $root = false, $name
153153
$comments[] = 'Required';
154154
}
155155

156+
if ($child->isDeprecated()) {
157+
$comments[] = sprintf('Deprecated (%s)', $child->getDeprecationMessage($child->getName(), $child->getPath()));
158+
}
159+
156160
if ($child instanceof EnumNode) {
157161
$comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues()));
158162
}

src/Symfony/Component/Config/Definition/Dumper/YamlReferenceDumper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ private function writeNode(NodeInterface $node, $depth = 0, $prototypedArray = f
123123
$comments[] = 'Required';
124124
}
125125

126+
// deprecated?
127+
if ($node->isDeprecated()) {
128+
$comments[] = sprintf('Deprecated (%s)', $node->getDeprecationMessage($node->getName(), $node->getPath()));
129+
}
130+
126131
// example
127132
if ($example && !is_array($example)) {
128133
$comments[] = 'Example: '.$example;

src/Symfony/Component/Config/Tests/Definition/Dumper/XmlReferenceDumperTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ private function getConfigurationAsString()
3838
return str_replace("\n", PHP_EOL, <<<'EOL'
3939
<!-- Namespace: http://example.org/schema/dic/acme_root -->
4040
<!-- scalar-required: Required -->
41+
<!-- scalar-deprecated: Deprecated (The child node "scalar_deprecated" at path "acme_root.scalar_deprecated" is deprecated.) -->
42+
<!-- scalar-deprecated-with-message: Deprecated (Deprecation custom message for "scalar_deprecated_with_message" at "acme_root.scalar_deprecated_with_message") -->
4143
<!-- enum-with-default: One of "this"; "that" -->
4244
<!-- enum: One of "this"; "that" -->
4345
<config
@@ -50,6 +52,8 @@ private function getConfigurationAsString()
5052
scalar-array-empty=""
5153
scalar-array-defaults="elem1,elem2"
5254
scalar-required=""
55+
scalar-deprecated=""
56+
scalar-deprecated-with-message=""
5357
node-with-a-looong-name=""
5458
enum-with-default="this"
5559
enum=""

src/Symfony/Component/Config/Tests/Definition/Dumper/YamlReferenceDumperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ private function getConfigurationAsString()
9898
- elem1
9999
- elem2
100100
scalar_required: ~ # Required
101+
scalar_deprecated: ~ # Deprecated (The child node "scalar_deprecated" at path "acme_root.scalar_deprecated" is deprecated.)
102+
scalar_deprecated_with_message: ~ # Deprecated (Deprecation custom message for "scalar_deprecated_with_message" at "acme_root.scalar_deprecated_with_message")
101103
node_with_a_looong_name: ~
102104
enum_with_default: this # One of "this"; "that"
103105
enum: ~ # One of "this"; "that"

src/Symfony/Component/Config/Tests/Fixtures/Configuration/ExampleConfiguration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function getConfigTreeBuilder()
3535
->scalarNode('scalar_array_empty')->defaultValue(array())->end()
3636
->scalarNode('scalar_array_defaults')->defaultValue(array('elem1', 'elem2'))->end()
3737
->scalarNode('scalar_required')->isRequired()->end()
38+
->scalarNode('scalar_deprecated')->setDeprecated()->end()
39+
->scalarNode('scalar_deprecated_with_message')->setDeprecated('Deprecation custom message for "%node%" at "%path%"')->end()
3840
->scalarNode('node_with_a_looong_name')->end()
3941
->enumNode('enum_with_default')->values(array('this', 'that'))->defaultValue('this')->end()
4042
->enumNode('enum')->values(array('this', 'that'))->end()

0 commit comments

Comments
 (0)