2011-06-03 19:38:47 +01: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 ;
2013-09-09 15:57:50 +01:00
use Symfony\Component\Config\Definition\Dumper\YamlReferenceDumper ;
use Symfony\Component\Config\Definition\Dumper\XmlReferenceDumper ;
2011-06-03 19:38:47 +01:00
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Output\OutputInterface ;
2012-06-26 09:46:00 +01:00
use Symfony\Component\Config\Definition\ConfigurationInterface ;
2011-06-03 19:38:47 +01:00
/**
* A console command for dumping available configuration reference
*
* @ author Kevin Bond < kevinbond @ gmail . com >
2013-09-09 15:57:50 +01:00
* @ author Wouter J < waldio . webdesign @ gmail . com >
2011-06-03 19:38:47 +01:00
*/
2012-02-02 16:47:41 +00:00
class ConfigDumpReferenceCommand extends ContainerDebugCommand
2011-06-03 19:38:47 +01:00
{
/**
2013-03-07 13:36:36 +00:00
* { @ inheritdoc }
2011-06-03 19:38:47 +01:00
*/
protected function configure ()
{
$this
2012-02-12 15:37:55 +00:00
-> setName ( 'config:dump-reference' )
2011-06-03 19:38:47 +01:00
-> setDefinition ( array (
2013-09-09 15:57:50 +01:00
new InputArgument ( 'name' , InputArgument :: OPTIONAL , 'The Bundle or extension alias' ),
new InputOption ( 'format' , null , InputOption :: VALUE_REQUIRED , 'The format, either yaml or xml' , 'yaml' ),
2011-06-03 19:38:47 +01:00
))
2012-02-12 15:37:55 +00:00
-> setDescription ( 'Dumps default configuration for an extension' )
2011-06-03 19:38:47 +01:00
-> setHelp ( <<< EOF
2012-02-12 15:37:55 +00:00
The < info >% command . name %</ info > command dumps the default configuration for an extension / bundle .
2011-06-03 19:38:47 +01:00
The extension alias or bundle name can be used :
Example :
2012-02-12 15:37:55 +00:00
< info > php % command . full_name % framework </ info >
2011-06-03 19:38:47 +01:00
or
2012-02-12 15:37:55 +00:00
< info > php % command . full_name % FrameworkBundle </ info >
2013-09-09 15:57:50 +01:00
With the < info > format </ info > option specifies the format of the configuration , this is either yaml
or xml . When the option is not provided , yaml is used .
2011-06-03 19:38:47 +01:00
EOF
)
;
}
/**
2013-03-07 13:36:36 +00:00
* { @ inheritdoc }
*
* @ throws \LogicException
2011-06-03 19:38:47 +01:00
*/
protected function execute ( InputInterface $input , OutputInterface $output )
{
2012-07-31 14:04:27 +01:00
$bundles = $this -> getContainer () -> get ( 'kernel' ) -> getBundles ();
2011-06-03 19:38:47 +01:00
$containerBuilder = $this -> getContainerBuilder ();
$name = $input -> getArgument ( 'name' );
2013-04-06 18:41:49 +01:00
if ( empty ( $name )) {
$output -> writeln ( 'Available registered bundles with their extension alias if available:' );
foreach ( $bundles as $bundle ) {
$extension = $bundle -> getContainerExtension ();
$output -> writeln ( $bundle -> getName () . ( $extension ? ': ' . $extension -> getAlias () : '' ));
}
return ;
}
2011-06-03 19:38:47 +01:00
$extension = null ;
if ( preg_match ( '/Bundle$/' , $name )) {
// input is bundle name
2012-07-31 14:04:27 +01:00
if ( isset ( $bundles [ $name ])) {
$extension = $bundles [ $name ] -> getContainerExtension ();
}
2011-06-03 19:38:47 +01:00
if ( ! $extension ) {
2013-04-10 11:24:37 +01:00
throw new \LogicException ( sprintf ( 'No extensions with configuration available for "%s"' , $name ));
2011-06-03 19:38:47 +01:00
}
$message = 'Default configuration for "' . $name . '"' ;
} else {
2012-07-31 14:04:27 +01:00
foreach ( $bundles as $bundle ) {
2011-06-03 19:38:47 +01:00
$extension = $bundle -> getContainerExtension ();
2012-01-24 20:51:23 +00:00
if ( $extension && $extension -> getAlias () === $name ) {
2011-06-03 19:38:47 +01:00
break ;
}
$extension = null ;
}
if ( ! $extension ) {
2013-04-10 11:24:37 +01:00
throw new \LogicException ( sprintf ( 'No extension with alias "%s" is enabled' , $name ));
2011-06-03 19:38:47 +01:00
}
$message = 'Default configuration for extension with alias: "' . $name . '"' ;
}
$configuration = $extension -> getConfiguration ( array (), $containerBuilder );
if ( ! $configuration ) {
2013-04-10 11:24:37 +01:00
throw new \LogicException ( sprintf ( 'The extension with alias "%s" does not have it\'s getConfiguration() method setup' , $extension -> getAlias ()));
2011-06-03 19:38:47 +01:00
}
2012-06-26 09:46:00 +01:00
if ( ! $configuration instanceof ConfigurationInterface ) {
2013-04-10 11:24:37 +01:00
throw new \LogicException ( sprintf ( 'Configuration class "%s" should implement ConfigurationInterface in order to be dumpable' , get_class ( $configuration )));
2012-06-26 09:46:00 +01:00
}
2011-06-03 19:38:47 +01:00
$output -> writeln ( $message );
2013-09-09 15:57:50 +01:00
switch ( $input -> getOption ( 'format' )) {
case 'yaml' :
$dumper = new YamlReferenceDumper ();
break ;
case 'xml' :
$dumper = new XmlReferenceDumper ();
break ;
default :
throw new \InvalidArgumentException ( 'Only the yaml and xml formats are supported.' );
}
$output -> writeln ( $dumper -> dump ( $configuration ), $extension -> getNamespace ());
2011-06-03 19:38:47 +01:00
}
}