8000 [TwigBundle] Restore the preview mechanism by yceruto · Pull Request #34331 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBundle] Restore the preview mechanism #34331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file 8000
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/Symfony/Bridge/Twig/ErrorRenderer/TwigErrorRenderer.php
Original file line number Diff line number Diff line change
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)) {
8000 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
Original file line number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
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