2010-02-17 13:55:05 +00:00
< ? php
2011-01-15 13:29:43 +00:00
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2011-01-15 13:29:43 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2010-07-09 09:05:26 +01:00
namespace Symfony\Bundle\FrameworkBundle\Command ;
2010-02-17 13:55:05 +00:00
2013-04-30 16:08:17 +01:00
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputInterface ;
2013-08-03 14:13:38 +01:00
use Symfony\Component\Console\Input\InputOption ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\Console\Output\OutputInterface ;
2011-10-16 12:56:51 +01:00
use Symfony\Component\Routing\RouterInterface ;
2013-10-02 15:07:32 +01:00
use Symfony\Component\Routing\Route ;
2010-02-17 13:55:05 +00:00
/**
2014-03-13 04:05:55 +00:00
* A console command for retrieving information about routes .
2010-02-17 13:55:05 +00:00
*
2011-03-06 11:40:06 +00:00
* @ author Fabien Potencier < fabien @ symfony . com >
2013-03-08 12:06:24 +00:00
* @ author Tobias Schultze < http :// tobion . de >
2010-02-17 13:55:05 +00:00
*/
2011-06-20 20:04:55 +01:00
class RouterDebugCommand extends ContainerAwareCommand
2010-02-17 13:55:05 +00:00
{
2011-06-28 23:18:41 +01:00
/**
2013-03-07 13:36:36 +00:00
* { @ inheritdoc }
2011-06-28 23:18:41 +01:00
*/
public function isEnabled ()
{
if ( ! $this -> getContainer () -> has ( 'router' )) {
return false ;
}
$router = $this -> getContainer () -> get ( 'router' );
2011-10-16 12:56:51 +01:00
if ( ! $router instanceof RouterInterface ) {
2011-06-28 23:18:41 +01:00
return false ;
}
2011-10-29 11:05:45 +01:00
2011-06-28 23:18:41 +01:00
return parent :: isEnabled ();
}
2010-05-06 12:25:53 +01:00
/**
2013-03-07 13:36:36 +00:00
* { @ inheritdoc }
2010-05-06 12:25:53 +01:00
*/
protected function configure ()
2010-02-17 13:55:05 +00:00
{
2010-05-06 12:25:53 +01:00
$this
2014-08-09 16:20:46 +01:00
-> setName ( 'debug:router' )
-> setAliases ( array (
'router:debug' ,
))
2010-05-06 12:25:53 +01:00
-> setDefinition ( array (
new InputArgument ( 'name' , InputArgument :: OPTIONAL , 'A route name' ),
2013-04-30 16:08:17 +01:00
new InputOption ( 'show-controllers' , null , InputOption :: VALUE_NONE , 'Show assigned controllers in overview' ),
2013-10-02 15:03:04 +01:00
new InputOption ( 'format' , null , InputOption :: VALUE_REQUIRED , 'To output route(s) in other formats' , 'txt' ),
2013-04-30 16:08:17 +01:00
new InputOption ( 'raw' , null , InputOption :: VALUE_NONE , 'To output raw route(s)' ),
2010-05-06 12:25:53 +01:00
))
-> setDescription ( 'Displays current routes for an application' )
-> setHelp ( <<< EOF
2012-02-12 15:37:55 +00:00
The < info >% command . name %</ info > displays the configured routes :
2010-05-06 12:25:53 +01:00
2012-02-12 15:37:55 +00:00
< info > php % command . full_name %</ info >
2015-01-13 14:10:03 +00:00
2010-05-06 12:25:53 +01:00
EOF
)
;
2010-02-17 13:55:05 +00:00
}
2010-05-06 12:25:53 +01:00
/**
2013-03-07 13:36:36 +00:00
* { @ inheritdoc }
*
* @ throws \InvalidArgumentException When route does not exist
2010-05-06 12:25:53 +01:00
*/
protected function execute ( InputInterface $input , OutputInterface $output )
2010-02-17 13:55:05 +00:00
{
2015-01-13 14:10:03 +00:00
if ( false !== strpos ( $input -> getFirstArgument (), ':d' )) {
$output -> writeln ( '<comment>The use of "router:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:router" instead.</comment>' );
}
2012-04-17 09:08:13 +01:00
$name = $input -> getArgument ( 'name' );
2013-04-30 16:08:17 +01:00
$helper = new DescriptorHelper ();
2010-05-06 12:25:53 +01:00
2012-04-17 09:08:13 +01:00
if ( $name ) {
2013-04-30 16:08:17 +01:00
$route = $this -> getContainer () -> get ( 'router' ) -> getRouteCollection () -> get ( $name );
if ( ! $route ) {
throw new \InvalidArgumentException ( sprintf ( 'The route "%s" does not exist.' , $name ));
2013-08-03 14:13:38 +01:00
}
2013-10-02 15:07:32 +01:00
$this -> convertController ( $route );
2013-10-02 15:03:04 +01:00
$helper -> describe ( $output , $route , array (
2014-10-26 07:41:27 +00:00
'format' => $input -> getOption ( 'format' ),
2013-10-02 15:03:04 +01:00
'raw_text' => $input -> getOption ( 'raw' ),
2014-10-26 07:41:27 +00:00
'name' => $name ,
2013-10-02 15:03:04 +01:00
));
2013-04-30 16:08:17 +01:00
} else {
$routes = $this -> getContainer () -> get ( 'router' ) -> getRouteCollection ();
2013-10-02 15:07:32 +01:00
foreach ( $routes as $route ) {
$this -> convertController ( $route );
}
2013-04-30 16:08:17 +01:00
$helper -> describe ( $output , $routes , array (
2014-10-26 07:41:27 +00:00
'format' => $input -> getOption ( 'format' ),
'raw_text' => $input -> getOption ( 'raw' ),
2013-04-30 16:08:17 +01:00
'show_controllers' => $input -> getOption ( 'show-controllers' ),
));
2010-05-06 12:25:53 +01:00
}
2010-02-17 13:55:05 +00:00
}
2013-10-02 15:07:32 +01:00
private function convertController ( Route $route )
{
$nameParser = $this -> getContainer () -> get ( 'controller_name_converter' );
if ( $route -> hasDefault ( '_controller' )) {
try {
$route -> setDefault ( '_controller' , $nameParser -> build ( $route -> getDefault ( '_controller' )));
} catch ( \InvalidArgumentException $e ) {
}
}
}
2010-02-17 13:55:05 +00:00
}