[WebProfilerBundle] Imply forward request by a new X-Previous-Debug-Token header

This commit is contained in:
Roland Franssen 2017-04-15 15:18:24 +02:00 committed by Fabien Potencier
parent 65b2bcde1b
commit 07dd09db59
13 changed files with 52 additions and 72 deletions

View File

@ -16,6 +16,7 @@ FrameworkBundle
--------------- ---------------
* A `RouterInterface` that does not implement the `WarmableInterface` is deprecated and will not be supported in Symfony 5.0. * 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 HttpFoundation
-------------- --------------

View File

@ -15,6 +15,7 @@ FrameworkBundle
--------------- ---------------
* Using a `RouterInterface` that does not implement the `WarmableInterface` is not supported anymore. * 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 HttpFoundation
-------------- --------------

View File

@ -9,6 +9,7 @@ CHANGELOG
* Allowed the `Router` to work with any PSR-11 container * Allowed the `Router` to work with any PSR-11 container
* Added option in workflow dump command to label graph with a custom label * 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. * 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 4.0.0
----- -----

View File

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

View File

@ -11,67 +11,17 @@
namespace Symfony\Bundle\FrameworkBundle\DataCollector; namespace Symfony\Bundle\FrameworkBundle\DataCollector;
use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector as BaseRequestDataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; @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);
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector as BaseRequestCollector;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/** /**
* RequestDataCollector. * RequestDataCollector.
* *
* @author Jules Pietri <jusles@heahprod.com> * @author Jules Pietri <jusles@heahprod.com>
*/
class RequestDataCollector extends BaseRequestCollector implements EventSubscriberInterface
{
/**
* {@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 * @deprecated since version 4.1, to be removed in Symfony 5.0
* 'controller' the parsed forward controller, false otherwise
*/ */
public function getForward() class RequestDataCollector extends BaseRequestDataCollector
{ {
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';
}
} }

View File

@ -12,7 +12,7 @@
<call method="setKernel"><argument type="service" id="kernel" on-invalid="ignore" /></call> <call method="setKernel"><argument type="service" id="kernel" on-invalid="ignore" /></call>
</service> </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="kernel.event_subscriber" />
<tag name="data_collector" template="@WebProfiler/Collector/request.html.twig" id="request" priority="335" /> <tag name="data_collector" template="@WebProfiler/Collector/request.html.twig" id="request" priority="335" />
</service> </service>

View File

@ -24,7 +24,6 @@ class SubRequestServiceResolutionController implements ContainerAwareInterface
public function indexAction() public function indexAction()
{ {
$request = $this->container->get('request_stack')->getCurrentRequest(); $request = $this->container->get('request_stack')->getCurrentRequest();
$path['_forwarded'] = $request->attributes;
$path['_controller'] = 'TestBundle:SubRequestServiceResolution:fragment'; $path['_controller'] = 'TestBundle:SubRequestServiceResolution:fragment';
$subRequest = $request->duplicate(array(), null, $path); $subRequest = $request->duplicate(array(), null, $path);

View File

@ -12,9 +12,10 @@
{% endset %} {% endset %}
{% endif %} {% endif %}
{% if collector.forward|default(false) %} {% if collector.forwardtoken %}
{% set forward_profile = profile.childByToken(collector.forwardtoken) %}
{% set forward_handler %} {% set forward_handler %}
{{ helper.set_handler(collector.forward.controller) }} {{ helper.set_handler(forward_profile ? forward_profile.collector('request').controller : 'n/a') }}
{% endset %} {% endset %}
{% endif %} {% endif %}
@ -24,7 +25,7 @@
<span class="sf-toolbar-status sf-toolbar-status-{{ request_status_code_color }}">{{ collector.statuscode }}</span> <span class="sf-toolbar-status sf-toolbar-status-{{ request_status_code_color }}">{{ collector.statuscode }}</span>
{% if collector.route %} {% if collector.route %}
{% if collector.redirect %}{{ include('@WebProfiler/Icon/redirect.svg') }}{% endif %} {% 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-label">{{ 'GET' != collector.method ? collector.method }} @</span>
<span class="sf-toolbar-value sf-toolbar-info-piece-additional">{{ collector.route }}</span> <span class="sf-toolbar-value sf-toolbar-info-piece-additional">{{ collector.route }}</span>
{% endif %} {% endif %}
@ -81,7 +82,7 @@
<b>Forwarded to</b> <b>Forwarded to</b>
<span> <span>
{{ forward_handler }} {{ 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> </span>
</div> </div>
</div> </div>

View File

@ -44,18 +44,22 @@
</dl> </dl>
{%- endif %} {%- endif %}
{% if request_collector and request_collector.forward|default(false) and request_collector.forward.controller.class is defined -%} {% if request_collector and request_collector.forwardtoken -%}
{%- set forward = request_collector.forward -%} {% set forward_profile = profile.childByToken(request_collector.forwardtoken) %}
{%- set controller = forward.controller -%} {% set controller = forward_profile ? forward_profile.collector('request').controller : 'n/a' %}
<dl class="metadata"> <dl class="metadata">
<dt>Forwarded to</dt> <dt>Forwarded to</dt>
<dd> <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 -%} {%- if link %}<a href="{{ link }}" title="{{ controller.file }}">{% endif -%}
{% if controller.class is defined %}
{{- controller.class|abbr_class|striptags -}} {{- controller.class|abbr_class|striptags -}}
{{- controller.method ? ' :: ' ~ controller.method }} {{- controller.method ? ' :: ' ~ controller.method -}}
{% else %}
{{- controller -}}
{% endif %}
{%- if link %}</a>{% 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> </dd>
</dl> </dl>
{%- endif %} {%- endif %}

View File

@ -17,7 +17,7 @@
], ],
"require": { "require": {
"php": "^7.1.3", "php": "^7.1.3",
"symfony/http-kernel": "~3.4|~4.0", "symfony/http-kernel": "~4.1",
"symfony/routing": "~3.4|~4.0", "symfony/routing": "~3.4|~4.0",
"symfony/twig-bridge": "~3.4|~4.0", "symfony/twig-bridge": "~3.4|~4.0",
"symfony/var-dumper": "~3.4|~4.0", "symfony/var-dumper": "~3.4|~4.0",

View File

@ -158,6 +158,10 @@ class RequestDataCollector extends DataCollector implements EventSubscriberInter
} }
$this->data['identifier'] = $this->data['route'] ?: (is_array($this->data['controller']) ? $this->data['controller']['class'].'::'.$this->data['controller']['method'].'()' : $this->data['controller']); $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() public function lateCollect()
@ -322,6 +326,11 @@ class RequestDataCollector extends DataCollector implements EventSubscriberInter
return isset($this->data['redirect']) ? $this->data['redirect'] : false; 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) public function onKernelController(FilterControllerEvent $event)
{ {
$this->controllers[$event->getRequest()] = $event->getController(); $this->controllers[$event->getRequest()] = $event->getController();

View File

@ -216,6 +216,17 @@ class Profile
$child->setParent($this); $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. * Gets a Collector by name.
* *

View File

@ -156,6 +156,10 @@ class Profiler
$profile->setIp('Unknown'); $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()); $response->headers->set('X-Debug-Token', $profile->getToken());
foreach ($this->collectors as $collector) { foreach ($this->collectors as $collector) {