Skip to content

Commit e111652

Browse files
committed
[WebProfiler] fixed WDT display
1 parent 988722b commit e111652

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\WebProfilerBundle\Tests;
13+
14+
use Symfony\Bundle\WebProfilerBundle\WebDebugToolbarListener;
15+
use Symfony\Component\HttpFoundation\Response;
16+
17+
class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @dataProvider getInjectToolbarTests
21+
*/
22+
public function testInjectToolbar($content, $expected)
23+
{
24+
$resolver = $this->getMock('Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver', array(), array(), '', false);
25+
$resolver->expects($this->any())
26+
->method('render')
27+
->will($this->returnValue('WDT'));
28+
;
29+
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
30+
$listener = new WebDebugToolbarListener($resolver);
31+
$r = new \ReflectionObject($listener);
32+
$m = $r->getMethod('injectToolbar');
33+
$m->setAccessible(true);
34+
35+
$response = new Response($content);
36+
37+
$m->invoke($listener, $request, $response);
38+
$this->assertEquals($expected, $response->getContent());
39+
}
40+
41+
public function getInjectToolbarTests()
42+
{
43+
return array(
44+
array('<html><head></head><body></body></html>', "<html><head></head><body>\nWDT\n</body></html>"),
45+
array('<html>
46+
<head></head>
47+
<body>
48+
<textarea><html><head></head><body></body></html></textarea>
49+
</body>
50+
</html>', "<html>
51+
<head></head>
52+
<body>
53+
<textarea><html><head></head><body></body></html></textarea>
54+
55+
WDT
56+
</body>
57+
</html>"),
58+
);
59+
}
60+
}

src/Symfony/Bundle/WebProfilerBundle/WebDebugToolbarListener.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function handle(Event $event, Response $response)
7272
return $response;
7373
}
7474

75-
$response->setContent($this->injectToolbar($request, $response));
75+
$this->injectToolbar($request, $response);
7676

7777
return $response;
7878
}
@@ -86,12 +86,23 @@ public function handle(Event $event, Response $response)
8686
*/
8787
protected function injectToolbar(Request $request, Response $response)
8888
{
89+
if (function_exists('mb_stripos')) {
90+
$posrFunction = 'mb_strripos';
91+
$substrFunction = 'mb_substr';
92+
} else {
93+
$posrFunction = 'strripos';
94+
$substrFunction = 'substr';
95+
}
96+
8997
$toolbar = "\n".str_replace("\n", '', $this->resolver->render('WebProfilerBundle:Profiler:toolbar'))."\n";
90-
$content = str_ireplace('</body>', $toolbar.'</body>', $response->getContent(), $count);
91-
if (!$count) {
98+
$content = $response->getContent();
99+
100+
if (false === $pos = $posrFunction($content, '</body>')) {
92101
$content .= $toolbar;
102+
} else {
103+
$content = $substrFunction($content, 0, $pos).$toolbar.$substrFunction($content, $pos);
93104
}
94105

95-
return $content;
106+
$response->setContent($content);
96107
}
97108
}

0 commit comments

Comments
 (0)