1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace Shoot\Shoot\Twig\Node; |
||
5 | |||
6 | use Shoot\Shoot\Extension; |
||
7 | use Shoot\Shoot\SuppressedException; |
||
8 | use Shoot\Shoot\View; |
||
9 | use Twig\Compiler; |
||
10 | use Twig\Node\ModuleNode; |
||
11 | use Twig\Node\Node; |
||
12 | use Twig\Source; |
||
13 | |||
14 | /** |
||
15 | * This node is added to the bottom of the display method of a Twig template and is used by Shoot to wrap its contents |
||
16 | * in a callback. |
||
17 | * |
||
18 | * @internal |
||
19 | */ |
||
20 | final class DisplayEndNode extends Node |
||
21 | { |
||
22 | /** @var ModuleNode */ |
||
23 | private $module; |
||
24 | |||
25 | /** |
||
26 | * @param ModuleNode $module |
||
27 | */ |
||
28 | 6 | public function __construct(ModuleNode $module) |
|
29 | { |
||
30 | 6 | parent::__construct(); |
|
31 | |||
32 | 6 | $this->module = $module; |
|
33 | |||
34 | 6 | $this->setSourceContext($module->getSourceContext()); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
35 | 6 | } |
|
36 | |||
37 | /** |
||
38 | * @param Compiler $compiler |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | 6 | public function compile(Compiler $compiler): void |
|
43 | { |
||
44 | 6 | if ($this->module->hasAttribute('is_embedded')) { |
|
45 | 1 | return; |
|
46 | } |
||
47 | |||
48 | /** @var Source $source */ |
||
49 | 6 | $source = $this->getSourceContext(); |
|
50 | 6 | $templateName = $source->getName(); |
|
51 | |||
52 | 6 | $extensionClass = Extension::class; |
|
53 | 6 | $suppressedExceptionClass = SuppressedException::class; |
|
54 | 6 | $viewClass = View::class; |
|
55 | |||
56 | $compiler |
||
57 | 6 | ->raw("\n") |
|
58 | 6 | ->write("if (\$suppressedException instanceof {$suppressedExceptionClass}) {\n") |
|
59 | 6 | ->indent() |
|
60 | 6 | ->write("throw \$suppressedException;\n") |
|
61 | 6 | ->outdent() |
|
62 | 6 | ->write("}\n") |
|
63 | 6 | ->outdent() |
|
64 | 6 | ->write("};\n\n") |
|
65 | 6 | ->write("\$this->env\n") |
|
66 | 6 | ->indent() |
|
67 | 6 | ->write("->getExtension({$extensionClass}::class)\n") |
|
68 | 6 | ->write("->process(new {$viewClass}('{$templateName}', \$presentationModel, \$callback));\n") |
|
69 | 6 | ->outdent(); |
|
70 | 6 | } |
|
71 | } |
||
72 |