Skip to content

Commit e09373c

Browse files
committed
Load js-phpize on demand
1 parent 459fa29 commit e09373c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/JsPhpize/JsPhpizePhug.php

+14-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Phug\AbstractCompilerModule;
1010
use Phug\Compiler;
1111
use Phug\CompilerEvent;
12+
use Phug\CompilerInterface;
1213
use Phug\Renderer;
1314
use Phug\Util\ModuleContainerInterface;
1415

@@ -49,8 +50,7 @@ public function __construct(ModuleContainerInterface $container)
4950
'patterns' => [
5051
'transform_expression' => function ($jsCode) use ($compiler) {
5152

52-
/** @var JsPhpize $jsPhpize */
53-
$jsPhpize = $compiler->getOption('jsphpize_engine');
53+
$jsPhpize = $this->getJsPhpizeEngine($compiler);
5454

5555
$compilation = $this->compile($jsPhpize, $jsCode, $compiler->getPath());
5656

@@ -64,6 +64,18 @@ public function __construct(ModuleContainerInterface $container)
6464
]);
6565
}
6666

67+
/**
68+
* @return JsPhpize
69+
*/
70+
public function getJsPhpizeEngine(CompilerInterface $compiler)
71+
{
72+
if (!$compiler->hasOption('jsphpize_engine')) {
73+
$compiler->setOption('jsphpize_engine', new JsPhpize($this->getOptions()));
74+
}
75+
76+
return $compiler->getOption('jsphpize_engine');
77+
}
78+
6779
public function compile(JsPhpize $jsPhpize, $code, $fileName)
6880
{
6981
try {
@@ -92,10 +104,6 @@ public function compile(JsPhpize $jsPhpize, $code, $fileName)
92104
public function getEventListeners()
93105
{
94106
return [
95-
CompilerEvent::COMPILE => function (Compiler\Event\CompileEvent $event) {
96-
$event->getTarget()->setOption('jsphpize_engine', new JsPhpize($this->getOptions()));
97-
},
98-
99107
CompilerEvent::OUTPUT => function (Compiler\Event\OutputEvent $event) {
100108
$compiler = $event->getTarget();
101109

0 commit comments

Comments
 (0)