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
{
2016-04-10 17:33:46 +01:00
$title = 'Available registered bundles with their extension alias if available' ;
2015-05-08 18:52:59 +01:00
$headers = array ( 'Bundle name' , 'Extension alias' );
$rows = array ();
2016-01-22 13:33:13 +00:00
$bundles = $this -> getContainer () -> get ( 'kernel' ) -> getBundles ();
2016-06-20 17:47:20 +01:00
usort ( $bundles , function ( $bundleA , $bundleB ) {
2016-01-22 13:33:13 +00:00
return strcmp ( $bundleA -> getName (), $bundleB -> getName ());
});
foreach ( $bundles as $bundle ) {
2014-01-30 19:09:44 +00:00
$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 ) {
2016-04-10 17:33:46 +01:00
$output -> title ( $title );
2015-05-08 18:52:59 +01:00
$output -> table ( $headers , $rows );
2015-04-13 16:21:02 +01:00
} else {
2016-04-10 17:33:46 +01:00
$output -> writeln ( $title );
2015-05-08 18:52:59 +01:00
$table = new Table ( $output );
2016-07-23 16:07:08 +01:00
$table -> setHeaders ( $headers ) -> setRows ( $rows ) -> render ();
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 ();
2016-01-21 22:51:50 +00:00
$minScore = INF ;
2015-03-10 10:04:39 +00:00
foreach ( $bundles as $bundle ) {
2016-01-20 12:10:14 +00:00
if ( $name === $bundle -> getName ()) {
2016-01-21 23:44:07 +00:00
if ( ! $bundle -> getContainerExtension ()) {
throw new \LogicException ( sprintf ( 'Bundle "%s" does not have a container extension.' , $name ));
}
2016-01-20 12:10:14 +00:00
return $bundle -> getContainerExtension ();
}
2014-01-30 19:09:44 +00:00
2016-01-21 22:51:50 +00:00
$distance = levenshtein ( $name , $bundle -> getName ());
if ( $distance < $minScore ) {
$guess = $bundle -> getName ();
$minScore = $distance ;
}
2016-01-20 12:10:14 +00:00
$extension = $bundle -> getContainerExtension ();
2016-01-21 22:51:50 +00:00
if ( $extension ) {
if ( $name === $extension -> getAlias ()) {
return $extension ;
}
$distance = levenshtein ( $name , $extension -> getAlias ());
if ( $distance < $minScore ) {
$guess = $extension -> getAlias ();
$minScore = $distance ;
}
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 )) {
2016-01-21 22:51:50 +00:00
$message = sprintf ( 'No extensions with configuration available for "%s".' , $name );
2016-01-20 12:10:14 +00:00
} else {
2016-01-21 22:51:50 +00:00
$message = sprintf ( 'No extension with alias "%s" is enabled.' , $name );
}
if ( isset ( $guess ) && $minScore < 3 ) {
$message .= sprintf ( " \n \n Did you mean \" %s \" ? " , $guess );
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
}