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($handler, callable $cb) {
25 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
26 $comm = sprintf("%s %s/%s", PHP_BINARY, __DIR__, $handler);
27 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
28 $port = trim(fgets($pipes[2]));
31 $cb($port, $stdin = $pipes[0], $stdout = $pipes[1], $stderr = $pipes[2]);
32 } catch (Exception $e) {
36 proc_terminate($proc);