Skip to content

Commit

Permalink
[WebProfilerBundle] Imply forward request by a new X-Previous-Debug-T…
Browse files Browse the repository at this point in the history
…oken header
  • Loading branch information
ro0NL authored and fabpot committed Feb 12, 2018
1 parent 65b2bcd commit 07dd09d
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 72 deletions.
1 change: 1 addition & 0 deletions UPGRADE-4.1.md
Expand Up @@ -16,6 +16,7 @@ FrameworkBundle
---------------

* A `RouterInterface` that does not implement the `WarmableInterface` is deprecated and will not be supported in Symfony 5.0.
* The `RequestDataCollector` class has been deprecated and will be removed in Symfony 5.0. Use the `Symfony\Component\HttpKernel\DataCollector\RequestDataCollector` class instead.

HttpFoundation
--------------
Expand Down
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Expand Up @@ -15,6 +15,7 @@ FrameworkBundle
---------------

* Using a `RouterInterface` that does not implement the `WarmableInterface` is not supported anymore.
* The `RequestDataCollector` class has been removed. Use the `Symfony\Component\HttpKernel\DataCollector\RequestDataCollector` class instead.

HttpFoundation
--------------
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Expand Up @@ -9,7 +9,8 @@ CHANGELOG
* Allowed the `Router` to work with any PSR-11 container
* Added option in workflow dump command to label graph with a custom label
* Using a `RouterInterface` that does not implement the `WarmableInterface` is deprecated and will not be supported in Symfony 5.0.

* The `RequestDataCollector` class has been deprecated and will be removed in Symfony 5.0. Use the `Symfony\Component\HttpKernel\DataCollector\RequestDataCollector` class instead.

4.0.0
-----

Expand Down
Expand Up @@ -83,7 +83,6 @@ protected function generateUrl(string $route, array $parameters = array(), int $
protected function forward(string $controller, array $path = array(), array $query = array()): Response
{
$request = $this->container->get('request_stack')->getCurrentRequest();
$path['_forwarded'] = $request->attributes;
$path['_controller'] = $controller;
$subRequest = $request->duplicate($query, null, $path);

Expand Down
Expand Up @@ -11,67 +11,17 @@

namespace Symfony\Bundle\FrameworkBundle\DataCollector;

use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector as BaseRequestCollector;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector as BaseRequestDataCollector;

@trigger_error(sprintf('The "%s" class is deprecated since version 4.1 and will be removed in Symfony 5.0. Use %s instead.', RequestDataCollector::class, BaseRequestDataCollector::class), E_USER_DEPRECATED);

/**
* RequestDataCollector.
*
* @author Jules Pietri <jusles@heahprod.com>
*
* @deprecated since version 4.1, to be removed in Symfony 5.0
*/
class RequestDataCollector extends BaseRequestCollector implements EventSubscriberInterface
class RequestDataCollector extends BaseRequestDataCollector
{
/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
parent::collect($request, $response, $exception);

if ($parentRequestAttributes = $request->attributes->get('_forwarded')) {
if ($parentRequestAttributes instanceof ParameterBag) {
$parentRequestAttributes->set('_forward_token', $response->headers->get('x-debug-token'));
}
}
if ($request->attributes->has('_forward_controller')) {
$this->data['forward'] = array(
'token' => $request->attributes->get('_forward_token'),
'controller' => $this->parseController($request->attributes->get('_forward_controller')),
);
}
}

/**
* Gets the parsed forward controller.
*
* @return array|bool An array with keys 'token' the forward profile token, and
* 'controller' the parsed forward controller, false otherwise
*/
public function getForward()
{
return isset($this->data['forward']) ? $this->data['forward'] : false;
}

public function onKernelController(FilterControllerEvent $event)
{
$this->controllers[$event->getRequest()] = $event->getController();

if ($parentRequestAttributes = $event->getRequest()->attributes->get('_forwarded')) {
if ($parentRequestAttributes instanceof ParameterBag) {
$parentRequestAttributes->set('_forward_controller', $event->getController());
}
}
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'request';
}
}
Expand Up @@ -12,7 +12,7 @@
<call method="setKernel"><argument type="service" id="kernel" on-invalid="ignore" /></call>
</service>

<service id="data_collector.request" class="Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector">
<service id="data_collector.request" class="Symfony\Component\HttpKernel\DataCollector\RequestDataCollector">
<tag name="kernel.event_subscriber" />
<tag name="data_collector" template="@WebProfiler/Collector/request.html.twig" id="request" priority="335" />
</service>
Expand Down
Expand Up @@ -24,7 +24,6 @@ class SubRequestServiceResolutionController implements ContainerAwareInterface
public function indexAction()
{
$request = $this->container->get('request_stack')->getCurrentRequest();
$path['_forwarded'] = $request->attributes;
$path['_controller'] = 'TestBundle:SubRequestServiceResolution:fragment';
$subRequest = $request->duplicate(array(), null, $path);

Expand Down
Expand Up @@ -12,9 +12,10 @@
{% endset %}
{% endif %}

{% if collector.forward|default(false) %}
{% if collector.forwardtoken %}
{% set forward_profile = profile.childByToken(collector.forwardtoken) %}
{% set forward_handler %}
{{ helper.set_handler(collector.forward.controller) }}
{{ helper.set_handler(forward_profile ? forward_profile.collector('request').controller : 'n/a') }}
{% endset %}
{% endif %}

Expand All @@ -24,7 +25,7 @@
<span class="sf-toolbar-status sf-toolbar-status-{{ request_status_code_color }}">{{ collector.statuscode }}</span>
{% if collector.route %}
{% if collector.redirect %}{{ include('@WebProfiler/Icon/redirect.svg') }}{% endif %}
{% if collector.forward|default(false) %}{{ include('@WebProfiler/Icon/forward.svg') }}{% endif %}
{% if collector.forwardtoken %}{{ include('@WebProfiler/Icon/forward.svg') }}{% endif %}
<span class="sf-toolbar-label">{{ 'GET' != collector.method ? collector.method }} @</span>
<span class="sf-toolbar-value sf-toolbar-info-piece-additional">{{ collector.route }}</span>
{% endif %}
Expand Down Expand Up @@ -81,7 +82,7 @@
<b>Forwarded to</b>
<span>
{{ forward_handler }}
(<a href="{{ path('_profiler', { token: collector.forward.token }) }}">{{ collector.forward.token }}</a>)
(<a href="{{ path('_profiler', { token: collector.forwardtoken }) }}">{{ collector.forwardtoken }}</a>)
</span>
</div>
</div>
Expand Down
Expand Up @@ -44,18 +44,22 @@
</dl>
{%- endif %}

{% if request_collector and request_collector.forward|default(false) and request_collector.forward.controller.class is defined -%}
{%- set forward = request_collector.forward -%}
{%- set controller = forward.controller -%}
{% if request_collector and request_collector.forwardtoken -%}
{% set forward_profile = profile.childByToken(request_collector.forwardtoken) %}
{% set controller = forward_profile ? forward_profile.collector('request').controller : 'n/a' %}
<dl class="metadata">
<dt>Forwarded to</dt>
<dd>
{% set link = controller.file|file_link(controller.line) -%}
{% set link = controller.file is defined ? controller.file|file_link(controller.line) : null -%}
{%- if link %}<a href="{{ link }}" title="{{ controller.file }}">{% endif -%}
{{- controller.class|abbr_class|striptags -}}
{{- controller.method ? ' :: ' ~ controller.method }}
{% if controller.class is defined %}
{{- controller.class|abbr_class|striptags -}}
{{- controller.method ? ' :: ' ~ controller.method -}}
{% else %}
{{- controller -}}
{% endif %}
{%- if link %}</a>{% endif %}
(<a href="{{ path('_profiler', { token: forward.token }) }}">{{ forward.token }}</a>)
(<a href="{{ path('_profiler', { token: request_collector.forwardtoken }) }}">{{ request_collector.forwardtoken }}</a>)
</dd>
</dl>
{%- endif %}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/WebProfilerBundle/composer.json
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": "^7.1.3",
"symfony/http-kernel": "~3.4|~4.0",
"symfony/http-kernel": "~4.1",
"symfony/routing": "~3.4|~4.0",
"symfony/twig-bridge": "~3.4|~4.0",
"symfony/var-dumper": "~3.4|~4.0",
Expand Down
Expand Up @@ -158,6 +158,10 @@ public function collect(Request $request, Response $response, \Exception $except
}

$this->data['identifier'] = $this->data['route'] ?: (is_array($this->data['controller']) ? $this->data['controller']['class'].'::'.$this->data['controller']['method'].'()' : $this->data['controller']);

if ($response->headers->has('x-previous-debug-token')) {
$this->data['forward_token'] = $response->headers->get('x-previous-debug-token');
}
}

public function lateCollect()
Expand Down Expand Up @@ -322,6 +326,11 @@ public function getRedirect()
return isset($this->data['redirect']) ? $this->data['redirect'] : false;
}

public function getForwardToken()
{
return isset($this->data['forward_token']) ? $this->data['forward_token'] : null;
}

public function onKernelController(FilterControllerEvent $event)
{
$this->controllers[$event->getRequest()] = $event->getController();
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/HttpKernel/Profiler/Profile.php
Expand Up @@ -216,6 +216,17 @@ public function addChild(Profile $child)
$child->setParent($this);
}

public function getChildByToken(string $token): ?self
{
foreach ($this->children as $child) {
if ($token === $child->getToken()) {
return $child;
}
}

return null;
}

/**
* Gets a Collector by name.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpKernel/Profiler/Profiler.php
Expand Up @@ -156,6 +156,10 @@ public function collect(Request $request, Response $response, \Exception $except
$profile->setIp('Unknown');
}

if ($prevToken = $response->headers->get('X-Debug-Token')) {
$response->headers->set('X-Previous-Debug-Token', $prevToken);
}

$response->headers->set('X-Debug-Token', $profile->getToken());

foreach ($this->collectors as $collector) {
Expand Down

0 comments on commit 07dd09d

Please sign in to comment.