3 function serve(callable $cb) {
4 foreach (range(8000, 9000) as $port) {
5 if (($server = stream_socket_server("tcp://localhost:$port"))) {
6 fprintf(STDERR, "%s\n", $port);
8 $R = [$server]; $W = []; $E = [];
9 $select = stream_select($R, $E, $E, 0, 10000);
10 if ($select && ($client = stream_socket_accept($server, 1))) {
11 if (getenv("PHP_HTTP_TEST_SSL")) {
12 stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER);
14 while (!feof($client)) {
18 } while ($select !== false);
24 function server($file, callable $cb) {
25 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
26 if (($proc = proc_open(PHP_BINARY . " $file", $spec, $pipes, __DIR__))) {
27 $port = trim(fgets($pipes[2]));
29 $cb($port, $stdin = $pipes[0], $stdout = $pipes[1], $stderr = $pipes[2]);
31 proc_terminate($proc);