|
9 | 9 | use Phug\CompilerEvent; |
10 | 10 | use Phug\Formatter\Element\KeywordElement; |
11 | 11 | use Phug\Formatter\Element\MixinElement; |
| 12 | +use Phug\Parser\NodeInterface as ParserNodeInterface; |
12 | 13 | use Phug\Parser\Node\CodeNode; |
13 | 14 | use Phug\Parser\Node\KeywordNode; |
14 | 15 | use Phug\Parser\Node\MixinCallNode; |
@@ -84,20 +85,32 @@ public function getKeywords(): array |
84 | 85 | ]; |
85 | 86 | } |
86 | 87 |
|
| 88 | + protected function getCodeNode(NodeInterface $linkedNode, ParserNodeInterface $parentNode = null, $value = null, array $children = null) |
| 89 | + { |
| 90 | + $code = new CodeNode($linkedNode->getToken(), null, $linkedNode->getLevel(), $parentNode, $children); |
| 91 | + |
| 92 | + if ($value !== null) { |
| 93 | + $code->setValue($value); |
| 94 | + } |
| 95 | + |
| 96 | + return $code; |
| 97 | + } |
| 98 | + |
87 | 99 | public function handleNodeEvent(NodeEvent $event): void |
88 | 100 | { |
89 | 101 | $call = $event->getNode(); |
90 | 102 |
|
91 | 103 | if ($call instanceof MixinCallNode) { |
92 | 104 | $call->setChildren(array_merge( |
93 | | - [(new CodeNode($call->getToken(), null, $call->getLevel(), $call))->setValue('$'.static::PUG_SLOT_NAME_VARIABLE.' = null')], |
94 | | - array_map(static function (NodeInterface $node) use ($call) { |
| 105 | + [$this->getCodeNode($call, $call, '$'.static::PUG_SLOT_NAME_VARIABLE.' = null')], |
| 106 | + array_map(function (NodeInterface $node) use ($call) { |
95 | 107 | if ($node instanceof KeywordNode && $node->getName() === 'slot') { |
96 | 108 | return $node; |
97 | 109 | } |
98 | 110 |
|
99 | | - return new CodeNode($node->getToken(), null, $node->getLevel(), $call, [ |
100 | | - (new TextNode($node->getToken(), null, $node->getLevel()))->setValue('if ($'.static::PUG_SLOT_NAME_VARIABLE.' === "__main__")'), |
| 111 | + return $this->getCodeNode($node, $call, null, [ |
| 112 | + (new TextNode($node->getToken(), null, $node->getLevel()))->setValue('if (!isset($'.static::PUG_SLOT_NAME_VARIABLE.') || $'.static::PUG_SLOT_NAME_VARIABLE.' === "__main__")'), |
| 113 | + $this->getCodeNode($node, null, '// main slot'), |
101 | 114 | $node, |
102 | 115 | ]); |
103 | 116 | }, $call->getChildren()) |
|
0 commit comments