-
Notifications
You must be signed in to change notification settings - Fork 36
/
server.inc.php
65 lines (60 loc) · 1.89 KB
/
server.inc.php
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
<?php
/**
*
* @author wukezhan<[email protected]>
* 2015-10-28 12:30
*
*/
class local_server
{
public static $host_port;
protected $_server;
public function listen($host, $port)
{
self::$host_port = $host.':'.$port;
// partly borrowed from https://github.com/php/php-src/blob/master/ext/curl/tests/server.inc
// but make it more simple
$php_executable = getenv('TEST_PHP_EXECUTABLE');
$php_executable = $php_executable ?: getenv('_');
$doc_root = __DIR__ . DIRECTORY_SEPARATOR . 'server';
$descriptorspec = array(
0 => STDIN,
1 => STDOUT,
2 => STDERR,
);
echo "start local server\n";
if (substr(PHP_OS, 0, 3) == 'WIN') {
$cmd = "{$php_executable} -t {$doc_root} -n -S ".self::$host_port;
$this->_server = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true, "suppress_errors" => true));
} else {
$cmd = "exec {$php_executable} -t {$doc_root} -n -S ".self::$host_port;
$cmd .= " 2>/dev/null";
$this->_server = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
}
if(is_resource($this->_server)){
$fp = 0;
$i = 0;
while (($i++ < 30) && !($fp = @fsockopen($host, $port))) {
usleep(10000);
}
if ($fp) {
fclose($fp);
}
echo "local server started\n";
}else{
echo "start local server failed\n";
exit;
}
}
public function __destruct()
{
$status = proc_terminate($this->_server);
if($status) {
echo "local server stopped\n";
}else{
echo "stop local server failed";
}
}
}
$server = new local_server();
$server->listen('127.0.0.1', 12345);