Skip to content

Commit 411dd48

Browse files
committed
fix #14358 and #17501
1 parent 5b59703 commit 411dd48

File tree

5 files changed

+172
-56
lines changed

5 files changed

+172
-56
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ protected function generateUrl($route, $parameters = array(), $referenceType = U
6464
*/
6565
protected function forward($controller, array $path = array(), array $query = array())
6666
{
67+
$request = $this->container->get('request_stack')->getCurrentRequest();
68+
$path['_forwarded'] = $request->attributes;
6769
$path['_controller'] = $controller;
68-
$subRequest = $this->container->get('request_stack')->getCurrentRequest()->duplicate($query, null, $path);
70+
$subRequest = $request->duplicate($query, null, $path);
6971

7072
return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
7173
}

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,24 @@
22

33
{% block toolbar %}
44
{% set request_handler %}
5-
{% if collector.controller.class is defined %}
6-
{% set link = collector.controller.file|file_link(collector.controller.line) %}
7-
{% if link %}<a href="{{ link }}" title="{{ collector.controller.file }}">{% else %}<span>{% endif %}
8-
9-
{{ collector.controller.class|abbr_class|striptags }}
10-
11-
{%- if collector.controller.method -%}
12-
&nbsp;::&nbsp;{{ collector.controller.method }}
13-
{%- endif -%}
5+
{% import _self as helper %}
6+
{{ helper.set_handler(collector.controller) }}
7+
{% endset %}
148

15-
{% if link %}</a>{% else %}</span>{% endif %}
16-
{% else %}
17-
<span>{{ collector.controller }}</span>
18-
{% endif %}
9+
{% if collector.forward %}
10+
{% set forward_handler %}
11+
{% import _self as helper %}
12+
{{ helper.set_handler(collector.forward.controller) }}
1913
{% endset %}
14+
{% endif %}
2015

2116
{% set request_status_code_color = (collector.statuscode >= 400) ? 'red' : (collector.statuscode >= 300) ? 'yellow' : 'green' %}
2217

2318
{% set icon %}
2419
<span class="sf-toolbar-status sf-toolbar-status-{{ request_status_code_color }}">{{ collector.statuscode }}</span>
2520
{% if collector.route %}
21+
{% if collector.forward %}&nbsp;{{ include('@WebProfiler/Icon/forward.svg') }}{% endif %}
22+
{% if collector.redirect %}&nbsp;{{ include('@WebProfiler/Icon/redirect.svg') }}{% endif %}
2623
<span class="sf-toolbar-label">@</span>
2724
<span class="sf-toolbar-value sf-toolbar-info-piece-additional">{{ collector.route }}</span>
2825
{% endif %}
@@ -55,6 +52,26 @@
5552
<b>Has session</b>
5653
<span>{% if collector.sessionmetadata|length %}yes{% else %}no{% endif %}</span>
5754
</div>
55+
56+
{% if forward_handler is defined %}
57+
<div class="sf-toolbar-info-piece">
58+
<b>Forwarded to</b>
59+
<span>
60+
{{ forward_handler }}
61+
&nbsp;(<a href="{{ path('_profiler', { token: collector.forward.token }) }}">{{ collector.forward.token }}</a>)
62+
</span>
63+
</div>
64+
{% endif %}
65+
66+
{% if collector.redirect %}
67+
<div class="sf-toolbar-info-piece">
68+
<b>Redirected from</b>
69+
<span>
70+
@{{ collector.redirect.route }}
71+
&nbsp;(<a href="{{ path('_profiler', { token: collector.redirect.token }) }}">{{ collector.redirect.token }}</a>)
72+
</span>
73+
</div>
74+
{% endif %}
5875
{% endset %}
5976

6077
{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { link: profiler_url }) }}
@@ -224,3 +241,20 @@
224241
{% endif %}
225242
</div>
226243
{% endblock %}
244+
245+
{% macro set_handler(controller) %}
246+
{% if controller.class is defined %}
247+
{% set link = controller.file|file_link(controller.line) %}
248+
{% if link %}<a href="{{ link }}" title="{{ controller.file }}">{% else %}<span>{% endif %}
249+
250+
{{ controller.class|abbr_class|striptags }}
251+
252+
{%- if controller.method -%}
253+
&nbsp;::&nbsp;{{ controller.method }}
254+
{%- endif -%}
255+
256+
{% if link %}</a>{% else %}</span>{% endif %}
257+
{% else %}
258+
<span>{{ controller }}</span>
259+
{% endif %}
260+
{% endmacro %}
Lines changed: 4 additions & 0 deletions
Loading
Lines changed: 10 additions & 0 deletions
Loading

src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php

Lines changed: 108 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -122,48 +122,34 @@ public function collect(Request $request, Response $response, \Exception $except
122122
}
123123

124124
if (isset($this->controllers[$request])) {
125-
$controller = $this->controllers[$request];
126-
if (is_array($controller)) {
127-
try {
128-
$r = new \ReflectionMethod($controller[0], $controller[1]);
129-
$this->data['controller'] = array(
130-
'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0],
131-
'method' => $controller[1],
132-
'file' => $r->getFileName(),
133-
'line' => $r->getStartLine(),
134-
);
135-
} catch (\ReflectionException $e) {
136-
if (is_callable($controller)) {
137-
// using __call or __callStatic
138-
$this->data['controller'] = array(
139-
'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0],
140-
'method' => $controller[1],
141-
'file' => 'n/a',
142-
'line' => 'n/a',
143-
);
144-
}
145-
}
146-
} elseif ($controller instanceof \Closure) {
147-
$r = new \ReflectionFunction($controller);
148-
$this->data['controller'] = array(
149-
'class' => $r->getName(),
150-
'method' => null,
151-
'file' => $r->getFileName(),
152-
'line' => $r->getStartLine(),
153-
);
154-
} elseif (is_object($controller)) {
155-
$r = new \ReflectionClass($controller);
156-
$this->data['controller'] = array(
157-
'class' => $r->getName(),
158-
'method' => null,
159-
'file' => $r->getFileName(),
160-
'line' => $r->getStartLine(),
161-
);
162-
} else {
163-
$this->data['controller'] = (string) $controller ?: 'n/a';
164-
}
125+
$this->data['controller'] = $this->parseController($this->controllers[$request]);
165126
unset($this->controllers[$request]);
166127
}
128+
129+
if ($parentRequestAttributes = $request->attributes->get('_forwarded')) {
130+
if ($parentRequestAttributes instanceof ParameterBag) {
131+
$parentRequestAttributes->set('_forward_token', $response->headers->get('x-debug-token'));
132+
}
133+
}
134+
135+
if ($request->attributes->has('_forward_controller')) {
136+
$this->data['forward'] = array(
137+
'token' => $request->attributes->get('_forward_token'),
138+
'controller' => $request->attributes->get('_forward_controller'),
139+
);
140+
}
141+
142+
if ($request->hasSession() && $request->getSession()->has('sf_redirect')) {
143+
$this->data['redirect'] = $request->getSession()->get('sf_redirect');
144+
$request->getSession()->remove('sf_redirect');
145+
}
146+
147+
if ($request->hasSession() && $response->isRedirect()) {
148+
$request->getSession()->set('sf_redirect', array(
149+
'token' => $response->headers->get('x-debug-token'),
150+
'route' => $request->attributes->get('_route', 'n/a'),
151+
));
152+
}
167153
}
168154

169155
public function getPathInfo()
@@ -276,18 +262,41 @@ public function getRouteParams()
276262
}
277263

278264
/**
279-
* Gets the controller.
265+
* Gets the parsed controller.
280266
*
281-
* @return string The controller as a string
267+
* @return array|string The controller as a string or array of data
268+
* with keys 'class', 'method', 'file' and 'line'
282269
*/
283270
public function getController()
284271
{
285272
return $this->data['controller'];
286273
}
287274

275+
/**
276+
* Gets the parsed forward controller.
277+
*
278+
* @return array|bool An array with keys 'token' the forward profile token, and
279+
* 'controller' the parsed forward controller, false otherwise
280+
*/
281+
public function getForward()
282+
{
283+
return isset($this->data['forward']) ? $this->data['forward'] : false;
284+
}
285+
286+
public function getRedirect()
287+
{
288+
return isset($this->data['redirect']) ? $this->data['redirect'] : false;
289+
}
290+
288291
public function onKernelController(FilterControllerEvent $event)
289292
{
290293
$this->controllers[$event->getRequest()] = $event->getController();
294+
295+
if ($parentRequestAttributes = $event->getRequest()->attributes->get('_forwarded')) {
296+
if ($parentRequestAttributes instanceof ParameterBag) {
297+
$parentRequestAttributes->set('_forward_controller', $this->parseController($event->getController()));
298+
}
299+
}
291300
}
292301

293302
public static function getSubscribedEvents()
@@ -339,4 +348,61 @@ private function getCookieHeader($name, $value, $expires, $path, $domain, $secur
339348

340349
return $cookie;
341350
}
351+
352+
/**
353+
* Parse a controller.
354+
*
355+
* @param mixed $controller The controller to parse
356+
*
357+
* @return array|string An array of controller data or a simple string
358+
*/
359+
private function parseController($controller)
360+
{
361+
if (is_array($controller)) {
362+
try {
363+
$r = new \ReflectionMethod($controller[0], $controller[1]);
364+
365+
return array(
366+
'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0],
367+
'method' => $controller[1],
368+
'file' => $r->getFileName(),
369+
'line' => $r->getStartLine(),
370+
);
371+
} catch (\ReflectionException $e) {
372+
if (is_callable($controller)) {
373+
// using __call or __callStatic
374+
return array(
375+
'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0],
376+
'method' => $controller[1],
377+
'file' => 'n/a',
378+
'line' => 'n/a',
379+
);
380+
}
381+
}
382+
}
383+
384+
if ($controller instanceof \Closure) {
385+
$r = new \ReflectionFunction($controller);
386+
387+
return array(
388+
'class' => $r->getName(),
389+
'method' => null,
390+
'file' => $r->getFileName(),
391+
'line' => $r->getStartLine(),
392+
);
393+
}
394+
395+
if (is_object($controller)) {
396+
$r = new \ReflectionClass($controller);
397+
398+
return array(
399+
'class' => $r->getName(),
400+
'method' => null,
401+
'file' => $r->getFileName(),
402+
'line' => $r->getStartLine(),
403+
);
404+
}
405+
406+
return (string) $controller ?: 'n/a';
407+
}
342408
}

0 commit comments

Comments
 (0)