Skip to content

Commit d87858e

Browse files
bug symfony#24083 Fix ability to deprecate a config node (chalasr)
This PR was merged into the 3.4 branch. Discussion ---------- Fix ability to deprecate a config node | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | not yet released | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#22382 | License | MIT | Doc PR | n/a Unlocks symfony#24025 Commits ------- fc91869 Fix ability to deprecate a config node
2 parents b80d8c8 + fc91869 commit d87858e

File tree

7 files changed

+60
-0
lines changed

7 files changed

+60
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ protected function createNode()
453453
$node->addEquivalentValue(false, $this->falseEquivalent);
454454
$node->setPerformDeepMerging($this->performDeepMerging);
455455
$node->setRequired($this->required);
456+
$node->setDeprecated($this->deprecationMessage);
456457
$node->setIgnoreExtraKeys($this->ignoreExtraKeys, $this->removeExtraKeys);
457458
$node->setNormalizeKeys($this->normalizeKeys);
458459

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ protected function validateType($value)
8484
*/
8585
protected function finalizeValue($value)
8686
{
87+
if ($this->deprecationMessage) {
88+
@trigger_error($this->getDeprecationMessage($this->getName(), $this->getPath()), E_USER_DEPRECATED);
89+
}
90+
8791
if (!$this->allowEmptyValue && $this->isValueEmpty($value)) {
8892
$ex = new InvalidConfigurationException(sprintf(
8993
'The path "%s" cannot contain an empty value, but got %s.',

src/Symfony/Component/Config/Tests/Definition/ArrayNodeTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,13 @@ public function testGetDefaultValueWithoutDefaultValue()
216216
$node = new ArrayNode('foo');
217217
$node->getDefaultValue();
218218
}
219+
220+
public function testSetDeprecated()
221+
{
222+
$node = new ArrayNode('foo');
223+
$node->setDeprecated('"%node%" is deprecated');
224+
225+
$this->assertTrue($node->isDeprecated());
226+
$this->assertSame('"foo" is deprecated', $node->getDeprecationMessage($node->getName(), $node->getPath()));
227+
}
219228
}

src/Symfony/Component/Config/Tests/Definition/Builder/ArrayNodeDefinitionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,20 @@ public function testRequiresAtLeastOneElement()
298298
$this->addToAssertionCount(1);
299299
}
300300

301+
public function testSetDeprecated()
302+
{
303+
$node = new ArrayNodeDefinition('root');
304+
$node
305+
->children()
306+
->arrayNode('foo')->setDeprecated('The "%path%" node is deprecated.')->end()
307+
->end()
308+
;
309+
$deprecatedNode = $node->getNode()->getChildren()['foo'];
310+
311+
$this->assertTrue($deprecatedNode->isDeprecated());
312+
$this->assertSame('The "root.foo" node is deprecated.', $deprecatedNode->getDeprecationMessage($deprecatedNode->getName(), $deprecatedNode->getPath()));
313+
}
314+
301315
/**
302316
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
303317
* @expectedExceptionMessage The path "root" should have at least 1 element(s) defined.

src/Symfony/Component/Config/Tests/Definition/Builder/BooleanNodeDefinitionTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,15 @@ public function testCannotBeEmptyThrowsAnException()
2525
$def = new BooleanNodeDefinition('foo');
2626
$def->cannotBeEmpty();
2727
}
28+
29+
public function testSetDeprecated()
30+
{
31+
$def = new BooleanNodeDefinition('foo');
32+
$def->setDeprecated('The "%path%" node is deprecated.');
33+
34+
$node = $def->getNode();
35+
36+
$this->assertTrue($node->isDeprecated());
37+
$this->assertSame('The "foo" node is deprecated.', $node->getDeprecationMessage($node->getName(), $node->getPath()));
38+
}
2839
}

src/Symfony/Component/Config/Tests/Definition/Builder/EnumNodeDefinitionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,16 @@ public function testGetNode()
6262
$node = $def->getNode();
6363
$this->assertEquals(array('foo', 'bar'), $node->getValues());
6464
}
65+
66+
public function testSetDeprecated()
67+
{
68+
$def = new EnumNodeDefinition('foo');
69+
$def->values(array('foo', 'bar'));
70+
$def->setDeprecated('The "%path%" node is deprecated.');
71+
72+
$node = $def->getNode();
73+
74+
$this->assertTrue($node->isDeprecated());
75+
$this->assertSame('The "foo" node is deprecated.', $def->getNode()->getDeprecationMessage($node->getName(), $node->getPath()));
76+
}
6577
}

src/Symfony/Component/Config/Tests/Definition/ScalarNodeTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public function getValidValues()
4040
);
4141
}
4242

43+
public function testSetDeprecated()
44+
{
45+
$node = new ScalarNode('foo');
46+
$node->setDeprecated('"%node%" is deprecated');
47+
48+
$this->assertTrue($node->isDeprecated());
49+
$this->assertSame('"foo" is deprecated', $node->getDeprecationMessage($node->getName(), $node->getPath()));
50+
}
51+
4352
/**
4453
* @dataProvider getInvalidValues
4554
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException

0 commit comments

Comments
 (0)