feature #9342 Add X-Debug-Url profiler url header (adrienbrault)

This PR was merged into the master branch.

Discussion
----------

Add X-Debug-Url profiler url header

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | None

Hey, working with rest APIs I find it way more convenient to have the profiler url directly in the header ... We human are used to deal with hypermedia!
A little bit of hateoas in symfony :)

Commits
-------

2f09754 Add X-Debug-Url profiler url header
This commit is contained in:
Fabien Potencier 2013-10-31 14:30:36 +01:00
commit f8e88bc7f9
3 changed files with 41 additions and 4 deletions

View File

@ -16,6 +16,7 @@ use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* WebDebugToolbarListener injects the Web Debug Toolbar.
@ -33,13 +34,15 @@ class WebDebugToolbarListener implements EventSubscriberInterface
const ENABLED = 2;
protected $twig;
protected $urlGenerator;
protected $interceptRedirects;
protected $mode;
protected $position;
public function __construct(\Twig_Environment $twig, $interceptRedirects = false, $mode = self::ENABLED, $position = 'bottom')
public function __construct(\Twig_Environment $twig, $interceptRedirects = false, $mode = self::ENABLED, $position = 'bottom', UrlGeneratorInterface $urlGenerator = null)
{
$this->twig = $twig;
$this->urlGenerator = $urlGenerator;
$this->interceptRedirects = (Boolean) $interceptRedirects;
$this->mode = (integer) $mode;
$this->position = $position;
@ -52,13 +55,20 @@ class WebDebugToolbarListener implements EventSubscriberInterface
public function onKernelResponse(FilterResponseEvent $event)
{
$response = $event->getResponse();
$request = $event->getRequest();
if ($response->headers->has('X-Debug-Token') && null !== $this->urlGenerator) {
$response->headers->set(
'X-Debug-Token-Link',
$this->urlGenerator->generate('_profiler', array('token' => $response->headers->get('X-Debug-Token')))
);
}
if (!$event->isMasterRequest()) {
return;
}
$response = $event->getResponse();
$request = $event->getRequest();
// do not capture redirects or modify XML HTTP Requests
if ($request->isXmlHttpRequest()) {
return;

View File

@ -15,6 +15,7 @@
<argument>%web_profiler.debug_toolbar.intercept_redirects%</argument>
<argument>%web_profiler.debug_toolbar.mode%</argument>
<argument>%web_profiler.debug_toolbar.position%</argument>
<argument type="service" id="router" />
</service>
</services>
</container>

View File

@ -185,6 +185,27 @@ class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
}
public function testXDebugUrlHeader()
{
$response = new Response();
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
$urlGenerator = $this->getUrlGeneratorMock();
$urlGenerator
->expects($this->once())
->method('generate')
->with('_profiler', array('token' => 'xxxxxxxx'))
->will($this->returnValue('/_profiler/xxxxxxxx'))
;
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, 'bottom', $urlGenerator);
$listener->onKernelResponse($event);
$this->assertEquals('/_profiler/xxxxxxxx', $response->headers->get('X-Debug-Token-Link'));
}
protected function getRequestMock($isXmlHttpRequest = false, $requestFormat = 'html', $hasSession = true)
{
$request = $this->getMock(
@ -219,6 +240,11 @@ class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
return $templating;
}
protected function getUrlGeneratorMock()
{
return $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
}
protected function getKernelMock()
{
return $this->getMock('Symfony\Component\HttpKernel\Kernel', array(), array(), '', false);