Skip to content

Commit 4f020b5

Browse files
committed
[FrameworkBundle] Extends the RequestDataCollector
Moves the logic of collect controller data while forwarding in the framework bundle.
1 parent 227ac77 commit 4f020b5

File tree

3 files changed

+117
-56
lines changed

3 files changed

+117
-56
lines changed
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+
}

src/Symfony/Bundle/FrameworkBundle/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>

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

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828
class RequestDataCollector extends DataCollector implements EventSubscriberInterface
2929
{
30+
/** @var \SplObjectStorage */
3031
protected $controllers;
3132

3233
public function __construct()
@@ -286,26 +287,9 @@ public function getRedirect()
286287
return isset($this->data['redirect']) ? $this->data['redirect'] : false;
287288
}
288289

289-
/**
290-
* Gets the parsed forward controller.
291-
*
292-
* @return array|bool An array with keys 'token' the forward profile token, and
293-
* 'controller' the parsed forward controller, false otherwise
294-
*/
295-
public function getForward()
296-
{
297-
return isset($this->data['forward']) ? $this->data['forward'] : false;
298-
}
299-
300290
public function onKernelController(FilterControllerEvent $event)
301291
{
302292
$this->controllers[$event->getRequest()] = $event->getController();
303-
304-
if ($parentRequestAttributes = $event->getRequest()->attributes->get('_forwarded')) {
305-
if ($parentRequestAttributes instanceof ParameterBag) {
306-
$parentRequestAttributes->set('_forward_controller', $event->getController());
307-
}
308-
}
309293
}
310294

311295
public static function getSubscribedEvents()
@@ -321,51 +305,14 @@ public function getName()
321305
return 'request';
322306
}
323307

324-
private function getCookieHeader($name, $value, $expires, $path, $domain, $secure, $httponly)
325-
{
326-
$cookie = sprintf('%s=%s', $name, urlencode($value));
327-
328-
if (0 !== $expires) {
329-
if (is_numeric($expires)) {
330-
$expires = (int) $expires;
331-
} elseif ($expires instanceof \DateTime) {
332-
$expires = $expires->getTimestamp();
333-
} else {
334-
$tmp = strtotime($expires);
335-
if (false === $tmp || -1 == $tmp) {
336-
throw new \InvalidArgumentException(sprintf('The "expires" cookie parameter is not valid (%s).', $expires));
337-
}
338-
$expires = $tmp;
339-
}
340-
341-
$cookie .= '; expires='.str_replace('+0000', '', \DateTime::createFromFormat('U', $expires, new \DateTimeZone('GMT'))->format('D, d-M-Y H:i:s T'));
342-
}
343-
344-
if ($domain) {
345-
$cookie .= '; domain='.$domain;
346-
}
347-
348-
$cookie .= '; path='.$path;
349-
350-
if ($secure) {
351-
$cookie .= '; secure';
352-
}
353-
354-
if ($httponly) {
355-
$cookie .= '; httponly';
356-
}
357-
358-
return $cookie;
359-
}
360-
361308
/**
362309
* Parse a controller.
363310
*
364311
* @param mixed $controller The controller to parse
365312
*
366313
* @return array|string An array of controller data or a simple string
367314
*/
368-
private function parseController($controller)
315+
protected function parseController($controller)
369316
{
370317
if (is_string($controller) && false !== strpos($controller, '::')) {
371318
$controller = explode('::', $controller);
@@ -418,4 +365,41 @@ private function parseController($controller)
418365

419366
return (string) $controller ?: 'n/a';
420367
}
368+
369+
private function getCookieHeader($name, $value, $expires, $path, $domain, $secure, $httponly)
370+
{
371+
$cookie = sprintf('%s=%s', $name, urlencode($value));
372+
373+
if (0 !== $expires) {
374+
if (is_numeric($expires)) {
375+
$expires = (int) $expires;
376+
} elseif ($expires instanceof \DateTime) {
377+
$expires = $expires->getTimestamp();
378+
} else {
379+
$tmp = strtotime($expires);
380+
if (false === $tmp || -1 == $tmp) {
381+
throw new \InvalidArgumentException(sprintf('The "expires" cookie parameter is not valid (%s).', $expires));
382+
}
383+
$expires = $tmp;
384+
}
385+
386+
$cookie .= '; expires='.str_replace('+0000', '', \DateTime::createFromFormat('U', $expires, new \DateTimeZone('GMT'))->format('D, d-M-Y H:i:s T'));
387+
}
388+
389+
if ($domain) {
390+
$cookie .= '; domain='.$domain;
391+
}
392+
393+
$cookie .= '; path='.$path;
394+
395+
if ($secure) {
396+
$cookie .= '; secure';
397+
}
398+
399+
if ($httponly) {
400+
$cookie .= '; httponly';
401+
}
402+
403+
return $cookie;
404+
}
421405
}

0 commit comments

Comments
 (0)