Restructuring and refactoring

This commit is contained in:
Brad Metcalf 2015-04-02 23:44:06 -05:00
commit 36dbfbfd6d

View file

@ -1,21 +1,21 @@
var gui = require('nw.gui'); var phpwebkit = {
var win = gui.Window.get();
var http = require('http');
var fs = require('fs');
var php = require('../lib/bridge');
var express = require('express');
var app = express();
var os = require('os');
var os = os.platform();
process.on('uncaughtException', function(err){ runApp: function(){
changeState('<strong>'+err+'</strong>', '#CD0000');
});
win.title = gui.App.manifest.name; var gui = require('nw.gui');
var win = gui.Window.get();
var os = require('os');
var os = os.platform();
var pw = this;
var bin = gui.App.manifest.phpwebkit.bin; process.on('uncaughtException', function(err){
if(bin === undefined || bin == "") { pw.changeState('<strong>'+err+'</strong>', '#CD0000');
});
win.title = gui.App.manifest.name;
var bin = gui.App.manifest.phpwebkit.bin;
if(bin === undefined || bin == "") {
if(os == 'win32' || os == 'win64') { if(os == 'win32' || os == 'win64') {
bin = './bin/php/php-cgi.exe'; bin = './bin/php/php-cgi.exe';
} else if(os == 'darwin') { } else if(os == 'darwin') {
@ -25,59 +25,81 @@ if(bin === undefined || bin == "") {
} else { } else {
bin = 'php-cgi'; bin = 'php-cgi';
} }
} }
binExists(bin, function(filename){ this.fileExists(bin, function(result){
bin = filename; if(result === false) {
}); return 'php-cgi';
}
});
var path = gui.App.manifest.phpwebkit.path; var path = gui.App.manifest.phpwebkit.path;
if(path === undefined || path == "") { if(path === undefined || path == "") {
path = './application'; path = './application';
} }
var host = gui.App.manifest.phpwebkit.host; var host = gui.App.manifest.phpwebkit.host;
if(host === undefined || host == "") { if(host === undefined || host == "") {
host = '127.0.0.1'; host = '127.0.0.1';
} }
var port = gui.App.manifest.phpwebkit.port; var port = gui.App.manifest.phpwebkit.port;
if(port === undefined || port == "") { if(port === undefined || port == "") {
port = 0; port = 0;
} }
var phpinfo = { var config = {
"path": path, "path": path,
"bin": bin, "bin": bin,
"host": host, "host": host,
"port": port, "port": port,
"arguments": gui.App.argv, "arguments": gui.App.argv,
"manifest": gui.App.manifest "manifest": gui.App.manifest
}; }
var server = http.createServer(app); this.startServer(config);
},
app.use('/', php.cgi(phpinfo)); startServer: function(config, callback){
server.listen(port, host, function(){ var http = require('http');
var url = 'http://'+host+':'+server.address().port+'/'; var php = require('../lib/bridge');
changeState('Starting application...', '#00CD00'); var express = require('express');
window.location = url; var app = express();
}).on('error', function(e) { var host = config.host;
changeState('<strong>Error: '+e.message+'</strong>', '#CD0000'); var port = config.port;
}); var pw = this;
var server = http.createServer(app);
app.use('/', php.cgi(config));
server.listen(port, host, function(){
pw.changeState('Starting application...', '#00CD00');
window.location = 'http://'+host+':'+server.address().port +'/';
}).on('error', function(err) {
pw.changeState('<strong>Error: '+server.err+'</strong>', '#CD0000');
});
},
changeState: function(msg, color){
function changeState(msg, color){
$('body').css('background-color', color); $('body').css('background-color', color);
$('#loading p').html(msg); $('#loading p').html(msg);
} },
function binExists(file, callback){ fileExists: function(file, callback){
fs.stat(bin, function(err, stats) {
var fs = require('fs');
fs.stat(file, function(err, stats) {
if(!err && stats.isFile()) { if(!err && stats.isFile()) {
return stats.filename; return true;
} else { } else {
return 'php-cgi'; return false;
} }
}); });
} }
};
phpwebkit.runApp();