55use Closure ;
66use Phug \AbstractPlugin ;
77use Phug \Ast \NodeInterface ;
8+ use Phug \Compiler \Event \ElementEvent ;
89use Phug \Compiler \Event \NodeEvent ;
910use Phug \Compiler \Event \OutputEvent ;
1011use Phug \CompilerEvent ;
12+ use Phug \Formatter \Element \CodeElement ;
1113use Phug \Parser \NodeInterface as ParserNodeInterface ;
1214use Phug \Parser \Node \CodeNode ;
1315use Phug \Parser \Node \KeywordNode ;
@@ -89,6 +91,8 @@ protected function getCodeNode(NodeInterface $linkedNode, ParserNodeInterface $p
8991 $ code ->setValue ($ value );
9092 }
9193
94+ $ code ->noTransform = true ;
95+
9296 return $ code ;
9397 }
9498
@@ -116,29 +120,21 @@ public function handleNodeEvent(NodeEvent $event): void
116120
117121 public function handleOutputEvent (OutputEvent $ event ): void
118122 {
119- $ event ->setOutput (
120- implode ("\n" , [
121- '<?php ' ,
122- '$firstMixin = function (string ...$names) use (&$__pug_mixins) { ' ,
123- ' foreach ($names as $name) { ' ,
124- ' if (isset($__pug_mixins[$name])) { ' ,
125- ' return $name; ' ,
126- ' } ' ,
127- ' } ' ,
128- ' throw new \\InvalidArgumentException("No defined mixin/component in [".implode(", ", $names)."]"); ' ,
129- '}; ' ,
130- '$firstComponent = $firstMixin; ' ,
131- '?> ' ,
132- ]).
133- $ event ->getOutput ()
134- );
123+ $ event ->setOutput ($ this ->parseOutput ($ event ->getOutput ()));
135124 }
136125
137126 public function attachEvents (): void
138127 {
139128 $ compiler = $ this ->getCompiler ();
140129 $ compiler ->attach (CompilerEvent::NODE , [$ this , 'handleNodeEvent ' ]);
141130 $ compiler ->attach (CompilerEvent::OUTPUT , [$ this , 'handleOutputEvent ' ]);
131+ $ compiler ->attach (CompilerEvent::ELEMENT , function (ElementEvent $ event ) {
132+ $ code = $ event ->getElement ();
133+
134+ if ($ code instanceof CodeElement && ($ node = $ code ->getOriginNode ()) && ($ node ->noTransform ?? false )) {
135+ $ code ->preventFromTransformation ();
136+ }
137+ });
142138 }
143139
144140 public function detachEvents (): void
@@ -147,4 +143,27 @@ public function detachEvents(): void
147143 $ compiler ->detach (CompilerEvent::NODE , [$ this , 'handleNodeEvent ' ]);
148144 $ compiler ->detach (CompilerEvent::OUTPUT , [$ this , 'handleOutputEvent ' ]);
149145 }
146+
147+ private function parseOutput (string $ output ): string
148+ {
149+ $ mixinFunctionsCode = implode ("\n" , [
150+ '' ,
151+ '$firstMixin = function (string ...$names) use (&$__pug_mixins) { ' ,
152+ ' foreach ($names as $name) { ' ,
153+ ' if (isset($__pug_mixins[$name])) { ' ,
154+ ' return $name; ' ,
155+ ' } ' ,
156+ ' } ' ,
157+ ' throw new \\InvalidArgumentException("No defined mixin/component in [".implode(", ", $names)."]"); ' ,
158+ '}; ' ,
159+ '$firstComponent = $firstMixin; ' ,
160+ '' ,
161+ ]);
162+
163+ if (preg_match ('/^(<\?(?:php)?\s+namespace\s\S.*)((\n[\s\S]*)?)$/U ' , $ output , $ matches )) {
164+ return $ matches [1 ].$ mixinFunctionsCode .$ matches [2 ];
165+ }
166+
167+ return "<?php $ mixinFunctionsCode?> $ output " ;
168+ }
150169}
0 commit comments