diff --git a/php-webkit/main.js b/php-webkit/main.js index 4c41d7e..277a2ac 100644 --- a/php-webkit/main.js +++ b/php-webkit/main.js @@ -1,105 +1,83 @@ -var phpwebkit = { +var gui = require('nw.gui'); +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(); - runApp: function(){ +process.on('uncaughtException', function(err){ + changeState(''+err+'', '#CD0000'); +}); - var gui = require('nw.gui'); - var win = gui.Window.get(); - var os = require('os'); - var os = os.platform(); - var pw = this; +win.title = gui.App.manifest.name; - process.on('uncaughtException', function(err){ - pw.changeState(''+err+'', '#CD0000'); - }); - - win.title = gui.App.manifest.name; - - var bin = gui.App.manifest.phpwebkit.bin; - if(bin === undefined || bin == "") { - if(os == 'win32' || os == 'win64') { - bin = './bin/php/php-cgi.exe'; - } else if(os == 'darwin') { - bin = './bin/php/php-cgi'; - } else if(os == 'linux') { - bin = './bin/php/php-cgi'; - } else { - bin = 'php-cgi'; - } - } - - this.fileExists(bin, function(result){ - if(result === false) { - return 'php-cgi'; - } - }); - - var path = gui.App.manifest.phpwebkit.path; - if(path === undefined || path == "") { - path = './application'; - } - - var host = gui.App.manifest.phpwebkit.host; - if(host === undefined || host == "") { - host = '127.0.0.1'; - } - - var port = gui.App.manifest.phpwebkit.port; - if(port === undefined || port == "") { - port = 0; - } - - var config = { - "path": path, - "bin": bin, - "host": host, - "port": port, - "arguments": gui.App.argv, - "manifest": gui.App.manifest - } - - this.startServer(config); - }, - - startServer: function(config, callback){ - - var http = require('http'); - var php = require('../lib/bridge'); - var express = require('express'); - var app = express(); - var host = config.host; - 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('Error: '+server.err+'', '#CD0000'); - }); - }, - - changeState: function(msg, color){ - - $('body').css('background-color', color); - $('#loading p').html(msg); - }, - - fileExists: function(file, callback){ - - var fs = require('fs'); - fs.stat(file, function(err, stats) { - if(!err && stats.isFile()) { - return true; - } else { - return false; - } - }); +var bin = gui.App.manifest.phpwebkit.bin; +if(bin === undefined || bin == "") { + if(os == 'win32' || os == 'win64') { + bin = './bin/php/php-cgi.exe'; + } else if(os == 'darwin') { + bin = './bin/php/php-cgi'; + } else if(os == 'linux') { + bin = './bin/php/php-cgi'; + } else { + bin = 'php-cgi'; } +} +binExists(bin, function(filename){ + bin = filename; +}); + +var path = gui.App.manifest.phpwebkit.path; +if(path === undefined || path == "") { + path = './application'; +} + +var host = gui.App.manifest.phpwebkit.host; +if(host === undefined || host == "") { + host = '127.0.0.1'; +} + +var port = gui.App.manifest.phpwebkit.port; +if(port === undefined || port == "") { + port = 0; +} + +var phpinfo = { + "path": path, + "bin": bin, + "host": host, + "port": port, + "arguments": gui.App.argv, + "manifest": gui.App.manifest }; -phpwebkit.runApp(); \ No newline at end of file +var server = http.createServer(app); + +app.use('/', php.cgi(phpinfo)); + +server.listen(port, host, function(){ + var url = 'http://'+host+':'+server.address().port+'/'; + changeState('Starting application...', '#00CD00'); + window.location = url; +}).on('error', function(e) { + changeState('Error: '+e.message+'', '#CD0000'); +}); + +function changeState(msg, color){ + $('body').css('background-color', color); + $('#loading p').html(msg); +} + +function binExists(file, callback){ + fs.stat(bin, function(err, stats) { + if(!err && stats.isFile()) { + return stats.filename; + } else { + return 'php-cgi'; + } + }); +} \ No newline at end of file