소스 검색

[WebProfilerBundle] made the WDT less intruisive by moving it to its own Ajax request

Fabien Potencier 14 년 전
부모
커밋
28bf834c0c

+ 10 - 0
src/Symfony/Bundle/WebProfilerBundle/Resources/config/routing/wdt.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<routes xmlns="http://www.symfony-project.org/schema/routing"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://www.symfony-project.org/schema/routing http://www.symfony-project.org/schema/routing/routing-1.0.xsd">
+
+    <route id="_wdt" pattern="/{token}">
+        <default key="_controller">WebProfilerBundle:Profiler:toolbar</default>
+    </route>
+</routes>

+ 1 - 0
src/Symfony/Bundle/WebProfilerBundle/Resources/config/toolbar.xml

@@ -13,6 +13,7 @@
         <service id="debug.toolbar" class="%debug.toolbar.class%">
             <tag name="kernel.listener" event="core.response" method="handle" priority="-128" />
             <argument type="service" id="http_kernel" />
+            <argument type="service" id="templating.engine.twig" />
             <argument>%debug.toolbar.intercept_redirects%</argument>
         </service>
     </services>

+ 13 - 0
src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar_js.html.twig

@@ -0,0 +1,13 @@
+<div id="sfwdt{{ token }}" style="display: none"></div>
+<script type="text/javascript">
+    wdt = document.getElementById('sfwdt{{ token }}');
+    if (window.XMLHttpRequest) {
+        xhr = new XMLHttpRequest();
+    } else {
+        xhr = new ActiveXObject('Microsoft.XMLHTTP');
+    }
+    xhr.open('GET', '{{ url("_wdt", { "token": token }) }}', false);
+    xhr.send('');
+    wdt.innerHTML = xhr.responseText;
+    wdt.style.display = 'block';
+</script>

+ 4 - 3
src/Symfony/Bundle/WebProfilerBundle/Tests/WebDebugToolbarListenerTest.php

@@ -21,13 +21,14 @@ class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
      */
     public function testInjectToolbar($content, $expected)
     {
-        $resolver = $this->getMock('Symfony\Bundle\FrameworkBundle\HttpKernel', array(), array(), '', false);
-        $resolver->expects($this->any())
+        $kernel = $this->getMock('Symfony\Bundle\FrameworkBundle\HttpKernel', array(), array(), '', false);
+        $templating = $this->getMock('Symfony\Bundle\TwigBundle\TwigEngine', array(), array(), '', false);
+        $templating->expects($this->any())
                  ->method('render')
                  ->will($this->returnValue('WDT'));
         ;
         $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
-        $listener = new WebDebugToolbarListener($resolver);
+        $listener = new WebDebugToolbarListener($kernel, $templating);
         $m = new \ReflectionMethod($listener, 'injectToolbar');
         $m->setAccessible(true);
 

+ 5 - 2
src/Symfony/Bundle/WebProfilerBundle/WebDebugToolbarListener.php

@@ -16,6 +16,7 @@ use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Bundle\FrameworkBundle\HttpKernel;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
+use Symfony\Bundle\TwigBundle\TwigEngine;
 
 /**
  * WebDebugToolbarListener injects the Web Debug Toolbar.
@@ -30,11 +31,13 @@ use Symfony\Component\HttpKernel\HttpKernelInterface;
 class WebDebugToolbarListener
 {
     protected $kernel;
+    protected $templating;
     protected $interceptRedirects;
 
-    public function __construct(HttpKernel $kernel, $interceptRedirects = false)
+    public function __construct(HttpKernel $kernel, TwigEngine $templating, $interceptRedirects = false)
     {
         $this->kernel = $kernel;
+        $this->templating = $templating;
         $this->interceptRedirects = $interceptRedirects;
     }
 
@@ -83,7 +86,7 @@ class WebDebugToolbarListener
             $substrFunction = 'substr';
         }
 
-        $toolbar = "\n".str_replace("\n", '', $this->kernel->render('WebProfilerBundle:Profiler:toolbar', array('attributes' => array('token' => $response->headers->get('X-Debug-Token')))))."\n";
+        $toolbar = "\n".str_replace("\n", '', $this->templating->render('WebProfilerBundle:Profiler:toolbar_js.html.twig', array('token' => $response->headers->get('X-Debug-Token'))))."\n";
         $content = $response->getContent();
 
         if (false !== $pos = $posrFunction($content, '</body>')) {