14
14
use Symfony \Component \ErrorHandler \ErrorRenderer \ErrorRendererInterface ;
15
15
use Symfony \Component \ErrorHandler \ErrorRenderer \HtmlErrorRenderer ;
16
16
use Symfony \Component \ErrorHandler \Exception \FlattenException ;
17
+ use Symfony \Component \HttpFoundation \RequestStack ;
17
18
use Twig \Environment ;
18
19
use Twig \Error \LoaderError ;
19
20
use Twig \Loader \ExistsLoaderInterface ;
@@ -30,8 +31,15 @@ class TwigErrorRenderer implements ErrorRendererInterface
30
31
private $ fallbackErrorRenderer ;
31
32
private $ debug ;
32
33
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 )
34
38
{
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
+
35
43
$ this ->twig = $ twig ;
36
44
$ this ->fallbackErrorRenderer = $ fallbackErrorRenderer ?? new HtmlErrorRenderer ();
37
45
$ this ->debug = $ debug ;
@@ -43,8 +51,9 @@ public function __construct(Environment $twig, HtmlErrorRenderer $fallbackErrorR
43
51
public function render (\Throwable $ exception ): FlattenException
44
52
{
45
53
$ exception = $ this ->fallbackErrorRenderer ->render ($ exception );
54
+ $ debug = \is_bool ($ this ->debug ) ? $ this ->debug : ($ this ->debug )($ exception );
46
55
47
- if ($ this -> debug || !$ template = $ this ->findTemplate ($ exception ->getStatusCode ())) {
56
+ if ($ debug || !$ template = $ this ->findTemplate ($ exception ->getStatusCode ())) {
48
57
return $ exception ;
49
58
}
50
59
@@ -56,6 +65,17 @@ public function render(\Throwable $exception): FlattenException
56
65
]));
57
66
}
58
67
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
+
59
79
private function findTemplate (int $ statusCode ): ?string
60
80
{
61
81
$ template = sprintf ('@Twig/Exception/error%s.html.twig ' , $ statusCode );
0 commit comments