Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 869bb15

Browse files
committed
[Config][FrameworkBundle] Allow to dump extension config reference sub path
1 parent 9e6d6ba commit 869bb15

File tree

6 files changed

+146
-4
lines changed

6 files changed

+146
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected function configure()
3737
->setName('config:dump-reference')
3838
->setDefinition(array(
3939
new InputArgument('name', InputArgument::OPTIONAL, 'The Bundle name or the extension alias'),
40+
new InputArgument('path', InputArgument::OPTIONAL, 'The configuration option path'),
4041
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (yaml or xml)', 'yaml'),
4142
))
4243
->setDescription('Dumps the default configuration for an extension')
@@ -54,6 +55,10 @@ protected function configure()
5455
When the option is not provided, <comment>yaml</comment> is used.
5556
5657
<info>php %command.full_name% FrameworkBundle --format=xml</info>
58+
59+
For dumping a specific option, add its path as second argument (only available for the yaml format):
60+
61+
<info>php %command.full_name% framework profiler.matcher</info>
5762

5863
EOF
5964
)
@@ -71,7 +76,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
7176

7277
if (null === $name = $input->getArgument('name')) {
7378
$this->listBundles($io);
74-
$io->comment('Provide the name of a bundle as the first argument of this command to dump its default configuration. (e.g. <comment>config:dump-reference FrameworkBundle</comment>)');
79+
$io->comment(array(
80+
'Provide the name of a bundle as the first argument of this command to dump its default configuration. (e.g. <comment>config:dump-reference FrameworkBundle</comment>)',
81+
'For dumping a specific option, add its path as the second argument of this command. (e.g. <comment>config:dump-reference FrameworkBundle profiler.matcher</comment> to dump the <comment>framework.profiler.matcher</comment> configuration)',
82+
));
7583

7684
return;
7785
}
@@ -82,13 +90,26 @@ protected function execute(InputInterface $input, OutputInterface $output)
8290

8391
$this->validateConfiguration($extension, $configuration);
8492

93+
$format = $input->getOption('format');
94+
$path = $input->getArgument('path');
95+
96+
if ($path !== null && 'yaml' !== $format) {
97+
$io->error('The "path" option is only available for the "yaml" format.');
98+
99+
return 1;
100+
}
101+
85102
if ($name === $extension->getAlias()) {
86103
$message = sprintf('Default configuration for extension with alias: "%s"', $name);
87104
} else {
88105
$message = sprintf('Default configuration for "%s"', $name);
89106
}
90107

91-
switch ($input->getOption('format')) {
108+
if ($path !== null) {
109+
$message .= sprintf(' at path "%s"', $path);
110+
}
111+
112+
switch ($format) {
92113
case 'yaml':
93114
$io->writeln(sprintf('# %s', $message));
94115
$dumper = new YamlReferenceDumper();
@@ -102,6 +123,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
102123
throw new \InvalidArgumentException('Only the yaml and xml formats are supported.');
103124
}
104125

105-
$io->writeln($dumper->dump($configuration, $extension->getNamespace()));
126+
$io->writeln(null === $path ? $dumper->dump($configuration, $extension->getNamespace()) : $dumper->dumpAtPath($configuration, $path));
106127
}
107128
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/DependencyInjection/Config/CustomConfig.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public function addConfiguration($rootNode)
1818
$rootNode
1919
->children()
2020
->scalarNode('custom')->end()
21+
->arrayNode('array')
22+
->children()
23+
->scalarNode('child1')->end()
24+
->scalarNode('child2')->end()
25+
->end()
26+
->end()
27+
->end()
2128
->end()
2229
;
2330
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDumpReferenceCommandTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,39 @@ public function testDumpBundleName()
4040
$this->assertContains(' custom:', $tester->getDisplay());
4141
}
4242

43+
public function testDumpAtPath()
44+
{
45+
$tester = $this->createCommandTester();
46+
$ret = $tester->execute(array(
47+
'name' => 'test',
48+
'path' => 'array',
49+
));
50+
51+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
52+
$this->assertSame(<<<'EOL'
53+
# Default configuration for extension with alias: "test" at path "array"
54+
array:
55+
child1: ~
56+
child2: ~
57+
58+
59+
EOL
60+
, $tester->getDisplay(true));
61+
}
62+
63+
public function testDumpAtPathXml()
64+
{
65+
$tester = $this->createCommandTester();
66+
$ret = $tester->execute(array(
67+
'name' => 'test',
68+
'path' => 'array',
69+
'--format' => 'xml',
70+
));
71+
72+
$this->assertSame(1, $ret);
73+
$this->assertContains('[ERROR] The "path" option is only available for the "yaml" format.', $tester->getDisplay());
74+
}
75+
4376
/**
4477
* @return CommandTester
4578
*/

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"symfony/cache": "~3.3",
2121
"symfony/class-loader": "~3.2",
2222
"symfony/dependency-injection": "~3.3",
23-
"symfony/config": "~2.8|~3.0",
23+
"symfony/config": "~3.3",
2424
"symfony/event-dispatcher": "~2.8|~3.0",
2525
"symfony/http-foundation": "~3.1",
2626
"symfony/http-kernel": "~3.3",

src/Symfony/Component/Config/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 = '';

src/Symfony/Component/Config/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)