Skip to content

Commit 72840c1

Browse files
committed
Merge branch '5.4' into 6.4
* 5.4: [Config] Fix `YamlReferenceDumper` handling of array examples
2 parents c556c10 + 7ba3d8e commit 72840c1

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\Config\Definition\NodeInterface;
1919
use Symfony\Component\Config\Definition\PrototypedArrayNode;
2020
use Symfony\Component\Config\Definition\ScalarNode;
21-
use Symfony\Component\Config\Definition\VariableNode;
2221
use Symfony\Component\Yaml\Inline;
2322

2423
/**
@@ -99,19 +98,12 @@ private function writeNode(NodeInterface $node, ?NodeInterface $parentNode = nul
9998
$children = $this->getPrototypeChildren($node);
10099
}
101100

102-
if (!$children) {
103-
if ($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue())) {
104-
$default = '';
105-
} elseif (!\is_array($example)) {
106-
$default = '[]';
107-
}
101+
if (!$children && !($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue()))) {
102+
$default = '[]';
108103
}
109104
} elseif ($node instanceof EnumNode) {
110105
$comments[] = 'One of '.$node->getPermissibleValues('; ');
111106
$default = $node->hasDefaultValue() ? Inline::dump($node->getDefaultValue()) : '~';
112-
} elseif (VariableNode::class === $node::class && \is_array($example)) {
113-
// If there is an array example, we are sure we dont need to print a default value
114-
$default = '';
115107
} else {
116108
$default = '~';
117109

@@ -179,7 +171,7 @@ private function writeNode(NodeInterface $node, ?NodeInterface $parentNode = nul
179171

180172
$this->writeLine('# '.$message.':', $depth * 4 + 4);
181173

182-
$this->writeArray(array_map(Inline::dump(...), $example), $depth + 1);
174+
$this->writeArray(array_map(Inline::dump(...), $example), $depth + 1, true);
183175
}
184176

185177
if ($children) {
@@ -200,7 +192,7 @@ private function writeLine(string $text, int $indent = 0): void
200192
$this->reference .= sprintf($format, $text)."\n";
201193
}
202194

203-
private function writeArray(array $array, int $depth): void
195+
private function writeArray(array $array, int $depth, bool $asComment = false): void
204196
{
205197
$isIndexed = array_is_list($array);
206198

@@ -211,14 +203,16 @@ private function writeArray(array $array, int $depth): void
211203
$val = $value;
212204
}
213205

206+
$prefix = $asComment ? '# ' : '';
207+
214208
if ($isIndexed) {
215-
$this->writeLine('- '.$val, $depth * 4);
209+
$this->writeLine($prefix.'- '.$val, $depth * 4);
216210
} else {
217-
$this->writeLine(sprintf('%-20s %s', $key.':', $val), $depth * 4);
211+
$this->writeLine(sprintf('%s%-20s %s', $prefix, $key.':', $val), $depth * 4);
218212
}
219213

220214
if (\is_array($value)) {
221-
$this->writeArray($value, $depth + 1);
215+
$this->writeArray($value, $depth + 1, $asComment);
222216
}
223217
}
224218
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ enum=""
109109
110110
</pipou>
111111
112+
<array-with-array-example-and-no-default-value />
113+
112114
</config>
113115

114116
EOL

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ enum: ~ # One of "this"; "that"; Symfony\Component\Config\Tests\
114114
# which should be indented
115115
child3: ~ # Example: 'example setting'
116116
scalar_prototyped: []
117-
variable:
117+
variable: ~
118118
119119
# Examples:
120-
- foo
121-
- bar
120+
# - foo
121+
# - bar
122122
parameters:
123123
124124
# Prototype: Parameter name
@@ -142,6 +142,11 @@ enum: ~ # One of "this"; "that"; Symfony\Component\Config\Tests\
142142
143143
# Prototype
144144
name: []
145+
array_with_array_example_and_no_default_value: []
146+
147+
# Examples:
148+
# - foo
149+
# - bar
145150
custom_node: true
146151

147152
EOL;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public function getConfigTreeBuilder(): TreeBuilder
9797
->end()
9898
->end()
9999
->end()
100+
->arrayNode('array_with_array_example_and_no_default_value')
101+
->example(['foo', 'bar'])
102+
->end()
100103
->append(new CustomNodeDefinition('acme'))
101104
->end()
102105
;

0 commit comments

Comments
 (0)