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 ;
2015-05-08 18:52:59 +01:00
use Symfony\Component\Console\Style\StyleInterface ;
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
{
2015-09-28 19:06:02 +01:00
protected function listBundles ( $output )
2014-01-30 19:09:44 +00:00
{
2015-05-08 18:52:59 +01:00
$headers = array ( 'Bundle name' , 'Extension alias' );
$rows = array ();
2014-01-30 19:09:44 +00:00
foreach ( $this -> getContainer () -> get ( 'kernel' ) -> getBundles () as $bundle ) {
$extension = $bundle -> getContainerExtension ();
2015-05-08 18:52:59 +01:00
$rows [] = array ( $bundle -> getName (), $extension ? $extension -> getAlias () : '' );
2014-01-30 19:09:44 +00:00
}
2015-05-08 18:52:59 +01:00
if ( $output instanceof StyleInterface ) {
$output -> table ( $headers , $rows );
2015-04-13 16:21:02 +01:00
} else {
2015-09-28 19:06:02 +01:00
$output -> writeln ( 'Available registered bundles with their extension alias if available:' );
2015-05-08 18:52:59 +01:00
$table = new Table ( $output );
$table -> setHeaders ( $headers ) -> setRows ( $rows ) -> render ( $output );
2015-04-13 16:21:02 +01:00
}
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
{
2015-03-10 10:04:39 +00:00
$bundles = $this -> initializeBundles ();
foreach ( $bundles as $bundle ) {
2016-01-20 12:10:14 +00:00
if ( $name === $bundle -> getName ()) {
return $bundle -> getContainerExtension ();
}
2014-01-30 19:09:44 +00:00
2016-01-20 12:10:14 +00:00
$extension = $bundle -> getContainerExtension ();
if ( $extension && $name === $extension -> getAlias ()) {
return $extension ;
2014-01-30 19:09:44 +00:00
}
2015-03-10 10:04:39 +00:00
}
2014-01-30 19:09:44 +00:00
2016-01-20 12:10:14 +00:00
if ( 'Bundle' !== substr ( $name , - 6 )) {
$message = sprintf ( 'No extensions with configuration available for "%s"' , $name );
} else {
2015-03-10 10:04:39 +00:00
$message = sprintf ( 'No extension with alias "%s" is enabled' , $name );
2014-01-30 19:09:44 +00:00
}
2016-01-20 12:10:14 +00:00
throw new \LogicException ( $message );
2014-01-30 19:09:44 +00:00
}
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
}