2011-09-20 07:10:46 +00:00
|
|
|
<?php
|
2012-05-06 16:05:02 +00:00
|
|
|
define ("PHP_CLI_SERVER_HOSTNAME", "localhost");
|
|
|
|
define ("PHP_CLI_SERVER_PORT", 8964);
|
|
|
|
define ("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
|
2011-09-20 07:10:46 +00:00
|
|
|
|
2011-10-23 13:57:25 +00:00
|
|
|
function php_cli_server_start($code = 'echo "Hello world";', $no_router = FALSE) {
|
2011-10-25 10:54:39 +00:00
|
|
|
$php_executable = getenv('TEST_PHP_EXECUTABLE');
|
2011-09-20 07:10:46 +00:00
|
|
|
$doc_root = __DIR__;
|
2011-10-23 13:57:25 +00:00
|
|
|
$router = "index.php";
|
2012-03-20 07:41:43 +00:00
|
|
|
|
2011-11-15 03:19:54 +00:00
|
|
|
if ($code) {
|
|
|
|
file_put_contents($doc_root . '/' . $router, '<?php ' . $code . ' ?>');
|
|
|
|
}
|
2011-09-20 07:10:46 +00:00
|
|
|
|
|
|
|
$descriptorspec = array(
|
|
|
|
0 => STDIN,
|
|
|
|
1 => STDOUT,
|
2011-11-17 02:32:21 +00:00
|
|
|
2 => STDERR,
|
2011-09-20 07:10:46 +00:00
|
|
|
);
|
|
|
|
|
2011-10-25 10:54:39 +00:00
|
|
|
if (substr(PHP_OS, 0, 3) == 'WIN') {
|
2011-11-18 06:15:25 +00:00
|
|
|
$cmd = "{$php_executable} -t {$doc_root} -n -S " . PHP_CLI_SERVER_ADDRESS;
|
2011-10-25 10:54:39 +00:00
|
|
|
if (!$no_router) {
|
|
|
|
$cmd .= " {$router}";
|
|
|
|
}
|
2011-10-23 13:57:25 +00:00
|
|
|
|
2011-11-17 02:32:21 +00:00
|
|
|
$handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true, "suppress_errors" => true));
|
2011-10-25 10:54:39 +00:00
|
|
|
} else {
|
2011-11-18 06:15:25 +00:00
|
|
|
$cmd = "exec {$php_executable} -t {$doc_root} -n -S " . PHP_CLI_SERVER_ADDRESS;
|
2011-10-25 10:54:39 +00:00
|
|
|
if (!$no_router) {
|
|
|
|
$cmd .= " {$router}";
|
|
|
|
}
|
2011-11-17 02:32:21 +00:00
|
|
|
$cmd .= " 2>/dev/null";
|
2011-09-20 07:10:46 +00:00
|
|
|
|
2011-10-25 10:54:39 +00:00
|
|
|
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
|
|
|
|
}
|
2012-05-06 16:05:02 +00:00
|
|
|
|
|
|
|
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
|
|
|
|
// it might not be listening yet...need to wait until fsockopen() call returns
|
2012-05-09 03:21:24 +00:00
|
|
|
$i = 0;
|
2012-08-01 15:54:01 +00:00
|
|
|
while (($i++ < 30) && !($fp = @fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT))) {
|
2012-05-09 03:21:24 +00:00
|
|
|
usleep(10000);
|
|
|
|
}
|
2012-05-06 16:05:02 +00:00
|
|
|
|
2012-05-09 03:21:24 +00:00
|
|
|
if ($fp) {
|
|
|
|
fclose($fp);
|
|
|
|
}
|
2011-09-20 07:10:46 +00:00
|
|
|
|
2011-10-25 10:54:39 +00:00
|
|
|
register_shutdown_function(
|
2011-11-17 02:32:21 +00:00
|
|
|
function($handle) use($router) {
|
2011-10-25 10:54:39 +00:00
|
|
|
proc_terminate($handle);
|
|
|
|
@unlink(__DIR__ . "/{$router}");
|
|
|
|
},
|
2011-11-17 02:32:21 +00:00
|
|
|
$handle
|
2011-10-25 10:54:39 +00:00
|
|
|
);
|
2012-05-06 16:05:02 +00:00
|
|
|
// don't bother sleeping, server is already up
|
|
|
|
// server can take a variable amount of time to be up, so just sleeping a guessed amount of time
|
|
|
|
// does not work. this is why tests sometimes pass and sometimes fail. to get a reliable pass
|
|
|
|
// sleeping doesn't work.
|
2011-09-20 07:10:46 +00:00
|
|
|
}
|
|
|
|
?>
|
2011-11-17 02:32:21 +00:00
|
|
|
|