-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (84 loc) · 2.88 KB
/
index.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var php2ast = require("phptoast"),
php2js = require("phptojs"),
phprt = require("phpruntime/sync"),
beauty = require("beautifier").js_beautify,
path = require("path"),
fs = require("fs");
// This environment is used throughout the global scope.
var $environment = phprt.createEnvironment();
var $parser = php2ast.create();
var $options = {};
// Set up defaults...
var $stdout = $environment.getStdout();
var $stderr = $environment.getStderr();
// # I/O for console. STDIN is unused by default.
$stdout.on("data",function(str){ process.stdout.write(str); });
$stderr.on("data",function(str){ process.stderr.write(str); });
// include/_once|require/_once() support...
$environment.options = {
include: function(requiredFile, p, sourceFile) {
var requireable;
if(path.isAbsolute(requiredFile)) {
requireable = requiredFile;
} else {
var sourceDir = path.dirname(sourceFile);
requireable = path.join(sourceDir, requiredFile);
}
var src = fs.readFileSync(requireable, "utf8");
var bareSource = compile(src, true);
var bareWrapper = new Function('return ' + bareSource + ';');
var wrapper = phprt.compile(bareWrapper);
p.resolve(wrapper);
}
}
// Simple wrapper function.
function compile(source, bare) {
bare = bare || false;
var jsSource = php2js.transpile(
$parser.parse(source),
{ sync: true, bare: bare }
);
return beauty(jsSource);
}
function makeModule(wrapper, file) {
file = JSON.stringify(file);
var thisModule = JSON.stringify(__filename);
var _Module = JSON.stringify(require.resolve("microdash"));
return beauty([
// Pull ourselves in.
"var uniter = require("+thisModule+")",
"var _ = require("+_Module+");",
// Generate/compile the engine and create a context
"var generator = "+wrapper,
"var opts = _.extend({path: "+file+"}, uniter.getOptions());",
"var context = generator(opts, uniter.getEnvironment());",
// Expose the JS stuff...
"context.expose(module, 'module');",
"context.expose(exports, 'exports');",
// I want to ACTUALLY expose require...
"context.expose(require, 'require');",
// Return.
"var rt = context.execute();"
].join("\n"));
}
module.exports = function UniterPHP() {
// Register!
require.extensions[".php"] = function(module, filename) {
var source = fs.readFileSync(filename, 'utf8');
var moduleCode = makeModule(compile(source), filename);
// Evaluate.
module._compile(moduleCode, filename);
}
}
module.exports.getEnvironment = function() {
return $environment;
}
module.exports.getParser = function() {
return $parser;
}
module.exports.getOptions = function() {
return $options;
}
module.exports.setOptions = function(opt) {
$options = opt;
}