-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp-runner.js
More file actions
111 lines (88 loc) · 3.1 KB
/
php-runner.js
File metadata and controls
111 lines (88 loc) · 3.1 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"use strict";
var util = require("util");
var c9util = require("../cloud9.core/util");
var ShellRunner = require("../cloud9.run.shell/shell").Runner;
var exports = module.exports = function(url, vfs, pm, sandbox, callback) {
sandbox.getProjectDir(function(err, projectDir) {
if (err) return callback(err);
init(projectDir, url);
});
function init(projectDir, url) {
pm.addRunner("php", exports.factory(vfs, sandbox, projectDir, url));
callback();
}
};
exports.factory = function(vfs, sandbox, root, url) {
return function(args, eventEmitter, eventName, callback) {
var options = {};
c9util.extend(options, args);
options.root = root;
options.file = args.file;
options.args = args.args;
options.cwd = args.cwd;
options.env = args.env;
options.encoding = args.encoding;
options.eventEmitter = eventEmitter;
options.eventName = eventName;
options.url = url;
options.sandbox = sandbox;
new Runner(vfs, options, callback);
};
};
var Runner = exports.Runner = function(vfs, options, callback) {
var self = this;
if (!options.sandbox) {
return callback("No sandbox specified");
}
self.vfs = vfs;
self.root = options.root;
self.file = options.file || "";
options.env = options.env || {};
// first we need to get an open port
options.sandbox.getPort(function (err, port) {
if (err) {
return console.error("getPort failed");
}
// create a url.
// this can be passed in as an option, or we can construct it
// based on the host and the port
if (!options.url) {
options.sandbox.getHost(function(err, host) {
if (err) return console.error(err);
var url = "http://" + host + ":" + port;
startProcess(url, port);
});
}
else {
startProcess(options.url, port);
}
});
function startProcess(url, port) {
self.port = port;
if (self.port) {
options.env.PORT = self.port;
}
// a nice debug message for our users when we fire up the process
var debugMessageListener = function (msg) {
// process dies? then we die as well
if (msg.type === "php-exit") {
return options.eventEmitter.removeListener(options.eventName, debugMessageListener);
}
if (msg.type === "php-start") {
var info = [];
options.eventEmitter.emit(options.eventName, {
type: "php-debug-data",
stream: "stdout",
data: info.join("\n"),
extra: null,
pid: msg.pid
});
}
};
options.eventEmitter.on(options.eventName, debugMessageListener);
options.cwd = options.cwd ? options.cwd : options.root;
options.command = "php";
ShellRunner.call(self, vfs, options, callback);
}
};
util.inherits(Runner, ShellRunner);