[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%"> <service id="fragment.renderer.inline" class="%fragment.renderer.inline.class%">
<tag name="kernel.fragment_renderer" /> <tag name="kernel.fragment_renderer" />
<argument type="service" id="http_kernel" /> <argument type="service" id="http_kernel" />
<argument type="service" id="event_dispatcher" />
<call method="setFragmentPath"><argument>%fragment.path%</argument></call> <call method="setFragmentPath"><argument>%fragment.path%</argument></call>
</service> </service>

View File

@ -15,6 +15,9 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Controller\ControllerReference; 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. * 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 class InlineFragmentRenderer extends RoutableFragmentRenderer
{ {
private $kernel; private $kernel;
private $dispatcher;
/** /**
* Constructor. * Constructor.
* *
* @param HttpKernelInterface $kernel A HttpKernelInterface instance * @param HttpKernelInterface $kernel A HttpKernelInterface instance
*/ */
public function __construct(HttpKernelInterface $kernel) public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
{ {
$this->kernel = $kernel; $this->kernel = $kernel;
$this->dispatcher = $dispatcher;
} }
/** /**
@ -61,6 +66,14 @@ class InlineFragmentRenderer extends RoutableFragmentRenderer
try { try {
return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false); return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
} catch (\Exception $e) { } 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 // let's clean up the output buffers that were created by the sub-request
while (ob_get_level() > $level) { while (ob_get_level() > $level) {
ob_get_clean(); 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\Controller\ControllerReference;
use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer; use Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
@ -75,14 +76,20 @@ class InlineFragmentRendererTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRenderExceptionNoIgnoreErrors() 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()); $this->assertEquals('foo', $strategy->render('/', Request::create('/'))->getContent());
} }
public function testRenderExceptionIgnoreErrors() 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()); $this->assertEmpty($strategy->render('/', Request::create('/'), array('ignore_errors' => true))->getContent());
} }