Skip to content

Commit

Permalink
[WebProfiler] fixed WDT display
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Oct 26, 2010
1 parent 988722b commit e111652
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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>"),
);
}
}
19 changes: 15 additions & 4 deletions src/Symfony/Bundle/WebProfilerBundle/WebDebugToolbarListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function handle(Event $event, Response $response)
return $response;
}

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

return $response;
}
Expand All @@ -86,12 +86,23 @@ public function handle(Event $event, Response $response)
*/
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);
}
}

0 comments on commit e111652

Please sign in to comment.