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 ;
2013-09-18 13:22:34 +01:00
use Symfony\Component\Console\Input\InputOption ;
2011-06-03 19:38:47 +01:00
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Output\OutputInterface ;
2015-05-08 18:52:59 +01:00
use Symfony\Component\Console\Style\SymfonyStyle ;
2011-06-03 19:38:47 +01:00
/**
2014-03-13 04:05:55 +00:00
* A console command for dumping available configuration reference .
2011-06-03 19:38:47 +01:00
*
* @ author Kevin Bond < kevinbond @ gmail . com >
2013-09-09 15:57:50 +01:00
* @ author Wouter J < waldio . webdesign @ gmail . com >
2014-01-30 19:09:44 +00:00
* @ author Grégoire Pineau < lyrixx @ lyrixx . info >
2011-06-03 19:38:47 +01:00
*/
2014-01-30 19:09:44 +00:00
class ConfigDumpReferenceCommand extends AbstractConfigCommand
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-18 13:22:34 +01:00
new InputArgument ( 'name' , InputArgument :: OPTIONAL , 'The Bundle name or the extension alias' ),
2016-11-29 16:47:25 +00:00
new InputArgument ( 'path' , InputArgument :: OPTIONAL , 'The configuration option path' ),
2015-05-16 14:21:12 +01:00
new InputOption ( 'format' , null , InputOption :: VALUE_REQUIRED , 'The output format (yaml or xml)' , 'yaml' ),
2011-06-03 19:38:47 +01:00
))
2013-09-18 13:22:34 +01:00
-> setDescription ( 'Dumps the default configuration for an extension' )
2015-12-21 11:01:57 +00:00
-> setHelp ( <<< 'EOF'
2013-09-18 13:22:34 +01:00
The < info >% command . name %</ info > command dumps the default configuration for an
extension / bundle .
2011-06-03 19:38:47 +01:00
2014-01-30 19:09:44 +00:00
Either the extension alias or bundle name can be used :
2011-06-03 19:38:47 +01:00
2012-02-12 15:37:55 +00:00
< info > php % command . full_name % framework </ info >
< info > php % command . full_name % FrameworkBundle </ info >
2013-09-09 15:57:50 +01:00
2015-01-04 09:52:37 +00:00
With the < info >-- format </ info > option specifies the format of the configuration ,
2014-01-30 19:09:44 +00:00
this is either < comment > yaml </ comment > or < comment > xml </ comment >.
When the option is not provided , < comment > yaml </ comment > is used .
< info > php % command . full_name % FrameworkBundle -- format = xml </ info >
2016-11-29 16:47:25 +00:00
For dumping a specific option , add its path as second argument ( only available for the yaml format ) :
< info > php % command . full_name % framework profiler . matcher </ info >
2014-03-13 04:05:55 +00:00
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 )
{
2015-12-07 12:11:35 +00:00
$io = new SymfonyStyle ( $input , $output );
2011-06-03 19:38:47 +01:00
2016-04-10 17:33:46 +01:00
if ( null === $name = $input -> getArgument ( 'name' )) {
$this -> listBundles ( $io );
2016-11-29 16:47:25 +00:00
$io -> comment ( array (
'Provide the name of a bundle as the first argument of this command to dump its default configuration. (e.g. <comment>config:dump-reference FrameworkBundle</comment>)' ,
'For dumping a specific option, add its path as the second argument of this command. (e.g. <comment>config:dump-reference FrameworkBundle profiler.matcher</comment> to dump the <comment>framework.profiler.matcher</comment> configuration)' ,
));
2013-04-06 18:41:49 +01:00
return ;
}
2014-02-23 16:22:13 +00:00
$extension = $this -> findExtension ( $name );
2012-07-31 14:04:27 +01:00
2014-01-30 19:09:44 +00:00
$configuration = $extension -> getConfiguration ( array (), $this -> getContainerBuilder ());
2011-06-03 19:38:47 +01:00
2014-01-30 19:09:44 +00:00
$this -> validateConfiguration ( $extension , $configuration );
2011-06-03 19:38:47 +01:00
2016-11-29 16:47:25 +00:00
$format = $input -> getOption ( 'format' );
$path = $input -> getArgument ( 'path' );
if ( $path !== null && 'yaml' !== $format ) {
$io -> error ( 'The "path" option is only available for the "yaml" format.' );
return 1 ;
}
2014-01-30 19:09:44 +00:00
if ( $name === $extension -> getAlias ()) {
$message = sprintf ( 'Default configuration for extension with alias: "%s"' , $name );
2011-06-03 19:38:47 +01:00
} else {
2014-01-30 19:09:44 +00:00
$message = sprintf ( 'Default configuration for "%s"' , $name );
2012-06-26 09:46:00 +01:00
}
2016-11-29 16:47:25 +00:00
if ( $path !== null ) {
$message .= sprintf ( ' at path "%s"' , $path );
}
switch ( $format ) {
2013-09-09 15:57:50 +01:00
case 'yaml' :
2015-12-07 12:11:35 +00:00
$io -> writeln ( sprintf ( '# %s' , $message ));
2013-09-09 15:57:50 +01:00
$dumper = new YamlReferenceDumper ();
break ;
case 'xml' :
2015-12-07 12:11:35 +00:00
$io -> writeln ( sprintf ( '<!-- %s -->' , $message ));
2013-09-09 15:57:50 +01:00
$dumper = new XmlReferenceDumper ();
break ;
default :
2015-12-07 12:11:35 +00:00
$io -> writeln ( $message );
2013-09-09 15:57:50 +01:00
throw new \InvalidArgumentException ( 'Only the yaml and xml formats are supported.' );
}
2016-11-29 16:47:25 +00:00
$io -> writeln ( null === $path ? $dumper -> dump ( $configuration , $extension -> getNamespace ()) : $dumper -> dumpAtPath ( $configuration , $path ));
2011-06-03 19:38:47 +01:00
}
}