Skip to content

Commit

Permalink
bug #34331 [TwigBundle] Restore the preview mechanism (yceruto)
Browse files Browse the repository at this point in the history
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 #34312

Commits
-------

d3f1121 [TwigBundle] Restore the preview mechanism
  • Loading branch information
nicolas-grekas committed Nov 12, 2019
2 parents 92c81b7 + d3f1121 commit f63976f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
24 changes: 22 additions & 2 deletions src/Symfony/Bridge/Twig/ErrorRenderer/TwigErrorRenderer.php
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\ExistsLoaderInterface;
Expand All @@ -30,8 +31,15 @@ class TwigErrorRenderer implements ErrorRendererInterface
private $fallbackErrorRenderer;
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->fallbackErrorRenderer = $fallbackErrorRenderer ?? new HtmlErrorRenderer();
$this->debug = $debug;
Expand All @@ -43,8 +51,9 @@ public function __construct(Environment $twig, HtmlErrorRenderer $fallbackErrorR
public function render(\Throwable $exception): FlattenException
{
$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;
}

Expand All @@ -56,6 +65,17 @@ public function render(\Throwable $exception): FlattenException
]));
}

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
{
$template = sprintf('@Twig/Exception/error%s.html.twig', $statusCode);
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Expand Up @@ -165,7 +165,13 @@
<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.error_renderer.html.inner" />
<argument>%kernel.debug%</argument>
<argument type="service">
<service>
<factory class="Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer" method="isDebug" />
<argument type="service" id="request_stack" />
<argument>%kernel.debug%</argument>
</service>
</argument>
</service>
</services>
</container>
Expand Up @@ -550,7 +550,7 @@ public function testCustomExceptionHandler()
$handler->handleException(new \Exception());
}

public function testSendPhpResponse()
public function testRenderException()
{
$handler = new ErrorHandler();
$handler->setExceptionHandler([$handler, 'renderException']);
Expand Down

0 comments on commit f63976f

Please sign in to comment.