[HttpKernel] added logging when an inline fragment cannot be rendered and ignore_errors is on

This commit is contained in:
Fabien Potencier 2013-04-22 15:39:53 +02:00
parent 4cbf3a1c97
commit 7ef73b14c6
3 changed files with 24 additions and 3 deletions

View File

@ -22,6 +22,7 @@
<service id="fragment.renderer.inline" class="%fragment.renderer.inline.class%">
<tag name="kernel.fragment_renderer" />
<argument type="service" id="http_kernel" />
<argument type="service" id="event_dispatcher" />
<call method="setFragmentPath"><argument>%fragment.path%</argument></call>
</service>

View File

@ -15,6 +15,9 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Implements the inline rendering strategy where the Request is rendered by the current HTTP kernel.
@ -24,15 +27,17 @@ use Symfony\Component\HttpKernel\Controller\ControllerReference;
class InlineFragmentRenderer extends RoutableFragmentRenderer
{
private $kernel;
private $dispatcher;
/**
* Constructor.
*
* @param HttpKernelInterface $kernel A HttpKernelInterface instance
*/
public function __construct(HttpKernelInterface $kernel)
public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
{
$this->kernel = $kernel;
$this->dispatcher = $dispatcher;
}
/**
@ -61,6 +66,14 @@ class InlineFragmentRenderer extends RoutableFragmentRenderer
try {
return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
} catch (\Exception $e) {
// we dispatch the exception event to trigger the logging
// the response that comes back is simply ignored
if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
$event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
$this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
}
// let's clean up the output buffers that were created by the sub-request
while (ob_get_level() > $level) {
ob_get_clean();

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\HttpKernel\Fragment\Tests\FragmentRenderer;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\EventDispatcher\EventDispatcher;
@ -75,14 +76,20 @@ class InlineFragmentRendererTest extends \PHPUnit_Framework_TestCase
*/
public function testRenderExceptionNoIgnoreErrors()
{
$strategy = new InlineFragmentRenderer($this->getKernel($this->throwException(new \RuntimeException('foo'))));
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$dispatcher->expects($this->never())->method('dispatch');
$strategy = new InlineFragmentRenderer($this->getKernel($this->throwException(new \RuntimeException('foo'))), $dispatcher);
$this->assertEquals('foo', $strategy->render('/', Request::create('/'))->getContent());
}
public function testRenderExceptionIgnoreErrors()
{
$strategy = new InlineFragmentRenderer($this->getKernel($this->throwException(new \RuntimeException('foo'))));
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$dispatcher->expects($this->once())->method('dispatch')->with(KernelEvents::EXCEPTION);
$strategy = new InlineFragmentRenderer($this->getKernel($this->throwException(new \RuntimeException('foo'))), $dispatcher);
$this->assertEmpty($strategy->render('/', Request::create('/'), array('ignore_errors' => true))->getContent());
}