/
AbstractConfigCommand.php
108 lines (88 loc) · 3.72 KB
/
AbstractConfigCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Command;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
/**
* A console command for dumping available configuration reference.
*
* @author Kevin Bond <kevinbond@gmail.com>
* @author Wouter J <waldio.webdesign@gmail.com>
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
abstract class AbstractConfigCommand extends ContainerDebugCommand
{
protected function listBundles(OutputInterface $output)
{
$output->writeln('Available registered bundles with their extension alias if available:');
$table = $this->getHelperSet()->get('table');
$table->setHeaders(array('Bundle name', 'Extension alias'));
foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) {
$extension = $bundle->getContainerExtension();
$table->addRow(array($bundle->getName(), $extension ? $extension->getAlias() : ''));
}
$table->render($output);
}
protected function findExtension($name)
{
$extension = null;
$bundles = $this->getContainer()->get('kernel')->getBundles();
if (preg_match('/Bundle$/', $name)) {
// input is bundle name
if (isset($bundles[$name])) {
$extension = $bundles[$name]->getContainerExtension();
}
if (!$extension) {
throw new \LogicException(sprintf('No extensions with configuration available for "%s"', $name));
}
} else {
foreach ($bundles as $bundle) {
$extension = $bundle->getContainerExtension();
if ($extension && $name === $extension->getAlias()) {
break;
}
$extension = null;
}
if (!$extension) {
throw new \LogicException(sprintf('No extension with alias "%s" is enabled', $name));
}
}
$this->initializeExtensions($bundles);
return $extension;
}
public function validateConfiguration(ExtensionInterface $extension, $configuration)
{
if (!$configuration) {
throw new \LogicException(sprintf('The extension with alias "%s" does not have its getConfiguration() method setup', $extension->getAlias()));
}
if (!$configuration instanceof ConfigurationInterface) {
throw new \LogicException(sprintf('Configuration class "%s" should implement ConfigurationInterface in order to be dumpable', get_class($configuration)));
}
}
private function initializeExtensions($bundles)
{
// Re-build bundle manually to initialize DI extensions that can be extended by other bundles in their build() method
// as this method is not called when the container is loaded from the cache.
$parameters = $this->getContainer()->getParameterBag()->all();
$container = new ContainerBuilder(new ParameterBag($parameters));
foreach ($bundles as $bundle) {
if ($extension = $bundle->getContainerExtension()) {
$container->registerExtension($extension);
}
}
foreach ($bundles as $bundle) {
$bundle = clone $bundle;
$bundle->build($container);
}
}
}