Skip to content

Commit ba3da26

Browse files
committed
[Config][FrameworkBundle] Allow to dump extension config reference sub path
1 parent 7cf257a commit ba3da26

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

Definition/Dumper/YamlReferenceDumper.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ public function dump(ConfigurationInterface $configuration)
3333
return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree());
3434
}
3535

36+
public function dumpAtPath(ConfigurationInterface $configuration, $path)
37+
{
38+
$rootNode = $node = $configuration->getConfigTreeBuilder()->buildTree();
39+
40+
foreach (explode('.', $path) as $step) {
41+
if (!$node instanceof ArrayNode) {
42+
throw new \UnexpectedValueException(sprintf('Unable to find node at path "%s.%s"', $rootNode->getName(), $path));
43+
}
44+
45+
/** @var NodeInterface[] $children */
46+
$children = $node instanceof PrototypedArrayNode ? $this->getPrototypeChildren($node) : $node->getChildren();
47+
48+
foreach ($children as $child) {
49+
if ($child->getName() === $step) {
50+
$node = $child;
51+
52+
continue 2;
53+
}
54+
}
55+
56+
throw new \UnexpectedValueException(sprintf('Unable to find node at path "%s.%s"', $rootNode->getName(), $path));
57+
}
58+
59+
return $this->dumpNode($node);
60+
}
61+
3662
public function dumpNode(NodeInterface $node)
3763
{
3864
$this->reference = '';

Tests/Definition/Dumper/YamlReferenceDumperTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,61 @@ public function testDumper()
2525
$this->assertEquals($this->getConfigurationAsString(), $dumper->dump($configuration));
2626
}
2727

28+
public function provideDumpAtPath()
29+
{
30+
return array(
31+
'Regular node' => array('scalar_true', <<<EOL
32+
scalar_true: true
33+
EOL
34+
),
35+
'Array node' => array('array', <<<EOL
36+
# some info
37+
array:
38+
child1: ~
39+
child2: ~
40+
41+
# this is a long
42+
# multi-line info text
43+
# which should be indented
44+
child3: ~ # Example: example setting
45+
EOL
46+
),
47+
'Regular nested' => array('array.child2', <<<EOL
48+
child2: ~
49+
EOL
50+
),
51+
'Prototype' => array('cms_pages.page', <<<EOL
52+
# Prototype
53+
page:
54+
55+
# Prototype
56+
locale:
57+
title: ~ # Required
58+
path: ~ # Required
59+
EOL
60+
),
61+
'Nested prototype' => array('cms_pages.page.locale', <<<EOL
62+
# Prototype
63+
locale:
64+
title: ~ # Required
65+
path: ~ # Required
66+
EOL
67+
),
68+
);
69+
}
70+
71+
/**
72+
* @dataProvider provideDumpAtPath
73+
*/
74+
public function testDumpAtPath($path, $expected)
75+
{
76+
$configuration = new ExampleConfiguration();
77+
78+
$dumper = new YamlReferenceDumper();
79+
80+
$this->assertSame(trim($expected), trim($dumper->dumpAtPath($configuration, $path)));
81+
}
82+
2883
private function getConfigurationAsString()
2984
{
3085
return <<<'EOL'

0 commit comments

Comments
 (0)