8000 bug #34331 [TwigBundle] Restore the preview mechanism (yceruto) · symfony/symfony@f63976f · GitHub
[go: up one dir, main page]

Skip to content

Commit f63976f

Browse files
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 #34312 Commits ------- d3f1121 [TwigBundle] Restore the preview mechanism
2 parents 92c81b7 + d3f1121 commit f63976f

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/Symfony/Bridge/Twig/ErrorRenderer/TwigErrorRenderer.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
1515
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
1616
use Symfony\Component\ErrorHandler\Exception\FlattenException;
17+
use Symfony\Component\HttpFoundation\RequestStack;
1718
use Twig\Environment;
1819
use Twig\Error\LoaderError;
1920
use Twig\Loader\ExistsLoaderInterface;
@@ -30,8 +31,15 @@ class TwigErrorRenderer implements ErrorRendererInterface
3031
private $fallbackErrorRenderer;
3132
private $debug;
3233

33-
public function __construct(Environment $twig, HtmlErrorRenderer $fallbackErrorRenderer = null, bool $debug = false)
34+
/**
35+
* @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
36+
*/
37+
public function __construct(Environment $twig, HtmlErrorRenderer $fallbackErrorRenderer = null, $debug = false)
3438
{
39+
if (!\is_bool($debug) && !\is_callable($debug)) {
40+
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)));
41+
}
42+
3543
$this->twig = $twig;
3644
$this->fallbackErrorRenderer = $fallbackErrorRenderer ?? new HtmlErrorRenderer();
3745
$this->debug = $debug;
@@ -43,8 +51,9 @@ public function __construct(Environment $twig, HtmlErrorRenderer $fallbackErrorR
4351
public function render(\Throwable $exception): FlattenException
4452
{
4553
$exception = $this->fallbackErrorRenderer->render($exception);
54+
$debug = \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
4655

47-
if ($this->debug || !$template = $this->findTemplate($exception->getStatusCode())) {
56+
if ($debug || !$template = $this->findTemplate($exception->getStatusCode())) {
4857
return $exception;
4958
}
5059

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

68+
public static function isDebug(RequestStack $requestStack, bool $debug): \Closure
69+
{
70+
return static function () use ($requestStack, $debug): bool {
71+
if (!$request = $requestStack->getCurrentRequest()) {
72+
return $debug;
73+
}
74+
75+
return $debug && $request->attributes->getBoolean('showException', true);
76+
};
77+
}
78+
5979
private function findTemplate(int $statusCode): ?string
6080
{
6181
$template = sprintf('@Twig/Exception/error%s.html.twig', $statusCode);

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,13 @@
165165
<service id="twig.error_renderer.html" class="Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer" decorates="error_renderer.html">
166166
<argument type="service" id="twig" />
167167
<argument type="service" id="twig.error_renderer.html.inner" />
168-
<argument>%kernel.debug%</argument>
168+
<argument type="service">
169+
<service>
170+
<factory class="Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer" method="isDebug" />
171+
<argument type="service" id="request_stack" />
172+
<argument>%kernel.debug%</argument>
173+
</service>
174+
</argument>
169175
</service>
170176
</services>
171177
</container>

src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ public function testCustomExceptionHandler()
550550
$handler->handleException(new \Exception());
551551
}
552552

553-
public function testSendPhpResponse()
553+
public function testRenderException()
554554
{
555555
$handler = new ErrorHandler();
556556
$handler->setExceptionHandler([$handler, 'renderException']);

0 commit comments

Comments
 (0)
0