Bladeren bron

[WebProfiler] fixed WDT display

Fabien Potencier 14 jaren geleden
bovenliggende
commit
e1116524ed

+ 60 - 0
src/Symfony/Bundle/WebProfilerBundle/Tests/WebDebugToolbarListenerTest.php

@@ -0,0 +1,60 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\WebProfilerBundle\Tests;
+
+use Symfony\Bundle\WebProfilerBundle\WebDebugToolbarListener;
+use Symfony\Component\HttpFoundation\Response;
+
+class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @dataProvider getInjectToolbarTests
+     */
+    public function testInjectToolbar($content, $expected)
+    {
+        $resolver = $this->getMock('Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver', array(), array(), '', false);
+        $resolver->expects($this->any())
+                 ->method('render')
+                 ->will($this->returnValue('WDT'));
+        ;
+        $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
+        $listener = new WebDebugToolbarListener($resolver);
+        $r = new \ReflectionObject($listener);
+        $m = $r->getMethod('injectToolbar');
+        $m->setAccessible(true);
+
+        $response = new Response($content);
+
+        $m->invoke($listener, $request, $response);
+        $this->assertEquals($expected, $response->getContent());
+    }
+
+    public function getInjectToolbarTests()
+    {
+        return array(
+            array('<html><head></head><body></body></html>', "<html><head></head><body>\nWDT\n</body></html>"),
+            array('<html>
+            <head></head>
+            <body>
+            <textarea><html><head></head><body></body></html></textarea>
+            </body>
+            </html>', "<html>
+            <head></head>
+            <body>
+            <textarea><html><head></head><body></body></html></textarea>
+            
+WDT
+</body>
+            </html>"),
+        );
+    }
+}

+ 15 - 4
src/Symfony/Bundle/WebProfilerBundle/WebDebugToolbarListener.php

@@ -72,7 +72,7 @@ class WebDebugToolbarListener
             return $response;
         }
 
-        $response->setContent($this->injectToolbar($request, $response));
+        $this->injectToolbar($request, $response);
 
         return $response;
     }
@@ -86,12 +86,23 @@ class WebDebugToolbarListener
      */
     protected function injectToolbar(Request $request, Response $response)
     {
+        if (function_exists('mb_stripos')) {
+            $posrFunction = 'mb_strripos';
+            $substrFunction = 'mb_substr';
+        } else {
+            $posrFunction = 'strripos';
+            $substrFunction = 'substr';
+        }
+
         $toolbar = "\n".str_replace("\n", '', $this->resolver->render('WebProfilerBundle:Profiler:toolbar'))."\n";
-        $content = str_ireplace('</body>', $toolbar.'</body>', $response->getContent(), $count);
-        if (!$count) {
+        $content = $response->getContent();
+
+        if (false === $pos = $posrFunction($content, '</body>')) {
             $content .= $toolbar;
+        } else {
+            $content = $substrFunction($content, 0, $pos).$toolbar.$substrFunction($content, $pos);
         }
 
-        return $content;
+        $response->setContent($content);
     }
 }