-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathAbstractConfigCommand.php
195 lines (161 loc) · 6.5 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Exception\LogicException;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
/**
* A console command for dumping available configuration reference.
*
* @author Kevin Bond <[email protected]>
* @author Wouter J <[email protected]>
* @author Grégoire Pineau <[email protected]>
*/
abstract class AbstractConfigCommand extends ContainerDebugCommand
{
/**
* @return void
*/
protected function listBundles(OutputInterface|StyleInterface $output)
{
$title = 'Available registered bundles with their extension alias if available';
$headers = ['Bundle name', 'Extension alias'];
$rows = [];
$bundles = $this->getApplication()->getKernel()->getBundles();
usort($bundles, fn ($bundleA, $bundleB) => strcmp($bundleA->getName(), $bundleB->getName()));
foreach ($bundles as $bundle) {
$extension = $bundle->getContainerExtension();
$rows[] = [$bundle->getName(), $extension ? $extension->getAlias() : ''];
}
if ($output instanceof StyleInterface) {
$output->title($title);
$output->table($headers, $rows);
} else {
$output->writeln($title);
$table = new Table($output);
$table->setHeaders($headers)->setRows($rows)->render();
}
}
protected function listNonBundleExtensions(OutputInterface|StyleInterface $output): void
{
$title = 'Available registered non-bundle extension aliases';
$headers = ['Extension alias'];
$rows = [];
$kernel = $this->getApplication()->getKernel();
$bundleExtensions = [];
foreach ($kernel->getBundles() as $bundle) {
if ($extension = $bundle->getContainerExtension()) {
$bundleExtensions[$extension::class] = true;
}
}
$extensions = $this->getContainerBuilder($kernel)->getExtensions();
foreach ($extensions as $alias => $extension) {
if (isset($bundleExtensions[$extension::class])) {
continue;
}
$rows[] = [$alias];
}
if (!$rows) {
return;
}
if ($output instanceof StyleInterface) {
$output->title($title);
$output->table($headers, $rows);
} else {
$output->writeln($title);
$table = new Table($output);
$table->setHeaders($headers)->setRows($rows)->render();
}
}
protected function findExtension(string $name): ExtensionInterface
{
$bundles = $this->initializeBundles();
$minScore = \INF;
$kernel = $this->getApplication()->getKernel();
if ($kernel instanceof ExtensionInterface && ($kernel instanceof ConfigurationInterface || $kernel instanceof ConfigurationExtensionInterface)) {
if ($name === $kernel->getAlias()) {
return $kernel;
}
if ($kernel->getAlias()) {
$distance = levenshtein($name, $kernel->getAlias());
if ($distance < $minScore) {
$guess = $kernel->getAlias();
$minScore = $distance;
}
}
}
foreach ($bundles as $bundle) {
if ($name === $bundle->getName()) {
if (!$bundle->getContainerExtension()) {
throw new \LogicException(sprintf('Bundle "%s" does not have a container extension.', $name));
}
return $bundle->getContainerExtension();
}
$distance = levenshtein($name, $bundle->getName());
if ($distance < $minScore) {
$guess = $bundle->getName();
$minScore = $distance;
}
}
$container = $this->getContainerBuilder($kernel);
if ($container->hasExtension($name)) {
return $container->getExtension($name);
}
foreach ($container->getExtensions() as $extension) {
$distance = levenshtein($name, $extension->getAlias());
if ($distance < $minScore) {
$guess = $extension->getAlias();
$minScore = $distance;
}
}
if (!str_ends_with($name, 'Bundle')) {
$message = sprintf('No extensions with configuration available for "%s".', $name);
} else {
$message = sprintf('No extension with alias "%s" is enabled.', $name);
}
if (isset($guess) && $minScore < 3) {
$message .= sprintf("\n\nDid you mean \"%s\"?", $guess);
}
throw new LogicException($message);
}
/**
* @return void
*/
public function validateConfiguration(ExtensionInterface $extension, mixed $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_debug_type($configuration)));
}
}
private function initializeBundles(): array
{
// 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.
$kernel = $this->getApplication()->getKernel();
$container = $this->getContainerBuilder($kernel);
$bundles = $kernel->getBundles();
foreach ($bundles as $bundle) {
if ($extension = $bundle->getContainerExtension()) {
$container->registerExtension($extension);
}
}
foreach ($bundles as $bundle) {
$bundle->build($container);
}
return $bundles;
}
}