-
Notifications
You must be signed in to change notification settings - Fork 0
/
npm_require.php
61 lines (54 loc) · 1.75 KB
/
npm_require.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
function npm_require($modulename) {
return new npm_module($modulename);
}
class npm_module {
private $props = array();
function __construct($modulename) {
$mod = shell_exec('nodejs -e "var m=require(\"'.$modulename.'\");'.
'if(typeof m===\"function\"){console.log(\"self function\");}'.
'Object.keys(m).forEach(function(k){console.log(k, m[k]);})"'.PHP_EOL);
$props = explode("\n", trim($mod));
foreach($props as $prop) {
list($name, $val) = explode(' ', trim($prop));
if($name === 'self') {
$this->props['self'] = function() use ($modulename, $name) {
$args = json_encode(func_get_args());
$args = substr($args, 1, strlen($args) - 2);
$ret = json_decode(trim(shell_exec('nodejs -e "var m=require(\"'.$modulename.'\");'.
'console.log(JSON.stringify(m('.addslashes($args).')));"'.PHP_EOL)));
return $ret;
};
}
elseif($val === 'function') {
$this->props[$name] = function() use ($modulename, $name) {
$args = json_encode(func_get_args());
$args = substr($args, 1, strlen($args) - 2);
$ret = json_decode(trim(shell_exec('nodejs -e "var m=require(\"'.$modulename.'\");'.
'console.log(m[\''.$name.'\']('.addslashes($args).'));"'.PHP_EOL)));
return $ret;
};
}
else {
$this->props[$name] = $val;
}
}
return $mod;
}
function __invoke() {
if(isset($this->props['self'])) {
return call_user_func_array($this->props['self'], func_get_args());
}
}
function __call($name, $arguments) {
if(isset($this->props[$name])) {
return call_user_func_array($this->props[$name], $arguments);
}
}
function __get($name) {
if(isset($this->props[$name])) {
return $this->props[$name];
}
}
}
?>