2011-03-10 15:09:40 +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 ;
2013-04-30 16:08:17 +01:00
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper ;
2011-03-10 15:09:40 +00:00
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputOption ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Output\OutputInterface ;
2011-05-01 16:18:30 +01:00
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader ;
use Symfony\Component\DependencyInjection\ContainerBuilder ;
use Symfony\Component\Config\FileLocator ;
2014-07-06 17:38:58 +01:00
use Symfony\Component\Console\Question\ChoiceQuestion ;
2011-03-10 15:09:40 +00:00
/**
2014-03-13 04:05:55 +00:00
* A console command for retrieving information about services .
2011-03-10 15:09:40 +00:00
*
* @ author Ryan Weaver < ryan @ thatsquality . com >
*/
2011-09-15 15:46:01 +01:00
class ContainerDebugCommand extends ContainerAwareCommand
2011-03-10 15:09:40 +00:00
{
/**
2013-03-07 13:36:36 +00:00
* @ var ContainerBuilder | null
2011-03-10 15:09:40 +00:00
*/
2012-06-26 08:01:12 +01:00
protected $containerBuilder ;
2011-03-10 15:09:40 +00:00
/**
2013-03-07 13:36:36 +00:00
* { @ inheritdoc }
2011-03-10 15:09:40 +00:00
*/
protected function configure ()
{
$this
2014-08-09 16:20:46 +01:00
-> setName ( 'debug:container' )
-> setAliases ( array (
'container:debug' ,
))
2011-03-10 15:09:40 +00:00
-> setDefinition ( array (
2012-03-07 11:09:07 +00:00
new InputArgument ( 'name' , InputArgument :: OPTIONAL , 'A service name (foo)' ),
2014-03-13 04:05:55 +00:00
new InputOption ( 'show-private' , null , InputOption :: VALUE_NONE , 'Used to show public *and* private services' ),
new InputOption ( 'tag' , null , InputOption :: VALUE_REQUIRED , 'Shows all services with a specific tag' ),
2013-03-30 15:35:41 +00:00
new InputOption ( 'tags' , null , InputOption :: VALUE_NONE , 'Displays tagged services for an application' ),
new InputOption ( 'parameter' , null , InputOption :: VALUE_REQUIRED , 'Displays a specific parameter for an application' ),
2013-04-30 16:08:17 +01:00
new InputOption ( 'parameters' , null , InputOption :: VALUE_NONE , 'Displays parameters for an application' ),
2013-10-02 15:03:04 +01:00
new InputOption ( 'format' , null , InputOption :: VALUE_REQUIRED , 'To output description in other formats' , 'txt' ),
2013-04-30 16:08:17 +01:00
new InputOption ( 'raw' , null , InputOption :: VALUE_NONE , 'To output raw description' ),
2011-03-10 15:09:40 +00:00
))
-> setDescription ( 'Displays current services for an application' )
-> setHelp ( <<< EOF
2012-02-12 15:37:55 +00:00
The < info >% command . name %</ info > command displays all configured < comment > public </ comment > services :
2011-03-10 15:09:40 +00:00
2012-02-12 15:37:55 +00:00
< info > php % command . full_name %</ info >
2011-03-10 15:09:40 +00:00
2011-04-19 23:01:22 +01:00
To get specific information about a service , specify its name :
2011-03-10 15:09:40 +00:00
2012-02-12 15:37:55 +00:00
< info > php % command . full_name % validator </ info >
2011-03-10 15:09:40 +00:00
By default , private services are hidden . You can display all services by
2015-01-04 09:47:47 +00:00
using the < info >-- show - private </ info > flag :
2011-03-10 15:09:40 +00:00
2012-02-12 15:37:55 +00:00
< info > php % command . full_name % -- show - private </ info >
2012-09-16 16:15:14 +01:00
Use the -- tags option to display tagged < comment > public </ comment > services grouped by tag :
< info > php % command . full_name % -- tags </ info >
2015-01-04 09:47:47 +00:00
Find all services with a specific tag by specifying the tag name with the < info >-- tag </ info > option :
2012-09-16 16:15:14 +01:00
< info > php % command . full_name % -- tag = form . type </ info >
2013-03-30 15:35:41 +00:00
2015-01-04 09:47:47 +00:00
Use the < info >-- parameters </ info > option to display all parameters :
2013-03-30 15:35:41 +00:00
< info > php % command . full_name % -- parameters </ info >
2015-01-04 09:47:47 +00:00
Display a specific parameter by specifying his name with the < info >-- parameter </ info > option :
2013-03-30 15:35:41 +00:00
< info > php % command . full_name % -- parameter = kernel . debug </ info >
2014-03-13 04:05:55 +00:00
2011-03-10 15:09:40 +00:00
EOF
)
;
}
/**
2013-03-07 13:36:36 +00:00
* { @ inheritdoc }
2011-03-10 15:09:40 +00:00
*/
protected function execute ( InputInterface $input , OutputInterface $output )
{
2015-01-13 14:10:03 +00:00
if ( false !== strpos ( $input -> getFirstArgument (), ':d' )) {
$output -> writeln ( '<comment>The use of "container:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:container" instead.</comment>' );
}
2013-03-30 15:35:41 +00:00
$this -> validateInput ( $input );
2011-03-10 15:09:40 +00:00
2013-03-30 15:35:41 +00:00
if ( $input -> getOption ( 'parameters' )) {
2013-04-30 16:08:17 +01:00
$object = $this -> getContainerBuilder () -> getParameterBag ();
$options = array ();
} elseif ( $parameter = $input -> getOption ( 'parameter' )) {
$object = $this -> getContainerBuilder ();
$options = array ( 'parameter' => $parameter );
} elseif ( $input -> getOption ( 'tags' )) {
$object = $this -> getContainerBuilder ();
$options = array ( 'group_by' => 'tags' , 'show_private' => $input -> getOption ( 'show-private' ));
} elseif ( $tag = $input -> getOption ( 'tag' )) {
$object = $this -> getContainerBuilder ();
$options = array ( 'tag' => $tag , 'show_private' => $input -> getOption ( 'show-private' ));
} elseif ( $name = $input -> getArgument ( 'name' )) {
$object = $this -> getContainerBuilder ();
2014-07-06 17:38:58 +01:00
$name = $this -> findProperServiceName ( $input , $output , $object , $name );
2013-04-30 16:08:17 +01:00
$options = array ( 'id' => $name );
2012-09-16 16:15:14 +01:00
} else {
2013-04-30 16:08:17 +01:00
$object = $this -> getContainerBuilder ();
$options = array ( 'show_private' => $input -> getOption ( 'show-private' ));
2012-09-16 16:15:14 +01:00
}
2011-04-19 23:01:22 +01:00
2013-04-30 16:08:17 +01:00
$helper = new DescriptorHelper ();
2013-10-02 15:03:04 +01:00
$options [ 'format' ] = $input -> getOption ( 'format' );
$options [ 'raw_text' ] = $input -> getOption ( 'raw' );
$helper -> describe ( $output , $object , $options );
2014-07-06 17:38:58 +01:00
if ( ! $input -> getArgument ( 'name' ) && $input -> isInteractive ()) {
2014-12-10 20:30:26 +00:00
$output -> writeln ( 'To search for a service, re-run this command with a search term. <comment>debug:container log</comment>' );
2014-07-06 17:38:58 +01:00
}
2011-03-10 15:09:40 +00:00
}
2013-04-30 16:08:17 +01:00
/**
* Validates input arguments and options .
*
* @ param InputInterface $input
*
* @ throws \InvalidArgumentException
*/
2013-03-30 15:35:41 +00:00
protected function validateInput ( InputInterface $input )
{
$options = array ( 'tags' , 'tag' , 'parameters' , 'parameter' );
$optionsCount = 0 ;
foreach ( $options as $option ) {
if ( $input -> getOption ( $option )) {
2015-03-31 00:07:44 +01:00
++ $optionsCount ;
2013-03-30 15:35:41 +00:00
}
}
$name = $input -> getArgument ( 'name' );
if (( null !== $name ) && ( $optionsCount > 0 )) {
throw new \InvalidArgumentException ( 'The options tags, tag, parameters & parameter can not be combined with the service name argument.' );
} elseif (( null === $name ) && $optionsCount > 1 ) {
2013-04-21 12:14:35 +01:00
throw new \InvalidArgumentException ( 'The options tags, tag, parameters & parameter can not be combined together.' );
2013-03-30 15:35:41 +00:00
}
}
2011-03-10 15:09:40 +00:00
/**
* Loads the ContainerBuilder from the cache .
*
2011-05-01 16:18:30 +01:00
* @ return ContainerBuilder
2012-12-16 12:02:54 +00:00
*
* @ throws \LogicException
2011-03-10 15:09:40 +00:00
*/
2011-09-15 15:46:01 +01:00
protected function getContainerBuilder ()
2011-03-10 15:09:40 +00:00
{
2015-03-10 10:04:39 +00:00
if ( $this -> containerBuilder ) {
return $this -> containerBuilder ;
}
2011-05-01 16:18:30 +01:00
if ( ! $this -> getApplication () -> getKernel () -> isDebug ()) {
throw new \LogicException ( sprintf ( 'Debug information about the container is only available in debug mode.' ));
}
2011-03-10 15:09:40 +00:00
2011-08-29 14:28:03 +01:00
if ( ! is_file ( $cachedFile = $this -> getContainer () -> getParameter ( 'debug.container.dump' ))) {
2011-03-10 15:09:40 +00:00
throw new \LogicException ( sprintf ( 'Debug information about the container could not be found. Please clear the cache and try again.' ));
}
2011-05-01 16:18:30 +01:00
$container = new ContainerBuilder ();
$loader = new XmlFileLoader ( $container , new FileLocator ());
$loader -> load ( $cachedFile );
2015-03-10 10:04:39 +00:00
return $this -> containerBuilder = $container ;
2011-03-10 15:09:40 +00:00
}
2014-07-06 17:38:58 +01:00
private function findProperServiceName ( InputInterface $input , OutputInterface $output , ContainerBuilder $builder , $name )
{
if ( $builder -> has ( $name ) || ! $input -> isInteractive ()) {
return $name ;
}
2014-10-08 19:45:36 +01:00
$matchingServices = $this -> findServiceIdsContaining ( $builder , $name );
if ( empty ( $matchingServices )) {
throw new \InvalidArgumentException ( sprintf ( 'No services found that match "%s".' , $name ));
}
$question = new ChoiceQuestion ( 'Choose a number for more information on the service' , $matchingServices );
2014-07-06 17:38:58 +01:00
$question -> setErrorMessage ( 'Service %s is invalid.' );
return $this -> getHelper ( 'question' ) -> ask ( $input , $output , $question );
}
private function findServiceIdsContaining ( ContainerBuilder $builder , $name )
{
$serviceIds = $builder -> getServiceIds ();
$foundServiceIds = array ();
$name = strtolower ( $name );
foreach ( $serviceIds as $serviceId ) {
if ( false === strpos ( $serviceId , $name )) {
continue ;
}
$foundServiceIds [] = $serviceId ;
}
return $foundServiceIds ;
}
2011-03-10 15:09:40 +00:00
}