Skip to content

Commit 64ea95d

Browse files
committed
[WebProfilerBundle] Add redirection info to the router panel
Conflicts: src/Symfony/Bundle/FrameworkBundle/DataCollector/RouterDataCollector.php src/Symfony/Bundle/FrameworkBundle/Resources/config/collectors.xml src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig
1 parent 826bd23 commit 64ea95d

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

src/Symfony/Bundle/FrameworkBundle/DataCollector/RouterDataCollector.php

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\HttpFoundation\Response;
1616
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
17+
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
18+
use Symfony\Component\HttpFoundation\RedirectResponse;
19+
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
1720

1821
/**
1922
* RouterDataCollector.
@@ -22,11 +25,73 @@
2225
*/
2326
class RouterDataCollector extends DataCollector
2427
{
28+
protected $controllers;
29+
30+
public function __construct()
31+
{
32+
$this->controllers = new \SplObjectStorage();
33+
34+
$this->data = array(
35+
'redirect' => false,
36+
'url' => null,
37+
'route' => null,
38+
);
39+
}
40+
2541
/**
2642
* {@inheritdoc}
2743
*/
2844
public function collect(Request $request, Response $response, \Exception $exception = null)
2945
{
46+
if ($response instanceof RedirectResponse) {
47+
$this->data['redirect'] = true;
48+
$this->data['url'] = $response->getTargetUrl();
49+
50+
if ($this->controllers->contains($request)) {
51+
$controller = $this->controllers[$request];
52+
if (is_array($controller)) {
53+
$controller = $controller[0];
54+
}
55+
56+
if ($controller instanceof RedirectController) {
57+
$this->data['route'] = $request->attributes->get('_route', 'n/a');
58+
}
59+
}
60+
}
61+
}
62+
63+
/**
64+
* Remembers the controller associated to each request.
65+
*
66+
* @param FilterControllerEvent The filter controller event
67+
*/
68+
public function onKernelController(FilterControllerEvent $event)
69+
{
70+
$this->controllers[$event->getRequest()] = $event->getController();
71+
}
72+
73+
/**
74+
* @return Boolean Whether this request will result in a redirect
75+
*/
76+
public function getRedirect()
77+
{
78+
return $this->data['redirect'];
79+
}
80+
81+
/**
82+
* @return string|null The target URL
83+
*/
84+
public function getTargetUrl()
85+
{
86+
return $this->data['url'];
87+
}
88+
89+
/**
90+
* @return string|null The target route
91+
*/
92+
public function getTargetRoute()
93+
{
94+
return $this->data['route'];
3095
}
3196

3297
/**
@@ -36,4 +101,4 @@ public function getName()
36101
{
37102
return 'router';
38103
}
39-
}
104+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
<tag name="data_collector" template="WebProfilerBundle:Collector:memory" id="memory" priority="255" />
5353
</service>
5454

55-
<service id="data_collector.router" class="%data_collector.router.class%" public="false">
55+
<service id="data_collector.router" class="%data_collector.router.class%" >
56+
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController"/>
5657
<tag name="data_collector" template="WebProfilerBundle:Collector:router" id="router" priority="255" />
5758
</service>
5859
</services>

src/Symfony/Bundle/WebProfilerBundle/Controller/RouterController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function panelAction($token)
4646

4747
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Router:panel.html.twig', array(
4848
'request' => $request,
49+
'router' => $profile->getCollector('router'),
4950
'traces' => $matcher->getTraces($request->getPathInfo()),
5051
));
5152
}

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Router/panel.html.twig

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44
<li><strong>Route:&nbsp;</strong>{{ request.route }}</li>
55
<li>
66
<strong>Route parameters:</strong>
7-
{% include 'WebProfilerBundle:Profiler:table.html.twig' with { 'data': request.routeParams } only %}
7+
{% if request.routeParams|length %}
8+
{% include 'WebProfilerBundle:Profiler:table.html.twig' with { 'data': request.routeParams, 'class': 'inline' } only %}
9+
{% else %}
10+
<em>No parameters</em>
11+
{% endif %}
812
</li>
9-
13+
{% if router.redirect %}
14+
<li>
15+
<strong>Redirecting to:&nbsp;</strong> "{{ router.targetUrl }}" {% if router.targetRoute %}(route: "{{ router.targetRoute }}"){% endif %}
16+
<li>
17+
{% endif %}
1018
<li>
1119
<strong>Route matching:</strong>
12-
<table class="routing">
20+
<table class="routing inline">
1321
<tr>
1422
<th>Route name</th>
1523
<th>Pattern</th>

0 commit comments

Comments
 (0)