Skip to content

Commit 1247a7e

Browse files
committed
feature #17589 [3.1] [WebProfilerBundle] [DX] Feature allow forward and redirection detection in wdt (HeahDude)
This PR was merged into the 3.1-dev branch. Discussion ---------- [3.1] [WebProfilerBundle] [DX] Feature allow forward and redirection detection in wdt | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #14358, #17501 | License | MIT | Doc PR | ? This PR allows to : - track explicit forward from `\Symfony\Bundle\FrameWorkBundle\Controller\Controller` in the web debug toolbar. - or pass a request attribute `_forwarded` with the current request attributes (an instance of `ParameterBag`) as value to your sub request before handling it. - see if you've been redirected (require session enabled) When redirected you will see the name of the route (if any) and a link to the profile of the original request. ![redirect](https://cloud.githubusercontent.com/assets/10107633/12716952/9aacdcba-c8e4-11e5-9a64-d26fe27f1cae.jpg) In case of forwarding, the name of the controller is a file link and next to it there is a direct link to the profile of the sub request. ![forward](https://cloud.githubusercontent.com/assets/10107633/12716968/ba6b1fbc-c8e4-11e5-85fc-7f71969cb372.jpg) This works pretty well in __Silex__ too by registering `SessionServiceProvider()` for redirections or by providing this method for forwarding : ```php class App extends \Silex\Application // (php7 bootstrap) $app = new class extends \Silex\Application { { public function forward($controller, array $path = array(), array $query = array() { if (!$this->booted) { throw new LogicException(sprintf('Method %s must be called from a controller.', __METHOD__)); } $this->flush(); $request = $this['request_stack']->getCurrentRequest(); $path['_forwarded'] = $request->attributes; $path['_controller'] = $controller; $subRequest = $request->duplicate($query, null, $path); return $this['kernel']->handle($subRequest, HttpKernelInterface::SUB_REQUEST); } } ``` Commits ------- 0a0e8af [WebProfilerBundle] show the http method in wdt if not 'GET' 4f020b5 [FrameworkBundle] Extends the RequestDataCollector 227ac77 [WebProfilerBundle] [FrameworkBundle] profile forward controller action 0a1b284 [WebProfiler] [HttpKernel] profile redirections
2 parents 154f69d + e96bd82 commit 1247a7e

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

Controller/Controller.php

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

7173
return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
7274
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\ParameterBag;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector as BaseRequestCollector;
18+
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
19+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20+
21+
/**
22+
* RequestDataCollector.
23+
*
24+
* @author Jules Pietri <[email protected]>
25+
*/
26+
class RequestDataCollector extends BaseRequestCollector implements EventSubscriberInterface
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function collect(Request $request, Response $response, \Exception $exception = null)
32+
{
33+
parent::collect($request, $response, $exception);
34+
35+
if ($parentRequestAttributes = $request->attributes->get('_forwarded')) {
36+
if ($parentRequestAttributes instanceof ParameterBag) {
37+
$parentRequestAttributes->set('_forward_token', $response->headers->get('x-debug-token'));
38+
}
39+
}
40+
if ($request->attributes->has('_forward_controller')) {
41+
$this->data['forward'] = array(
42+
'token' => $request->attributes->get('_forward_token'),
43+
'controller' => $this->parseController($request->attributes->get('_forward_controller')),
44+
);
45+
}
46+
}
47+
48+
/**
49+
* Gets the parsed forward controller.
50+
*
51+
* @return array|bool An array with keys 'token' the forward profile token, and
52+
* 'controller' the parsed forward controller, false otherwise
53+
*/
54+
public function getForward()
55+
{
56+
return isset($this->data['forward']) ? $this->data['forward'] : false;
57+
}
58+
59+
public function onKernelController(FilterControllerEvent $event)
60+
{
61+
$this->controllers[$event->getRequest()] = $event->getController();
62+
63+
if ($parentRequestAttributes = $event->getRequest()->attributes->get('_forwarded')) {
64+
if ($parentRequestAttributes instanceof ParameterBag) {
65+
$parentRequestAttributes->set('_forward_controller', $event->getController());
66+
}
67+
}
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function getName()
74+
{
75+
return 'request';
76+
}
77+
}

Resources/config/collectors.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<call method="setKernel"><argument type="service" id="kernel" on-invalid="ignore" /></call>
1111
</service>
1212

13-
<service id="data_collector.request" class="Symfony\Component\HttpKernel\DataCollector\RequestDataCollector">
13+
<service id="data_collector.request" class="Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector">
1414
<tag name="kernel.event_subscriber" />
1515
<tag name="data_collector" template="@WebProfiler/Collector/request.html.twig" id="request" priority="335" />
1616
</service>

0 commit comments

Comments
 (0)