Procházet zdrojové kódy

fixed profiler when using ESI in dev env

Fabien Potencier před 14 roky
rodič
revize
dff3585162

+ 7 - 6
src/Symfony/Component/HttpKernel/Profiler/ProfilerListener.php

@@ -9,12 +9,13 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Component\HttpKernel\Profiler;
+namespace Symfony\Bundle\FrameworkBundle\Profiler;
 
 use Symfony\Component\EventDispatcher\EventInterface;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 use Symfony\Component\HttpFoundation\RequestMatcherInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * ProfilerListener collects data for the current request by listening to the core.response event.
@@ -26,7 +27,7 @@ use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  */
 class ProfilerListener
 {
-    protected $profiler;
+    protected $container;
     protected $exception;
     protected $onlyException;
     protected $matcher;
@@ -34,13 +35,13 @@ class ProfilerListener
     /**
      * Constructor.
      *
-     * @param Profiler                $profiler      A Profiler instance
+     * @param ContainerInterface      $container     A ContainerInterface instance
      * @param RequestMatcherInterface $matcher       A RequestMatcher instance
      * @param Boolean                 $onlyException true if the profiler only collects data when an exception occurs, false otherwise
      */
-    public function __construct(Profiler $profiler, RequestMatcherInterface $matcher = null, $onlyException = false)
+    public function __construct(ContainerInterface $container, RequestMatcherInterface $matcher = null, $onlyException = false)
     {
-        $this->profiler = $profiler;
+        $this->container = $container;
         $this->matcher = $matcher;
         $this->onlyException = $onlyException;
     }
@@ -82,7 +83,7 @@ class ProfilerListener
             return $response;
         }
 
-        $this->profiler->collect($event->get('request'), $response, $this->exception);
+        $this->container->get('profiler')->collect($event->get('request'), $response, $this->exception);
         $this->exception = null;
 
         return $response;

+ 3 - 3
src/Symfony/Bundle/FrameworkBundle/Resources/config/profiling.xml

@@ -9,12 +9,12 @@
         <parameter key="profiler.storage.class">Symfony\Component\HttpKernel\Profiler\SqliteProfilerStorage</parameter>
         <parameter key="profiler.storage.file">%kernel.cache_dir%/profiler.db</parameter>
         <parameter key="profiler.storage.lifetime">86400</parameter>
-        <parameter key="profiler_listener.class">Symfony\Component\HttpKernel\Profiler\ProfilerListener</parameter>
+        <parameter key="profiler_listener.class">Symfony\Bundle\FrameworkBundle\Profiler\ProfilerListener</parameter>
         <parameter key="profiler_listener.only_exceptions">false</parameter>
     </parameters>
 
     <services>
-        <service id="profiler" class="%profiler.class%">
+        <service id="profiler" class="%profiler.class%" scope="request">
             <argument type="service" id="profiler.storage" />
             <argument type="service" id="logger" on-invalid="null" />
         </service>
@@ -27,7 +27,7 @@
         <service id="profiler_listener" class="%profiler_listener.class%">
             <tag name="kernel.listener" event="core.response" method="handleResponse" />
             <tag name="kernel.listener" event="core.exception" method="handleException" />
-            <argument type="service" id="profiler" />
+            <argument type="service" id="service_container" />
             <argument type="service" id="profiler.request_matcher" on-invalid="null" />
             <argument>%profiler_listener.only_exceptions%</argument>
         </service>

+ 9 - 6
src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

@@ -130,16 +130,19 @@ class ProfilerController extends ContainerAware
      *
      * @return Response A Response instance
      */
-    public function toolbarAction($token = null, $position = null)
+    public function toolbarAction($token, $position = null)
     {
+        if (null === $token) {
+            return $this->container->get('response');
+        }
+
         $profiler = $this->container->get('profiler');
+        $profiler->disable();
 
-        if (null !== $token) {
-            $profiler = $profiler->loadFromToken($token);
+        $profiler = $profiler->loadFromToken($token);
 
-            if ($profiler->isEmpty()) {
-                return $this->container->get('response');
-            }
+        if ($profiler->isEmpty()) {
+            return $this->container->get('response');
         }
 
         if (null === $position) {

+ 1 - 1
src/Symfony/Bundle/WebProfilerBundle/Tests/WebDebugToolbarListenerTest.php

@@ -33,7 +33,7 @@ class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
 
         $response = new Response($content);
 
-        $m->invoke($listener, $request, $response);
+        $m->invoke($listener, $response);
         $this->assertEquals($expected, $response->getContent());
     }
 

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

@@ -60,19 +60,17 @@ class WebDebugToolbarListener
             return $response;
         }
 
-        $this->injectToolbar($request, $response);
+        $this->injectToolbar($response);
 
         return $response;
     }
 
     /**
-     * Injects the web debug toolbar into a given HTML string.
+     * Injects the web debug toolbar into the given Response.
      *
-     * @param string $content The HTML content
-     *
-     * @return Response A Response instance
+     * @param Response $response A Response instance
      */
-    protected function injectToolbar(Request $request, Response $response)
+    protected function injectToolbar(Response $response)
     {
         if (function_exists('mb_stripos')) {
             $posrFunction = 'mb_strripos';
@@ -82,7 +80,7 @@ class WebDebugToolbarListener
             $substrFunction = 'substr';
         }
 
-        $toolbar = "\n".str_replace("\n", '', $this->kernel->render('WebProfilerBundle:Profiler:toolbar'))."\n";
+        $toolbar = "\n".str_replace("\n", '', $this->kernel->render('WebProfilerBundle:Profiler:toolbar', array('attributes' => array('token' => $response->headers->get('X-Debug-Token')))))."\n";
         $content = $response->getContent();
 
         if (false === $pos = $posrFunction($content, '</body>')) {