AbstractConfigCommand.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Helper\Table;
  13. use Symfony\Component\Console\Style\StyleInterface;
  14. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  15. /**
  16. * A console command for dumping available configuration reference.
  17. *
  18. * @author Kevin Bond <kevinbond@gmail.com>
  19. * @author Wouter J <waldio.webdesign@gmail.com>
  20. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  21. */
  22. abstract class AbstractConfigCommand extends ContainerDebugCommand
  23. {
  24. protected function listBundles($output)
  25. {
  26. $headers = array('Bundle name', 'Extension alias');
  27. $rows = array();
  28. $bundles = $this->getContainer()->get('kernel')->getBundles();
  29. usort($bundles, function ($bundleA, $bundleB) {
  30. return strcmp($bundleA->getName(), $bundleB->getName());
  31. });
  32. foreach ($bundles as $bundle) {
  33. $extension = $bundle->getContainerExtension();
  34. $rows[] = array($bundle->getName(), $extension ? $extension->getAlias() : '');
  35. }
  36. if ($output instanceof StyleInterface) {
  37. $output->table($headers, $rows);
  38. } else {
  39. $output->writeln('Available registered bundles with their extension alias if available:');
  40. $table = new Table($output);
  41. $table->setHeaders($headers)->setRows($rows)->render();
  42. }
  43. }
  44. protected function findExtension($name)
  45. {
  46. $bundles = $this->initializeBundles();
  47. foreach ($bundles as $bundle) {
  48. if ($name === $bundle->getName()) {
  49. if (!$bundle->getContainerExtension()) {
  50. throw new \LogicException(sprintf('Bundle "%s" does not have a container extension.', $name));
  51. }
  52. return $bundle->getContainerExtension();
  53. }
  54. $extension = $bundle->getContainerExtension();
  55. if ($extension && $name === $extension->getAlias()) {
  56. return $extension;
  57. }
  58. }
  59. if ('Bundle' !== substr($name, -6)) {
  60. $message = sprintf('No extensions with configuration available for "%s"', $name);
  61. } else {
  62. $message = sprintf('No extension with alias "%s" is enabled', $name);
  63. }
  64. throw new \LogicException($message);
  65. }
  66. public function validateConfiguration(ExtensionInterface $extension, $configuration)
  67. {
  68. if (!$configuration) {
  69. throw new \LogicException(sprintf('The extension with alias "%s" does not have its getConfiguration() method setup', $extension->getAlias()));
  70. }
  71. if (!$configuration instanceof ConfigurationInterface) {
  72. throw new \LogicException(sprintf('Configuration class "%s" should implement ConfigurationInterface in order to be dumpable', \get_class($configuration)));
  73. }
  74. }
  75. private function initializeBundles()
  76. {
  77. // Re-build bundle manually to initialize DI extensions that can be extended by other bundles in their build() method
  78. // as this method is not called when the container is loaded from the cache.
  79. $container = $this->getContainerBuilder();
  80. $bundles = $this->getContainer()->get('kernel')->getBundles();
  81. foreach ($bundles as $bundle) {
  82. if ($extension = $bundle->getContainerExtension()) {
  83. $container->registerExtension($extension);
  84. }
  85. }
  86. foreach ($bundles as $bundle) {
  87. $bundle->build($container);
  88. }
  89. return $bundles;
  90. }
  91. }