Skip to content

Commit 79baf8d

Browse files
committed
feature #10165 [FrameworkBundle] config:dump-reference command can now dump current configuration (lyrixx)
This PR was merged into the 2.5-dev branch. Discussion ---------- [FrameworkBundle] config:dump-reference command can now dump current configuration | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | I guess | Fixed tickets | - | License | MIT | Doc PR | - I did not add `xml` support as I don't know how to do that. ping @stof sample: ```yaml framework: secret: ThisTokenIsNotSoSecretChangeIt router: resource: /home/greg/dev/labs/se/app/config/routing_dev.yml strict_requirements: true http_port: 80 https_port: 443 form: enabled: true csrf_protection: { enabled: null, field_name: null } csrf_protection: enabled: true field_name: _token validation: enable_annotations: true enabled: true translation_domain: validators templating: engines: [twig] assets_version: null assets_version_format: '%%s?%%s' hinclude_default_template: null form: { resources: ['FrameworkBundle:Form'] } assets_base_urls: { http: { }, ssl: { } } loaders: { } packages: { } default_locale: en trusted_hosts: { } trusted_proxies: { } session: storage_id: session.storage.native handler_id: session.handler.native_file save_path: /home/greg/dev/labs/se/app/cache/dev/sessions metadata_update_threshold: '0' fragments: enabled: true path: /_fragment http_method_override: true profiler: only_exceptions: false enabled: true collect: true only_master_requests: false dsn: 'file:/home/greg/dev/labs/se/app/cache/dev/profiler' username: '' password: '' lifetime: 86400 ide: null esi: enabled: false translator: enabled: false fallback: en annotations: cache: file file_cache_dir: /home/greg/dev/labs/se/app/cache/dev/annotations debug: true serializer: enabled: false ``` Commits ------- 19a368e [FramworkBundle] Added config:debug command
2 parents aca3271 + 19a368e commit 79baf8d

File tree

5 files changed

+193
-54
lines changed

5 files changed

+193
-54
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
2.5.0
55
-----
66

7+
* Added `config:debug` command
78
* Added `yaml:lint` command
89
* Deprecated the `RouterApacheDumperCommand` which will be removed in Symfony 3.0.
910

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Command;
13+
14+
use Symfony\Component\Config\Definition\ConfigurationInterface;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
use Symfony\Component\DependencyInjection\Extension\Extension;
17+
18+
/**
19+
* A console command for dumping available configuration reference
20+
*
21+
* @author Kevin Bond <kevinbond@gmail.com>
22+
* @author Wouter J <waldio.webdesign@gmail.com>
23+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
24+
*/
25+
class AbstractConfigCommand extends ContainerDebugCommand
26+
{
27+
protected function listBundles(OutputInterface $output)
28+
{
29+
$output->writeln('Available registered bundles with their extension alias if available:');
30+
31+
$table = $this->getHelperSet()->get('table');
32+
$table->setHeaders(array('Bundle name', 'Extension alias'));
33+
foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) {
34+
$extension = $bundle->getContainerExtension();
35+
$table->addRow(array($bundle->getName(), $extension ? $extension->getAlias() : ''));
36+
}
37+
38+
$table->render($output);
39+
}
40+
41+
protected function findExtention($name)
42+
{
43+
$extension = null;
44+
45+
$bundles = $this->getContainer()->get('kernel')->getBundles();
46+
47+
if (preg_match('/Bundle$/', $name)) {
48+
// input is bundle name
49+
50+
if (isset($bundles[$name])) {
51+
$extension = $bundles[$name]->getContainerExtension();
52+
}
53+
54+
if (!$extension) {
55+
throw new \LogicException(sprintf('No extensions with configuration available for "%s"', $name));
56+
}
57+
} else {
58+
foreach ($bundles as $bundle) {
59+
$extension = $bundle->getContainerExtension();
60+
61+
if ($extension && $name === $extension->getAlias()) {
62+
break;
63+
}
64+
65+
$extension = null;
66+
}
67+
68+
if (!$extension) {
69+
throw new \LogicException(sprintf('No extension with alias "%s" is enabled', $name));
70+
}
71+
}
72+
73+
return $extension;
74+
}
75+
76+
public function validateConfiguration(Extension $extension, $configuration)
77+
{
78+
if (!$configuration) {
79+
throw new \LogicException(sprintf('The extension with alias "%s" does not have its getConfiguration() method setup', $extension->getAlias()));
80+
}
81+
82+
if (!$configuration instanceof ConfigurationInterface) {
83+
throw new \LogicException(sprintf('Configuration class "%s" should implement ConfigurationInterface in order to be dumpable', get_class($configuration)));
84+
}
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Command;
13+
14+
use Symfony\Component\Config\Definition\Processor;
15+
use Symfony\Component\Console\Input\InputArgument;
16+
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19+
use Symfony\Component\Yaml\Yaml;
20+
21+
/**
22+
* A console command for dumping available configuration reference
23+
*
24+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
25+
*/
26+
class ConfigDebugCommand extends AbstractConfigCommand
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function configure()
32+
{
33+
$this
34+
->setName('config:debug')
35+
->setDefinition(array(
36+
new InputArgument('name', InputArgument::OPTIONAL, 'The Bundle name or the extension alias'),
37+
))
38+
->setDescription('Dumps the current configuration for an extension')
39+
->setHelp(<<<EOF
40+
41+
The <info>%command.name%</info> command dumps the current configuration for an
42+
extension/bundle.
43+
44+
Either the extension alias or bundle name can be used:
45+
46+
<info>php %command.full_name% framework</info>
47+
<info>php %command.full_name% FrameworkBundle</info>
48+
49+
EOF
50+
)
51+
;
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
protected function execute(InputInterface $input, OutputInterface $output)
58+
{
59+
$name = $input->getArgument('name');
60+
61+
if (empty($name)) {
62+
$this->listBundles($output);
63+
64+
return;
65+
}
66+
67+
$extension = $this->findExtention($name);
68+
69+
$kernel = $this->getContainer()->get('kernel');
70+
$method = new \ReflectionMethod($kernel, 'buildContainer');
71+
$method->setAccessible(true);
72+
$container = $method->invoke($kernel);
73+
74+
$configs = $container->getExtensionConfig($extension->getAlias());
75+
$configuration = $extension->getConfiguration($configs, $container);
76+
77+
$this->validateConfiguration($extension, $configuration);
78+
79+
$processor = new Processor();
80+
$config = $processor->processConfiguration($configuration, $configs);
81+
82+
$config = $container->getParameterBag()->resolveValue($config);
83+
84+
if ($name === $extension->getAlias()) {
85+
$output->writeln(sprintf('# Current configuration for extension with alias: "%s"', $name));
86+
} else {
87+
$output->writeln(sprintf('# Current configuration for "%s"', $name));
88+
}
89+
90+
$output->writeln(Yaml::dump(array($extension->getAlias() => $config), 3));
91+
}
92+
}

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

+14-52
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
use Symfony\Component\Console\Input\InputOption;
1818
use Symfony\Component\Console\Input\InputInterface;
1919
use Symfony\Component\Console\Output\OutputInterface;
20-
use Symfony\Component\Config\Definition\ConfigurationInterface;
2120

2221
/**
2322
* A console command for dumping available configuration reference
2423
*
2524
* @author Kevin Bond <kevinbond@gmail.com>
2625
* @author Wouter J <waldio.webdesign@gmail.com>
26+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
2727
*/
28-
class ConfigDumpReferenceCommand extends ContainerDebugCommand
28+
class ConfigDumpReferenceCommand extends AbstractConfigCommand
2929
{
3030
/**
3131
* {@inheritdoc}
@@ -43,13 +43,16 @@ protected function configure()
4343
The <info>%command.name%</info> command dumps the default configuration for an
4444
extension/bundle.
4545
46-
The extension alias or bundle name can be used:
46+
Either the extension alias or bundle name can be used:
4747
4848
<info>php %command.full_name% framework</info>
4949
<info>php %command.full_name% FrameworkBundle</info>
5050
5151
With the <info>format</info> option specifies the format of the configuration,
52-
this is either <comment>yaml</comment> or <comment>xml</comment>. When the option is not provided, <comment>yaml</comment> is used.
52+
this is either <comment>yaml</comment> or <comment>xml</comment>.
53+
When the option is not provided, <comment>yaml</comment> is used.
54+
55+
<info>php %command.full_name% FrameworkBundle --format=xml</info>
5356
EOF
5457
)
5558
;
@@ -62,65 +65,24 @@ protected function configure()
6265
*/
6366
protected function execute(InputInterface $input, OutputInterface $output)
6467
{
65-
$bundles = $this->getContainer()->get('kernel')->getBundles();
66-
$containerBuilder = $this->getContainerBuilder();
67-
6868
$name = $input->getArgument('name');
6969

7070
if (empty($name)) {
71-
$output->writeln('Available registered bundles with their extension alias if available:');
72-
73-
$table = $this->getHelperSet()->get('table');
74-
$table->setHeaders(array('Bundle name', 'Extension alias'));
75-
foreach ($bundles as $bundle) {
76-
$extension = $bundle->getContainerExtension();
77-
$table->addRow(array($bundle->getName(), $extension ? $extension->getAlias() : ''));
78-
}
79-
$table->render($output);
71+
$this->listBundles($output);
8072

8173
return;
8274
}
8375

84-
$extension = null;
85-
86-
if (preg_match('/Bundle$/', $name)) {
87-
// input is bundle name
76+
$extension = $this->findExtention($name);
8877

89-
if (isset($bundles[$name])) {
90-
$extension = $bundles[$name]->getContainerExtension();
91-
}
78+
$configuration = $extension->getConfiguration(array(), $this->getContainerBuilder());
9279

93-
if (!$extension) {
94-
throw new \LogicException(sprintf('No extensions with configuration available for "%s"', $name));
95-
}
80+
$this->validateConfiguration($extension, $configuration);
9681

97-
$message = 'Default configuration for "'.$name.'"';
82+
if ($name === $extension->getAlias()) {
83+
$message = sprintf('Default configuration for extension with alias: "%s"', $name);
9884
} else {
99-
foreach ($bundles as $bundle) {
100-
$extension = $bundle->getContainerExtension();
101-
102-
if ($extension && $extension->getAlias() === $name) {
103-
break;
104-
}
105-
106-
$extension = null;
107-
}
108-
109-
if (!$extension) {
110-
throw new \LogicException(sprintf('No extension with alias "%s" is enabled', $name));
111-
}
112-
113-
$message = 'Default configuration for extension with alias: "'.$name.'"';
114-
}
115-
116-
$configuration = $extension->getConfiguration(array(), $containerBuilder);
117-
118-
if (!$configuration) {
119-
throw new \LogicException(sprintf('The extension with alias "%s" does not have it\'s getConfiguration() method setup', $extension->getAlias()));
120-
}
121-
122-
if (!$configuration instanceof ConfigurationInterface) {
123-
throw new \LogicException(sprintf('Configuration class "%s" should implement ConfigurationInterface in order to be dumpable', get_class($configuration)));
85+
$message = sprintf('Default configuration for "%s"', $name);
12486
}
12587

12688
switch ($input->getOption('format')) {

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

-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ protected function configure()
8686

8787
/**
8888
* {@inheritdoc}
89-
*
90-
* @throws \LogicException
9189
*/
9290
protected function execute(InputInterface $input, OutputInterface $output)
9391
{

0 commit comments

Comments
 (0)