Skip to content

Commit 5c2b442

Browse files
committed
Allow to pass options to JsPhpize instance
1 parent 6e6ad22 commit 5c2b442

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

src/JsPhpize/JsPhpizePhug.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,28 @@
77

88
class JsPhpizePhug extends CompilerModule
99
{
10+
/**
11+
* @var array
12+
*/
13+
protected $options = [];
14+
15+
public function __construct(array $options = [])
16+
{
17+
$this->options = array_merge([
18+
'catchDependencies' => true,
19+
], $options);
20+
}
21+
1022
public function injectCompiler(Compiler $compiler)
1123
{
12-
// 'dependencies_storage'
24+
$options = $this->options;
1325
$compiler->setOptionsRecursive([
1426
'formatter_options' => [
1527
'modules' => [new JsPhpizePhugFormatter($compiler)],
1628
],
1729
]);
18-
$compiler->addHook('pre_compile', 'jsphpize', function ($pugCode) use (&$compiler) {
19-
$compiler->setOption('jsphpize_engine', new JsPhpize([
20-
'catchDependencies' => true,
21-
]));
30+
$compiler->addHook('pre_compile', 'jsphpize', function ($pugCode) use (&$compiler, $options) {
31+
$compiler->setOption('jsphpize_engine', new JsPhpize($options));
2232

2333
return $pugCode;
2434
});

src/JsPhpize/JsPhpizePhugFormatter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,19 @@ public function __construct(Compiler $compiler)
2525
public function injectFormatter(Formatter $formatter)
2626
{
2727
$compiler = $this->compiler;
28-
// 'dependencies_storage'
2928
$formatter->setOptionsRecursive([
3029
'patterns' => [
3130
'transform_expression' => function ($jsCode) use (&$compiler, &$formatter) {
3231
/** @var JsPhpize $jsPhpize */
3332
$jsPhpize = $compiler->getOption('jsphpize_engine');
3433
$pugModuleName = $formatter->getOption('dependencies_storage');
35-
$jsCode = str_replace('$'.$pugModuleName, $pugModuleName, $jsCode);
34+
$newCode = str_replace('$'.$pugModuleName, $pugModuleName, $jsCode);
3635

3736
try {
3837
return rtrim(trim(preg_replace(
3938
'/\{\s*\}$/',
4039
'',
41-
trim($jsPhpize->compile($jsCode))
40+
trim($jsPhpize->compile($newCode))
4241
)), ';');
4342
} catch (Exception $e) {
4443
if (

tests/JsPhpize/ExceptionThrower.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Tests\JsPhpize;
4+
5+
class ExceptionThrower
6+
{
7+
public function __get($name)
8+
{
9+
throw new \Exception('ExceptionThrower');
10+
}
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Tests\JsPhpize;
4+
5+
use JsPhpize\JsPhpizePhug;
6+
use Phug\Compiler;
7+
8+
class JsPhpizePhugFormatterTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @expectedException \Exception
12+
* @expectedExceptionMessage ExceptionThrower
13+
*/
14+
public function testPlug()
15+
{
16+
include_once __DIR__ . '/ExceptionThrower.php';
17+
18+
$compiler = new Compiler([
19+
'modules' => [new JsPhpizePhug([
20+
'patterns' => [
21+
new ExceptionThrower(),
22+
],
23+
])],
24+
]);
25+
26+
$compiler->compile('a=foo');
27+
}
28+
}

0 commit comments

Comments
 (0)