Skip to content

Instantly share code, notes, and snippets.

@IngwiePhoenix
Created September 26, 2013 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IngwiePhoenix/6714232 to your computer and use it in GitHub Desktop.
Save IngwiePhoenix/6714232 to your computer and use it in GitHub Desktop.
<?php
// includes
include "WebSocket.php";
class Process extends Thread {
public $proc;
public $pipes=array();
public $spec=array();
public $programm;
public $args;
public function __construct($programm, array $args) {
$this->programm = $programm;
$this->args = $args;
}
public function run() {
$this->spec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", dirname(__file__)."/../Logs/PHP.err", "a")
);
$argStr = array(escapeshellcmd($this->programm));
foreach($this->args as $arg) $argStr[] .= escapeshellarg($arg);
$argStr = implode(" ", $argStr);
$this->proc = proc_open($argStr, $this->spec, $this->pipes, dirname(__file__), $_ENV);
}
public function stop() {
foreach($this->pipes as $p) fclose($this->pipes[$p]);
return proc_close($this->proc);
}
}
class Server extends Thread {
public function __construct($settings) {}
public function run() {}
}
class Browser extends Thread {
public function __construct($crport, $htport) {}
public function run() {
global $desk; // Import the desk object
$chrome = realpath(dirname(__file__)."/chromium");
$args = [
($desk->mode=="app"
?"--app"
:"--".$desk->mode
)."=http://localhost:".$this->htport."/",
"--remote-debugging-port=".$this->crport,
"--user-data-dir=".dirname(__file__)."/../",
"--disable-translate",
"--app-window-size=1024,800"
];
// Fire off the process
$this->chr = (new Process($chrome, $args))->start();
// All preparations done, give over to the next step;
$this->handle();
}
public function handle() {
// We need a web-socket. So we gonna see if we can have one
$appUrl = "http://localhost:".$this->htport."/";
$ws=null;
while($fgc = file_get_contents("http://localhost:".$this->crport."/json")) {
if($fgc != false) {
// Try to json_decode
$json = json_decode($fgc, true);
if(!is_null($json)) {
foreach($json as $k=>$o) {
if($o['url']==$appUrl) {
$ws=$o['webSocketDebuggerUrl'];
break; // OUT OF THE LOOP.
}
}
}
}
}
// Phew...fire off a new websocket...now.
$this->ws=new WebSocket($ws);
//...and possibly attach handlers and that lot.
}
}
class Deskshell {
public static main($desk, $appDir) {
$htdocs = realpath($appDir."/".$desk->htdocs);
$htport = freeport();
$crport = freeport($htport);
// Start server THEN browser.
(new Server([
"htdocs"=>$htdocs,
"defaultLocation"=>$desk->defaultLocation
"port"=>$htport,
]))->start();
(new Browser($crport, $htport))->start();
// Export the object
$GLOBALS['desk'] = $desk;
}
}
// Find a free port to use
function freeport($no=null) {
// Create a fake socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$p=0;
while($maybePort = mt_rand(1024,9999)) {
$p = @socket_bind($sock, "127.0.0.1", $maybePort);
if($no != $maybePort && $p) break;
}
socket_close($sock);
return $maybePort;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment