2014-01-30 19:09:44 +00:00
< ? 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 ;
2015-04-13 16:21:02 +01:00
use Symfony\Component\Console\Helper\Table ;
2014-01-30 19:09:44 +00:00
use Symfony\Component\Console\Output\OutputInterface ;
2014-12-23 11:45:12 +00:00
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface ;
2014-01-30 19:09:44 +00:00
/**
2014-03-13 04:05:55 +00:00
* A console command for dumping available configuration reference .
2014-01-30 19:09:44 +00:00
*
* @ author Kevin Bond < kevinbond @ gmail . com >
* @ author Wouter J < waldio . webdesign @ gmail . com >
* @ author Grégoire Pineau < lyrixx @ lyrixx . info >
*/
2014-02-24 16:03:44 +00:00
abstract class AbstractConfigCommand extends ContainerDebugCommand
2014-01-30 19:09:44 +00:00
{
protected function listBundles ( OutputInterface $output )
{
$output -> writeln ( 'Available registered bundles with their extension alias if available:' );
2015-04-13 16:21:02 +01:00
if ( class_exists ( 'Symfony\Component\Console\Helper\Table' )) {
$table = new Table ( $output );
} else {
$table = $this -> getHelperSet () -> get ( 'table' );
}
2014-01-30 19:09:44 +00:00
$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 () : '' ));
}
2015-04-13 16:21:02 +01:00
if ( class_exists ( 'Symfony\Component\Console\Helper\Table' )) {
$table -> render ();
} else {
$table -> render ( $output );
}
2014-01-30 19:09:44 +00:00
}
2014-02-23 16:22:13 +00:00
protected function findExtension ( $name )
2014-01-30 19:09:44 +00:00
{
$extension = null ;
2015-03-10 10:04:39 +00:00
$bundles = $this -> initializeBundles ();
foreach ( $bundles as $bundle ) {
$extension = $bundle -> getContainerExtension ();
2014-01-30 19:09:44 +00:00
2015-03-10 10:04:39 +00:00
if ( $extension && ( $name === $extension -> getAlias () || $name === $bundle -> getName ())) {
break ;
2014-01-30 19:09:44 +00:00
}
2015-03-10 10:04:39 +00:00
}
2014-01-30 19:09:44 +00:00
2015-03-10 10:04:39 +00:00
if ( ! $extension ) {
$message = sprintf ( 'No extension with alias "%s" is enabled' , $name );
if ( preg_match ( '/Bundle$/' , $name )) {
$message = sprintf ( 'No extensions with configuration available for "%s"' , $name );
2014-01-30 19:09:44 +00:00
}
2015-03-10 10:04:39 +00:00
throw new \LogicException ( $message );
2014-01-30 19:09:44 +00:00
}
return $extension ;
}
2014-12-23 11:45:12 +00:00
public function validateConfiguration ( ExtensionInterface $extension , $configuration )
2014-01-30 19:09:44 +00:00
{
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 )));
}
}
2015-01-02 15:05:58 +00:00
2015-03-10 10:04:39 +00:00
private function initializeBundles ()
2015-01-02 15:05:58 +00:00
{
// 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.
2015-03-10 10:04:39 +00:00
$container = $this -> getContainerBuilder ();
$bundles = $this -> getContainer () -> get ( 'kernel' ) -> registerBundles ();
2015-01-02 15:05:58 +00:00
foreach ( $bundles as $bundle ) {
if ( $extension = $bundle -> getContainerExtension ()) {
$container -> registerExtension ( $extension );
}
}
foreach ( $bundles as $bundle ) {
$bundle -> build ( $container );
}
2015-03-10 10:04:39 +00:00
return $bundles ;
2015-01-02 15:05:58 +00:00
}
2014-01-30 19:09:44 +00:00
}