Skip to content

Commit

Permalink
feature #19570 [Config] Fix YamlReferenceDumper prototyped array supp…
Browse files Browse the repository at this point in the history
…ort (ogizanagi)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[Config] Fix YamlReferenceDumper prototyped array support

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Also related to #19480 which fixes another prototype issue, but cannot be tested properly on Travis because marked as skipped by this missing implementation.

Previous output was:

```yaml
    [...]
    parameters:

        # Prototype
        name:                 ~
    connections:
        user:                 ~
        pass:                 ~
```

instead of:

```yaml
    [...]
    parameters:

        # Prototype
        name:                 ~
    connections:

        # Prototype
        -
            user:                 ~
            pass:                 ~
```

Commits
-------

063a980 [Config] Fix YamlReferenceDumper prototyped array support
  • Loading branch information
fabpot committed Sep 14, 2016
2 parents 7136247 + 063a980 commit db2e2c8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 29 deletions.
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);
}
}
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
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
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
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
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

0 comments on commit db2e2c8

Please sign in to comment.