Skip to content

Commit de056c9

Browse files
committed
Fix dependencies separate compilation
1 parent 7cabaa2 commit de056c9

File tree

5 files changed

+190
-8
lines changed

5 files changed

+190
-8
lines changed

phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<testsuite name="compile">
1414
<file>tests/compile.php</file>
1515
</testsuite>
16+
<testsuite name="JsPhpize">
17+
<file>tests/JsPhpize/JsPhpize.php</file>
18+
</testsuite>
1619
</testsuites>
1720
<filter>
1821
<whitelist processUncoveredFilesFromWhitelist="true">

src/JsPhpize/JsPhpize.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function compileWithoutDependencies($input, $filename = null)
117117
*/
118118
public function compileDependencies()
119119
{
120-
$parser = new Parser($this, '');
120+
$parser = new Parser($this, '', '__dependencies');
121121
$compiler = new Compiler($this);
122122
$block = $parser->parse();
123123
$block->addDependencies($this->dependencies);

src/JsPhpize/Nodes/Block.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,32 @@ class Block
2424
*/
2525
protected $localVariables;
2626

27+
/**
28+
* @var array
29+
*/
30+
protected $dependencies;
31+
2732
public function __construct($type, $parentheses = null)
2833
{
2934
$this->type = $type;
3035
$this->nodes = array();
36+
$this->dependencies = array();
3137
$this->parentheses = $parentheses;
3238
}
3339

40+
public function addDependencies($dependencies)
41+
{
42+
$this->dependencies = array_merge($this->dependencies, $dependencies);
43+
}
44+
45+
public function popDependencies()
46+
{
47+
$dependencies = $this->dependencies;
48+
$this->dependencies = array();
49+
50+
return $dependencies;
51+
}
52+
3453
public function let($variable, $prefix)
3554
{
3655
$this->addInstructions(
@@ -87,6 +106,9 @@ public function setParentheses($parentheses)
87106

88107
public function getNodes()
89108
{
109+
foreach ($this->dependencies as $varname => $dependency) {
110+
array_unshift($this->nodes, '$GLOBALS["' . $this->helperPrefix . $varname . '"] = ' . $dependency, new NodeEnd());
111+
}
90112
if (count($this->localVariables)) {
91113
$localVariables = 'array(' . implode(', ', array_map(function ($data) {
92114
return 'array(' . var_export($data[0], true) . ',' . var_export($data[1], true) . ')';

src/JsPhpize/Nodes/Main.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44

55
class Main extends Block
66
{
7+
/**
8+
* @var string
9+
*/
710
protected $helperPrefix;
811

912
public function __construct($helperPrefix, $parentheses = null)
1013
{
1114
$this->helperPrefix = $helperPrefix;
1215
parent::__construct('main', $parentheses);
1316
}
14-
15-
public function addDependencies($dependencies)
16-
{
17-
foreach ($dependencies as $varname => $dependency) {
18-
array_unshift($this->nodes, '$GLOBALS["' . $this->helperPrefix . $varname . '"] = ' . $dependency);
19-
}
20-
}
2117
}

tests/JsPhpize/JsPhpize.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
namespace JsPhpizeTest;
4+
5+
use JsPhpize\JsPhpize;
6+
7+
class JsPhpizeTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testCompileFile()
10+
{
11+
$jsPhpize = new JsPhpize();
12+
$actual = $jsPhpize->compileFile(__DIR__ . '/../../examples/basic.js');
13+
$expected = <<<'EOD'
14+
$GLOBALS["__jp_h_plus"] = function ($base) {
15+
foreach (array_slice(func_get_args(), 1) as $value) {
16+
$base = is_string($base) || is_string($value) ? $base . $value : $base + $value;
17+
}
18+
19+
return $base;
20+
};
21+
$GLOBALS["__jp_h_dot"] = function ($base) {
22+
foreach (array_slice(func_get_args(), 1) as $key) {
23+
$base = is_array($base)
24+
? (isset($base[$key]) ? $base[$key] : null)
25+
: (is_object($base)
26+
? (isset($base->$key)
27+
? $base->$key
28+
: (method_exists($base, $method = "get" . ucfirst($key))
29+
? $base->$method()
30+
: (method_exists($base, $key)
31+
? $base->$key()
32+
: null
33+
)
34+
)
35+
)
36+
: null
37+
);
38+
}
39+
40+
return $base;
41+
};
42+
$foo = array( 'bar' => array( "baz" => "hello" ) );
43+
// Comment
44+
$biz = 'bar';
45+
// com
46+
return call_user_func($GLOBALS["__jp_h_plus"], call_user_func($GLOBALS["__jp_h_dot"], $foo, 'bar', "baz"), ' ' , call_user_func($GLOBALS["__jp_h_plus"], call_user_func($GLOBALS["__jp_h_dot"], $foo, $biz, 'baz'), " " , call_user_func($GLOBALS["__jp_h_dot"], $foo, 'bar', 'baz')));
47+
EOD;
48+
$actual = preg_replace('/\s/', '', $actual);
49+
$expected = preg_replace('/\s/', '', $expected);
50+
$this->assertSame($expected, $actual);
51+
$this->assertSame('', $jsPhpize->compileDependencies());
52+
53+
$actual = $jsPhpize->compileFile(__DIR__ . '/../../examples/basic.js', true);
54+
$expected = <<<'EOD'
55+
$foo = array( 'bar' => array( "baz" => "hello" ) );
56+
// Comment
57+
$biz = 'bar';
58+
// com
59+
return call_user_func($GLOBALS["__jp_h_plus"], call_user_func($GLOBALS["__jp_h_dot"], $foo, 'bar', "baz"), ' ' , call_user_func($GLOBALS["__jp_h_plus"], call_user_func($GLOBALS["__jp_h_dot"], $foo, $biz, 'baz'), " " , call_user_func($GLOBALS["__jp_h_dot"], $foo, 'bar', 'baz')));
60+
EOD;
61+
$actual = preg_replace('/\s/', '', $actual);
62+
$expected = preg_replace('/\s/', '', $expected);
63+
$this->assertSame($expected, $actual);
64+
65+
$actual = $jsPhpize->compileDependencies();
66+
$expected = <<<'EOD'
67+
$GLOBALS["__jp_h_plus"] = function ($base) {
68+
foreach (array_slice(func_get_args(), 1) as $value) {
69+
$base = is_string($base) || is_string($value) ? $base . $value : $base + $value;
70+
}
71+
72+
return $base;
73+
};
74+
$GLOBALS["__jp_h_dot"] = function ($base) {
75+
foreach (array_slice(func_get_args(), 1) as $key) {
76+
$base = is_array($base)
77+
? (isset($base[$key]) ? $base[$key] : null)
78+
: (is_object($base)
79+
? (isset($base->$key)
80+
? $base->$key
81+
: (method_exists($base, $method = "get" . ucfirst($key))
82+
? $base->$method()
83+
: (method_exists($base, $key)
84+
? $base->$key()
85+
: null
86+
)
87+
)
88+
)
89+
: null
90+
);
91+
}
92+
93+
return $base;
94+
};
95+
EOD;
96+
$actual = preg_replace('/\s/', '', $actual);
97+
$expected = preg_replace('/\s/', '', $expected);
98+
$this->assertSame($expected, $actual);
99+
100+
$jsPhpize->compileFile(__DIR__ . '/../../examples/calcul.js', true);
101+
$actual = $jsPhpize->compileDependencies();
102+
$actual = preg_replace('/\s/', '', $actual);
103+
$this->assertSame($expected, $actual);
104+
}
105+
106+
/**
107+
* @expectedException \Exception
108+
* @expectedExceptionMessageRegExp /No such file/
109+
*/
110+
public function testCompileFileMissing()
111+
{
112+
try {
113+
$jsPhpize = new JsPhpize();
114+
$jsPhpize->compileFile('does/not/exists.js');
115+
} catch (\Exception $e) {
116+
throw new \Exception($e->getMessage(), 1);
117+
}
118+
}
119+
120+
public function testCompileSource()
121+
{
122+
$jsPhpize = new JsPhpize();
123+
$actual = $jsPhpize->compileCode('b = 8');
124+
$expected = '$b = 8;';
125+
$actual = preg_replace('/\s/', '', $actual);
126+
$expected = preg_replace('/\s/', '', $expected);
127+
$this->assertSame($expected, $actual);
128+
129+
$dir = getcwd();
130+
chdir(__DIR__ . '/../../examples');
131+
$actual = $jsPhpize->compileCode('calcul.js');
132+
chdir($dir);
133+
$expected = <<<'EOD'
134+
$GLOBALS["__jp_h_dot"] = function ($base) {
135+
foreach (array_slice(func_get_args(), 1) as $key) {
136+
$base = is_array($base)
137+
? (isset($base[$key]) ? $base[$key] : null)
138+
: (is_object($base)
139+
? (isset($base->$key)
140+
? $base->$key
141+
: (method_exists($base, $method = "get" . ucfirst($key))
142+
? $base->$method()
143+
: (method_exists($base, $key)
144+
? $base->$key()
145+
: null
146+
)
147+
)
148+
)
149+
: null
150+
);
151+
}
152+
153+
return $base;
154+
};
155+
call_user_func($GLOBALS["__jp_h_dot"], $calcul, 'js');
156+
EOD;
157+
$actual = preg_replace('/\s/', '', $actual);
158+
$expected = preg_replace('/\s/', '', $expected);
159+
$this->assertSame($expected, $actual);
160+
}
161+
}

0 commit comments

Comments
 (0)