AbstractConfigCommand.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Command;
  11. use Symfony\Component\Config\Definition\ConfigurationInterface;
  12. use Symfony\Component\Console\Exception\LogicException;
  13. use Symfony\Component\Console\Helper\Table;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Style\StyleInterface;
  16. use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
  17. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  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. abstract class AbstractConfigCommand extends ContainerDebugCommand
  26. {
  27. /**
  28. * @param OutputInterface|StyleInterface $output
  29. */
  30. protected function listBundles($output)
  31. {
  32. $title = 'Available registered bundles with their extension alias if available';
  33. $headers = ['Bundle name', 'Extension alias'];
  34. $rows = [];
  35. $bundles = $this->getApplication()->getKernel()->getBundles();
  36. usort($bundles, function ($bundleA, $bundleB) {
  37. return strcmp($bundleA->getName(), $bundleB->getName());
  38. });
  39. foreach ($bundles as $bundle) {
  40. $extension = $bundle->getContainerExtension();
  41. $rows[] = [$bundle->getName(), $extension ? $extension->getAlias() : ''];
  42. }
  43. if ($output instanceof StyleInterface) {
  44. $output->title($title);
  45. $output->table($headers, $rows);
  46. } else {
  47. $output->writeln($title);
  48. $table = new Table($output);
  49. $table->setHeaders($headers)->setRows($rows)->render();
  50. }
  51. }
  52. /**
  53. * @return ExtensionInterface
  54. */
  55. protected function findExtension(string $name)
  56. {
  57. $bundles = $this->initializeBundles();
  58. $minScore = \INF;
  59. $kernel = $this->getApplication()->getKernel();
  60. if ($kernel instanceof ExtensionInterface && ($kernel instanceof ConfigurationInterface || $kernel instanceof ConfigurationExtensionInterface)) {
  61. if ($name === $kernel->getAlias()) {
  62. return $kernel;
  63. }
  64. if ($kernel->getAlias()) {
  65. $distance = levenshtein($name, $kernel->getAlias());
  66. if ($distance < $minScore) {
  67. $guess = $kernel->getAlias();
  68. $minScore = $distance;
  69. }
  70. }
  71. }
  72. foreach ($bundles as $bundle) {
  73. if ($name === $bundle->getName()) {
  74. if (!$bundle->getContainerExtension()) {
  75. throw new \LogicException(sprintf('Bundle "%s" does not have a container extension.', $name));
  76. }
  77. return $bundle->getContainerExtension();
  78. }
  79. $distance = levenshtein($name, $bundle->getName());
  80. if ($distance < $minScore) {
  81. $guess = $bundle->getName();
  82. $minScore = $distance;
  83. }
  84. $extension = $bundle->getContainerExtension();
  85. if ($extension) {
  86. if ($name === $extension->getAlias()) {
  87. return $extension;
  88. }
  89. $distance = levenshtein($name, $extension->getAlias());
  90. if ($distance < $minScore) {
  91. $guess = $extension->getAlias();
  92. $minScore = $distance;
  93. }
  94. }
  95. }
  96. if ('Bundle' !== substr($name, -6)) {
  97. $message = sprintf('No extensions with configuration available for "%s".', $name);
  98. } else {
  99. $message = sprintf('No extension with alias "%s" is enabled.', $name);
  100. }
  101. if (isset($guess) && $minScore < 3) {
  102. $message .= sprintf("\n\nDid you mean \"%s\"?", $guess);
  103. }
  104. throw new LogicException($message);
  105. }
  106. public function validateConfiguration(ExtensionInterface $extension, $configuration)
  107. {
  108. if (!$configuration) {
  109. throw new \LogicException(sprintf('The extension with alias "%s" does not have its getConfiguration() method setup.', $extension->getAlias()));
  110. }
  111. if (!$configuration instanceof ConfigurationInterface) {
  112. throw new \LogicException(sprintf('Configuration class "%s" should implement ConfigurationInterface in order to be dumpable.', get_debug_type($configuration)));
  113. }
  114. }
  115. private function initializeBundles()
  116. {
  117. // Re-build bundle manually to initialize DI extensions that can be extended by other bundles in their build() method
  118. // as this method is not called when the container is loaded from the cache.
  119. $container = $this->getContainerBuilder();
  120. $bundles = $this->getApplication()->getKernel()->getBundles();
  121. foreach ($bundles as $bundle) {
  122. if ($extension = $bundle->getContainerExtension()) {
  123. $container->registerExtension($extension);
  124. }
  125. }
  126. foreach ($bundles as $bundle) {
  127. $bundle->build($container);
  128. }
  129. return $bundles;
  130. }
  131. }