merged branch fabpot/inline-fragment-logging (PR #7799)
This PR was merged into the master branch.
Discussion
----------
[HttpKernel] added logging when an inline fragment cannot be rendered and ignore_errors is on
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #7339, #1991, #6400
| License | MIT
| Doc PR | n/a
This is a rebased and tested version of #7339. This is probably not the most beautiful code, but it gets the job done and does the exact same thing as any other exception handling.
Commits
-------
7ef73b1
[HttpKernel] added logging when an inline fragment cannot be rendered and ignore_errors is on
This commit is contained in:
commit
13149c1c23
@ -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>
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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());
|
||||
}
|
||||
|
Reference in New Issue
Block a user