8000 [FrameworkBundle] added a command to help debugging route matching pr… · Kiruban2011/symfony@8cc3158 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8cc3158

Browse files
committed
[FrameworkBundle] added a command to help debugging route matching problems
1 parent 24b9392 commit 8cc3158

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Command;
13+
14+
use Symfony\Component\Console\Input\InputArgument;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Routing\RouterInterface;
18+
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
19+
20+
/**
21+
* A console command to test route matching.
22+
*
23+
* @author Fabien Potencier <fabien@symfony.com>
24+
*/
25+
class RouterMatchCommand extends ContainerAwareCommand
26+
{
27+
/**
28+
* {@inheritDoc}
29+
*/
30+
public function isEnabled()
31+
{
32+
if (!$this->getContainer()->has('router')) {
33+
return false;
34+
}
35+
$router = $this->getContainer()->get('router');
36+
if (!$router instanceof RouterInterface) {
37+
return false;
38+
}
39+
return parent::isEnabled();
40+
}
41+
42+
/**
43+
* @see Command
44+
*/
45+
protected function configure()
46+
{
47+
$this
48 10000 +
->setDefinition(array(
49+
new InputArgument('path_info', InputArgument::REQUIRED, 'A path info'),
50+
))
51+
->setName('router:match')
52+
->setDescription('Helps debug routes by simulating a path info match')
53+
->setHelp(<<<EOF
54+
The <info>router:match</info> simulates a path info match:
55+
56+
<info>router:match /foo</info>
57+
EOF
58+
)
59+
;
60+
}
61+
62+
/**
63+
* @see Command
64+
*/
65+
protected function execute(InputInterface $input, OutputInterface $output)
66+
{
67+
$router = $this->getContainer()->get('router');
68+
$matcher = new TraceableUrlMatcher($router->getRouteCollection(), $router->getContext());
69+
70+
$traces = $matcher->getTraces($input->getArgument('path_info'));
71+
72+
$matches = false;
73+
foreach ($traces as $i => $trace) {
74+
if (TraceableUrlMatcher::ROUTE_ALMOST_MATCHES == $trace['level']) {
75+
$output->writeln(sprintf('<fg=yellow>Route "%s" almost matches but %s</>', $trace['name'], lcfirst($trace['log'])));
76+
} elseif (TraceableUrlMatcher::ROUTE_MATCHES == $trace['level']) {
77+
$output->writeln(sprintf('<fg=green>Route "%s" matches</>', $trace['name']));
78+
$matches = true;
79+
} elseif ($input->getOption('verbose')) {
80+
$output->writeln(sprintf('Route "%s" does not match: %s', $trace['name'], $trace['log']));
81+
}
82+
}
83+
84+
if (!$matches) {
85+
$output->writeln('<fg=red>None of the routes matches</>');
86+
}
87+
}
88+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\DataCollector;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
17+
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
18+
use Symfony\Component\Routing\RouterInterface;
19+
20+
/**
21+
* RouterDataCollector.
22+
*
23+
* @author Fabien Potencier <fabien@symfony.com>
24+
*/
25+
class RouterDataCollector extends DataCollector
26+
{
27+
private $router;
28+
29+
public function __construct(RouterInterface $router = null)
30+
{
31+
$this->router = $router;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function collect(Request $request, Response $response, \Exception $exception = null)
38+
{
39+
$this->data['path_info'] = $request->getPathInfo();
40+
41+
if (!$this->router) {
42+
$this->data['traces'] = array();
43+
} else {
44+
$matcher = new TraceableUrlMatcher($this->router->getRouteCollection(), $this->router->getContext());
45+
46+
$this->data['traces'] = $matcher->getTraces($request->getPathInfo());
47+
}
48+
}
49+
50+
public function getPathInfo()
51+
{
52+
return $this->data['path_info'];
53+
}
54+
55+
public function getTraces()
56+
{
57+
return $this->data['traces'];
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function getName()
64+
{
65+
return 'router';
66+
}
67+
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/collectors.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<parameter key="data_collector.logger.class">Symfony\Component\HttpKernel\DataCollector\LoggerDataCollector</parameter>
1313
<parameter key="data_collector.time.class">Symfony\Component\HttpKernel\DataCollector\TimeDataCollector</parameter>
1414
<parameter key="data_collector.memory.class">Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector</parameter>
15+
<parameter key="data_collector.router.class">Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector</parameter>
1516
</parameters>
1617

1718
<services>
@@ -50,5 +51,10 @@
5051
<service id="data_collector.memory" class="%data_collector.memory.class%" public="false">
5152
<tag name="data_collector" template="WebProfilerBundle:Collector:memory" id="memory" priority="255" />
5253
</service>
54+
55+
<service id="data_collector.router" class="%data_collector.router.class%" public="false">
56+
<tag name="data_collector" template="WebProfilerBundle:Collector:router" id="router" priority="255" />
57+
<argument type="service" id="router" on-invalid="ignore" />
58+
</service>
5359
</services>
5460
</container>

0 commit comments

Comments
 (0)
0