bug #34331 [TwigBundle] Restore the preview mechanism (yceruto)
This PR was merged into the 4.4 branch.
Discussion
----------
[TwigBundle] Restore the preview mechanism
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | no
| New feature? | no
| Deprecations? | no
| Tickets | -
| License | MIT
| Doc PR | -
follow up https://github.com/symfony/symfony/pull/34312
Commits
-------
d3f1121885
[TwigBundle] Restore the preview mechanism
This commit is contained in:
commit
f63976fcbb
@ -14,6 +14,7 @@ namespace Symfony\Bridge\Twig\ErrorRenderer;
|
|||||||
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
|
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
|
||||||
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
|
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
|
||||||
use Symfony\Component\ErrorHandler\Exception\FlattenException;
|
use Symfony\Component\ErrorHandler\Exception\FlattenException;
|
||||||
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
use Twig\Environment;
|
use Twig\Environment;
|
||||||
use Twig\Error\LoaderError;
|
use Twig\Error\LoaderError;
|
||||||
use Twig\Loader\ExistsLoaderInterface;
|
use Twig\Loader\ExistsLoaderInterface;
|
||||||
@ -30,8 +31,15 @@ class TwigErrorRenderer implements ErrorRendererInterface
|
|||||||
private $fallbackErrorRenderer;
|
private $fallbackErrorRenderer;
|
||||||
private $debug;
|
private $debug;
|
||||||
|
|
||||||
public function __construct(Environment $twig, HtmlErrorRenderer $fallbackErrorRenderer = null, bool $debug = false)
|
/**
|
||||||
|
* @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
|
||||||
|
*/
|
||||||
|
public function __construct(Environment $twig, HtmlErrorRenderer $fallbackErrorRenderer = null, $debug = false)
|
||||||
{
|
{
|
||||||
|
if (!\is_bool($debug) && !\is_callable($debug)) {
|
||||||
|
throw new \TypeError(sprintf('Argument 2 passed to %s() must be a boolean or a callable, %s given.', __METHOD__, \is_object($debug) ? \get_class($debug) : \gettype($debug)));
|
||||||
|
}
|
||||||
|
|
||||||
$this->twig = $twig;
|
$this->twig = $twig;
|
||||||
$this->fallbackErrorRenderer = $fallbackErrorRenderer ?? new HtmlErrorRenderer();
|
$this->fallbackErrorRenderer = $fallbackErrorRenderer ?? new HtmlErrorRenderer();
|
||||||
$this->debug = $debug;
|
$this->debug = $debug;
|
||||||
@ -43,8 +51,9 @@ class TwigErrorRenderer implements ErrorRendererInterface
|
|||||||
public function render(\Throwable $exception): FlattenException
|
public function render(\Throwable $exception): FlattenException
|
||||||
{
|
{
|
||||||
$exception = $this->fallbackErrorRenderer->render($exception);
|
$exception = $this->fallbackErrorRenderer->render($exception);
|
||||||
|
$debug = \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
|
||||||
|
|
||||||
if ($this->debug || !$template = $this->findTemplate($exception->getStatusCode())) {
|
if ($debug || !$template = $this->findTemplate($exception->getStatusCode())) {
|
||||||
return $exception;
|
return $exception;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,6 +65,17 @@ class TwigErrorRenderer implements ErrorRendererInterface
|
|||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function isDebug(RequestStack $requestStack, bool $debug): \Closure
|
||||||
|
{
|
||||||
|
return static function () use ($requestStack, $debug): bool {
|
||||||
|
if (!$request = $requestStack->getCurrentRequest()) {
|
||||||
|
return $debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $debug && $request->attributes->getBoolean('showException', true);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private function findTemplate(int $statusCode): ?string
|
private function findTemplate(int $statusCode): ?string
|
||||||
{
|
{
|
||||||
$template = sprintf('@Twig/Exception/error%s.html.twig', $statusCode);
|
$template = sprintf('@Twig/Exception/error%s.html.twig', $statusCode);
|
||||||
|
@ -165,7 +165,13 @@
|
|||||||
<service id="twig.error_renderer.html" class="Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer" decorates="error_renderer.html">
|
<service id="twig.error_renderer.html" class="Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer" decorates="error_renderer.html">
|
||||||
<argument type="service" id="twig" />
|
<argument type="service" id="twig" />
|
||||||
<argument type="service" id="twig.error_renderer.html.inner" />
|
<argument type="service" id="twig.error_renderer.html.inner" />
|
||||||
|
<argument type="service">
|
||||||
|
<service>
|
||||||
|
<factory class="Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer" method="isDebug" />
|
||||||
|
<argument type="service" id="request_stack" />
|
||||||
<argument>%kernel.debug%</argument>
|
<argument>%kernel.debug%</argument>
|
||||||
</service>
|
</service>
|
||||||
|
</argument>
|
||||||
|
</service>
|
||||||
</services>
|
</services>
|
||||||
</container>
|
</container>
|
||||||
|
@ -550,7 +550,7 @@ class ErrorHandlerTest extends TestCase
|
|||||||
$handler->handleException(new \Exception());
|
$handler->handleException(new \Exception());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSendPhpResponse()
|
public function testRenderException()
|
||||||
{
|
{
|
||||||
$handler = new ErrorHandler();
|
$handler = new ErrorHandler();
|
||||||
$handler->setExceptionHandler([$handler, 'renderException']);
|
$handler->setExceptionHandler([$handler, 'renderException']);
|
||||||
|
Reference in New Issue
Block a user