2011-10-22 09:01:37 +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 ;
2015-03-31 13:42:44 +01:00
use Symfony\Component\Console\Input\ArrayInput ;
2011-10-22 09:01:37 +01:00
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputInterface ;
2013-10-18 19:18:27 +01:00
use Symfony\Component\Console\Input\InputOption ;
2011-10-22 09:01:37 +01:00
use Symfony\Component\Console\Output\OutputInterface ;
2015-03-31 13:42:44 +01:00
use Symfony\Component\Console\Style\SymfonyStyle ;
2011-10-22 09:01:37 +01:00
use Symfony\Component\Routing\RouterInterface ;
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher ;
/**
* A console command to test route matching .
*
* @ author Fabien Potencier < fabien @ symfony . com >
2017-07-22 10:58:19 +01:00
*
* @ final since version 3.4
2011-10-22 09:01:37 +01:00
*/
class RouterMatchCommand extends ContainerAwareCommand
{
2017-07-22 10:58:19 +01:00
private $router ;
/**
* @ param RouterInterface $router
*/
public function __construct ( $router = null )
{
if ( ! $router instanceof RouterInterface ) {
@ trigger_error ( sprintf ( 'Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.' , __METHOD__ ), E_USER_DEPRECATED );
2017-08-06 11:59:30 +01:00
parent :: __construct ( $router );
2017-07-22 10:58:19 +01:00
return ;
}
2017-08-06 11:59:30 +01:00
parent :: __construct ();
2017-07-22 10:58:19 +01:00
$this -> router = $router ;
}
2011-10-22 09:01:37 +01:00
/**
2013-03-07 13:36:36 +00:00
* { @ inheritdoc }
2017-07-22 10:58:19 +01:00
*
* BC to be removed in 4.0
2011-10-22 09:01:37 +01:00
*/
public function isEnabled ()
{
2017-07-22 10:58:19 +01:00
if ( null !== $this -> router ) {
return parent :: isEnabled ();
}
2011-10-22 09:01:37 +01:00
if ( ! $this -> getContainer () -> has ( 'router' )) {
return false ;
}
$router = $this -> getContainer () -> get ( 'router' );
if ( ! $router instanceof RouterInterface ) {
return false ;
}
2011-10-29 11:05:45 +01:00
2011-10-22 09:01:37 +01:00
return parent :: isEnabled ();
}
/**
2013-03-07 13:36:36 +00:00
* { @ inheritdoc }
2011-10-22 09:01:37 +01:00
*/
protected function configure ()
{
$this
2012-02-12 15:37:55 +00:00
-> setName ( 'router:match' )
2011-10-22 09:01:37 +01:00
-> setDefinition ( array (
new InputArgument ( 'path_info' , InputArgument :: REQUIRED , 'A path info' ),
2014-03-13 15:16:35 +00:00
new InputOption ( 'method' , null , InputOption :: VALUE_REQUIRED , 'Sets the HTTP method' ),
2014-03-21 16:46:08 +00:00
new InputOption ( 'scheme' , null , InputOption :: VALUE_REQUIRED , 'Sets the URI scheme (usually http or https)' ),
new InputOption ( 'host' , null , InputOption :: VALUE_REQUIRED , 'Sets the URI host' ),
2011-10-22 09:01:37 +01:00
))
-> setDescription ( 'Helps debug routes by simulating a path info match' )
2015-12-21 11:01:57 +00:00
-> setHelp ( <<< 'EOF'
2014-03-21 16:46:08 +00:00
The < info >% command . name %</ info > shows which routes match a given request and which don ' t and for what reason :
2011-10-22 09:01:37 +01:00
2012-02-12 15:37:55 +00:00
< info > php % command . full_name % / foo </ info >
2015-03-31 13:42:44 +01:00
2015-01-04 09:52:37 +00:00
or
2014-03-21 16:46:08 +00:00
< info > php % command . full_name % / foo -- method POST -- scheme https -- host symfony . com -- verbose </ info >
2014-03-13 04:05:55 +00:00
2011-10-22 09:01:37 +01:00
EOF
)
;
}
/**
2013-03-07 13:36:36 +00:00
* { @ inheritdoc }
2011-10-22 09:01:37 +01:00
*/
protected function execute ( InputInterface $input , OutputInterface $output )
{
2017-07-22 10:58:19 +01:00
// BC to be removed in 4.0
if ( null === $this -> router ) {
$this -> router = $this -> getContainer () -> get ( 'router' );
}
2015-12-07 12:11:35 +00:00
$io = new SymfonyStyle ( $input , $output );
2015-03-31 13:42:44 +01:00
2017-07-22 10:58:19 +01:00
$context = $this -> router -> getContext ();
2014-03-13 15:16:35 +00:00
if ( null !== $method = $input -> getOption ( 'method' )) {
$context -> setMethod ( $method );
}
2014-03-21 16:46:08 +00:00
if ( null !== $scheme = $input -> getOption ( 'scheme' )) {
$context -> setScheme ( $scheme );
}
2014-03-13 15:16:35 +00:00
if ( null !== $host = $input -> getOption ( 'host' )) {
$context -> setHost ( $host );
}
2013-10-18 19:18:27 +01:00
2017-07-22 10:58:19 +01:00
$matcher = new TraceableUrlMatcher ( $this -> router -> getRouteCollection (), $context );
2011-10-22 09:01:37 +01:00
$traces = $matcher -> getTraces ( $input -> getArgument ( 'path_info' ));
2015-12-07 12:11:35 +00:00
$io -> newLine ();
2015-03-31 13:42:44 +01:00
2011-10-22 09:01:37 +01:00
$matches = false ;
2014-03-13 04:05:55 +00:00
foreach ( $traces as $trace ) {
2011-10-22 09:01:37 +01:00
if ( TraceableUrlMatcher :: ROUTE_ALMOST_MATCHES == $trace [ 'level' ]) {
2015-12-07 12:11:35 +00:00
$io -> text ( sprintf ( 'Route <info>"%s"</> almost matches but %s' , $trace [ 'name' ], lcfirst ( $trace [ 'log' ])));
2011-10-22 09:01:37 +01:00
} elseif ( TraceableUrlMatcher :: ROUTE_MATCHES == $trace [ 'level' ]) {
2015-12-07 12:11:35 +00:00
$io -> success ( sprintf ( 'Route "%s" matches' , $trace [ 'name' ]));
2013-02-02 15:51:31 +00:00
2015-03-31 13:42:44 +01:00
$routerDebugCommand = $this -> getApplication () -> find ( 'debug:router' );
$routerDebugCommand -> run ( new ArrayInput ( array ( 'name' => $trace [ 'name' ])), $output );
2013-02-02 15:51:31 +00:00
2011-10-22 09:01:37 +01:00
$matches = true ;
} elseif ( $input -> getOption ( 'verbose' )) {
2015-12-07 12:11:35 +00:00
$io -> text ( sprintf ( 'Route "%s" does not match: %s' , $trace [ 'name' ], $trace [ 'log' ]));
2011-10-22 09:01:37 +01:00
}
}
if ( ! $matches ) {
2015-12-07 12:11:35 +00:00
$io -> error ( sprintf ( 'None of the routes match the path "%s"' , $input -> getArgument ( 'path_info' )));
2012-09-24 09:52:21 +01:00
return 1 ;
2011-10-22 09:01:37 +01:00
}
}
}