Skip to content

[Config] Fix YamlReferenceDumper prototyped array support #19570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\EnumNode;
use Symfony\Component\Config\Definition\PrototypedArrayNode;
use Symfony\Component\Config\Definition\ScalarNode;
use Symfony\Component\Yaml\Inline;

/**
Expand Down Expand Up @@ -45,8 +46,9 @@ public function dumpNode(NodeInterface $node)
/**
* @param NodeInterface $node
* @param int $depth
* @param bool $prototypedArray
*/
private function writeNode(NodeInterface $node, $depth = 0)
private function writeNode(NodeInterface $node, $depth = 0, $prototypedArray = false)
{
$comments = array();
$default = '';
Expand All @@ -59,29 +61,7 @@ private function writeNode(NodeInterface $node, $depth = 0)
$children = $node->getChildren();

if ($node instanceof PrototypedArrayNode) {
$prototype = $node->getPrototype();

if ($prototype instanceof ArrayNode) {
$children = $prototype->getChildren();
}

// check for attribute as key
if ($key = $node->getKeyAttribute()) {
$keyNodeClass = 'Symfony\Component\Config\Definition\\'.($prototype instanceof ArrayNode ? 'ArrayNode' : 'ScalarNode');
$keyNode = new $keyNodeClass($key, $node);

$info = 'Prototype';
if (null !== $prototype->getInfo()) {
$info .= ': '.$prototype->getInfo();
}
$keyNode->setInfo($info);

// add children
foreach ($children as $childNode) {
$keyNode->addChild($childNode);
}
$children = array($key => $keyNode);
}
$children = $this->getPrototypeChildren($node);
}

if (!$children) {
Expand Down Expand Up @@ -125,7 +105,8 @@ private function writeNode(NodeInterface $node, $depth = 0)
$default = (string) $default != '' ? ' '.$default : '';
$comments = count($comments) ? '# '.implode(', ', $comments) : '';

$text = rtrim(sprintf('%-20s %s %s', $node->getName().':', $default, $comments), ' ');
$key = $prototypedArray ? '-' : $node->getName().':';
$text = rtrim(sprintf('%-20s %s %s', $key, $default, $comments), ' ');

if ($info = $node->getInfo()) {
$this->writeLine('');
Expand Down Expand Up @@ -159,7 +140,7 @@ private function writeNode(NodeInterface $node, $depth = 0)

if ($children) {
foreach ($children as $childNode) {
$this->writeNode($childNode, $depth + 1);
$this->writeNode($childNode, $depth + 1, $node instanceof PrototypedArrayNode && !$node->getKeyAttribute());
}
}
}
Expand Down Expand Up @@ -200,4 +181,38 @@ private function writeArray(array $array, $depth)
}
}
}

/**
* @param PrototypedArrayNode $node
*
* @return array
*/
private function getPrototypeChildren(PrototypedArrayNode $node)
{
$prototype = $node->getPrototype();
$key = $node->getKeyAttribute();

// Do not expand prototype if it isn't an array node nor uses attribute as key
if (!$key && !$prototype instanceof ArrayNode) {
return $node->getChildren();
}

if ($prototype instanceof ArrayNode) {
$keyNode = new ArrayNode($key, $node);
// add children
foreach ($prototype->getChildren() as $childNode) {
$keyNode->addChild($childNode);
}
} else {
$keyNode = new ScalarNode($key, $node);
}

$info = 'Prototype';
if (null !== $prototype->getInfo()) {
$info .= ': '.$prototype->getInfo();
}
$keyNode->setInfo($info);

return array($key => $keyNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ enum=""
child3=""
/>

<!-- prototype -->
<scalar-prototyped>scalar value</scalar-prototyped>

<!-- prototype: Parameter name -->
<parameter name="parameter name">scalar value</parameter>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function testDumper()

$dumper = new YamlReferenceDumper();

$this->markTestIncomplete('The Yaml Dumper currently does not support prototyped arrays');
$this->assertEquals($this->getConfigurationAsString(), $dumper->dump($configuration));
}

Expand All @@ -32,7 +31,7 @@ private function getConfigurationAsString()
acme_root:
boolean: true
scalar_empty: ~
scalar_null: ~
scalar_null: null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it dumped as null here while all other places using null are dumping it as ~ ?

Copy link
Contributor Author

@ogizanagi ogizanagi Aug 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the ExampleConfiguration explicitly sets the default value to null.
When setting a default value, the YamlReferenceDumper will replace the default ~ by this value :)

(This is not related to the changes in this PR, though)

scalar_true: true
scalar_false: false
scalar_default: default
Expand All @@ -55,13 +54,17 @@ enum: ~ # One of "this"; "that"
# multi-line info text
# which should be indented
child3: ~ # Example: example setting
scalar_prototyped: []
parameters:

# Prototype: Parameter name
name: ~
connections:

# Prototype
- { user: ~, pass: ~ }
-
user: ~
pass: ~

EOL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('scalar_prototyped')
->prototype('scalar')->end()
->end()
->arrayNode('parameters')
->useAttributeAsKey('name')
->prototype('scalar')->info('Parameter name')->end()
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Config/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"php": ">=5.5.9",
"symfony/filesystem": "~2.8|~3.0"
},
"require-dev": {
"symfony/yaml": "~3.0"
},
"suggest": {
"symfony/yaml": "To use the yaml reference dumper"
},
Expand Down