10000 Fixed support for nodes not extending BaseNode · symfony/symfony@6827656 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6827656

Browse files
committed
Fixed support for nodes not extending BaseNode
1 parent 09fe733 commit 6827656

File tree

9 files changed

+114
-39
lines changed

9 files changed

+114
-39
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected function preNormalize($value)
6868
/**
6969
* Retrieves the children of this node.
7070
*
71-
* @return array The children
71+
* @return array<string, NodeInterface>
7272
*/
7373
public function getChildren()
7474
{

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ public function getNode($forceRootNode = false)
127127
}
128128

129129
$node = $this->createNode();
130-
$node->setAttributes($this->attributes);
130+
if ($node instanceof BaseNode) {
131+
$node->setAttributes($this->attributes);
132+
}
131133

132134
return $node;
133135
}

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

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Config\Definition\Dumper;
1313

1414
use Symfony\Component\Config\Definition\ArrayNode;
15+
use Symfony\Component\Config\Definition\BaseNode;
1516
use Symfony\Component\Config\Definition\ConfigurationInterface;
1617
use Symfony\Component\Config\Definition\EnumNode;
1718
use Symfony\Component\Config\Definition\NodeInterface;
@@ -126,50 +127,52 @@ private function writeNode(NodeInterface $node, int $depth = 0, bool $root = fal
126127

127128
// get attributes and elements
128129
foreach ($children as $child) {
129-
if (!$child instanceof ArrayNode) {
130-
// get attributes
130+
if ($child instanceof ArrayNode) {
131+
// get elements
132+
$rootChildren[] = $child;
131133

132-
// metadata
133-
$name = str_replace('_', '-', $child->getName());
134-
$value = '%%%%not_defined%%%%'; // use a string which isn't used in the normal world
134+
continue;
135+
}
135136

136-
// comments
137-
$comments = [];
138-
if ($info = $child->getInfo()) {
139-
$comments[] = $info;
140-
}
137+
// get attributes
141138

142-
if ($example = $child->getExample()) {
143-
$comments[] = 'Example: '.$example;
144-
}
139+
// metadata
140+
$name = str_replace('_', '-', $child->getName());
141+
$value = '%%%%not_defined%%%%'; // use a string which isn't used in the normal world
145142

146-
if ($child->isRequired()) {
147-
$comments[] = 'Required';
148-
}
143+
// comments
144+
$comments = [];
145+
if ($child instanceof BaseNode && $info = $child->getInfo()) {
146+
$comments[] = $info;
147+
}
149148

150-
if ($child->isDeprecated()) {
151-
$comments[] = sprintf('Deprecated (%s)', $child->getDeprecationMessage($child->getName(), $node->getPath()));
152-
}
149+
if ($child instanceof BaseNode && $example = $child->getExample()) {
150+
$comments[] = 'Example: '.$example;
151+
}
153152

154-
if ($child instanceof EnumNode) {
155-
$comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues()));
156-
}
153+
if ($child->isRequired()) {
154+
$comments[] = 'Required';
155+
}
157156

158-
if (\count($comments)) {
159-
$rootAttributeComments[$name] = implode(";\n", $comments);
160-
}
157+
if ($child instanceof BaseNode && $child->isDeprecated()) {
158+
$comments[] = sprintf('Deprecated (%s)', $child->getDeprecationMessage($child->getName(), $node->getPath()));
159+
}
161160

162-
// default values
163-
if ($child->hasDefaultValue()) {
164-
$value = $child->getDefaultValue();
165-
}
161+
if ($child instanceof EnumNode) {
162+
$comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues()));
163+
}
166164

167-
// append attribute
168-
$rootAttributes[$name] = $value;
169-
} else {
170-
// get elements
171-
$rootChildren[] = $child;
165+
if (\count($comments)) {
166+
$rootAttributeComments[$name] = implode(";\n", $comments);
172167
}
168+
169+
// default values
170+
if ($child->hasDefaultValue()) {
171+
$value = $child->getDefaultValue();
172+
}
173+
174+
// append attribute
175+
$rootAttributes[$name] = $value;
173176
}
174177
}
175178

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Config\Definition\Dumper;
1313

1414
use Symfony\Component\Config\Definition\ArrayNode;
15+
use Symfony\Component\Config\Definition\BaseNode;
1516
use Symfony\Component\Config\Definition\ConfigurationInterface;
1617
use Symfony\Component\Config\Definition\EnumNode;
1718
use Symfony\Component\Config\Definition\NodeInterface;
@@ -76,7 +77,10 @@ private function writeNode(NodeInterface $node, NodeInterface $parentNode = null
7677
$default = '';
7778
$defaultArray = null;
7879
$children = null;
79-
$example = $node->getExample();
80+
$example = null;
81+
if ($node instanceof BaseNode) {
82+
$example = $node->getExample();
83+
}
8084

8185
// defaults
8286
if ($node instanceof ArrayNode) {
@@ -123,7 +127,7 @@ private function writeNode(NodeInterface $node, NodeInterface $parentNode = null
123127
}
124128

125129
// deprecated?
126-
if ($node->isDeprecated()) {
130+
if ($node instanceof BaseNode && $node->isDeprecated()) {
127131
$comments[] = sprintf('Deprecated (%s)', $node->getDeprecationMessage($node->getName(), $parentNode ? $parentNode->getPath() : $node->getPath()));
128132
}
129133

@@ -138,7 +142,7 @@ private function writeNode(NodeInterface $node, NodeInterface $parentNode = null
138142
$key = $prototypedArray ? '-' : $node->getName().':';
139143
$text = rtrim(sprintf('%-21s%s %s', $key, $default, $comments), ' ');
140144

141-
if ($info = $node->getInfo()) {
145+
if ($node instanceof BaseNode && $info = $node->getInfo()) {
142146
$this->writeLine('');
143147
// indenting multi-line info
144148
$info = str_replace("\n", sprintf("\n%".($depth * 4).'s# ', ' '), $info);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ private function getConfigurationAsString()
5757
node-with-a-looong-name=""
5858
enum-with-default="this"
5959
enum=""
60+
custom-node="true"
6061
>
6162
6263
<!-- some info -->

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ enum: ~ # One of "this"; "that"
137137
138138
# Prototype
139139
name: []
140+
custom_node: true
140141

141142
EOL;
142143
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
4+
namespace Symfony\Component\Config\Tests\Fixtures\Configuration;
5+
6+
use Symfony\Component\Config\Definition\NodeInterface;
7+
8+
class CustomNode implements NodeInterface
9+
{
10+
public function getName()
11+
{
12+
return 'custom_node';
13+
}
14+
15+
public function getPath()
16+
{
17+
return 'custom';
18+
}
19+
20+
public function isRequired()
21+
{
22+
return false;
23+
}
24+
25+
public function hasDefaultValue()
26+
{
27+
return true;
28+
}
29+
30+
public function getDefaultValue()
31+
{
32+
return true;
33+
}
34+
35+
public function normalize($value)
36+
{
37+
return $value;
38+
}
39+
40+
public function merge($leftSide, $rightSide)
41+
{
42+
return array_merge($leftSide, $rightSide);
43+
}
44+
45+
public function finalize($value)
46+
{
47+
return $value;
48+
}
49+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
4+
namespace Symfony\Component\Config\Tests\Fixtures\Configuration;
5+
6+
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
7+
8+
class CustomNodeDefinition extends NodeDefinition
9+
{
10+
protected function createNode()
11+
{
12+
return new CustomNode();
13+
}
14+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function getConfigTreeBuilder(): TreeBuilder
9393
->end()
9494
->end()
9595
->end()
96+
->append(new CustomNodeDefinition('acme'))
9697
->end()
9798
;
9899

0 commit comments

Comments
 (0)
0