Annotation of embedaddon/php/sapi/cli/tests/php_cli_server.inc, revision 1.1.1.2
1.1 misho 1: <?php
1.1.1.2 ! misho 2: define ("PHP_CLI_SERVER_HOSTNAME", "localhost");
! 3: define ("PHP_CLI_SERVER_PORT", 8964);
! 4: define ("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
1.1 misho 5:
6: function php_cli_server_start($code = 'echo "Hello world";', $no_router = FALSE) {
7: $php_executable = getenv('TEST_PHP_EXECUTABLE');
8: $doc_root = __DIR__;
9: $router = "index.php";
10:
11: if ($code) {
12: file_put_contents($doc_root . '/' . $router, '<?php ' . $code . ' ?>');
13: }
14:
15: $descriptorspec = array(
16: 0 => STDIN,
17: 1 => STDOUT,
18: 2 => STDERR,
19: );
20:
21: if (substr(PHP_OS, 0, 3) == 'WIN') {
22: $cmd = "{$php_executable} -t {$doc_root} -n -S " . PHP_CLI_SERVER_ADDRESS;
23: if (!$no_router) {
24: $cmd .= " {$router}";
25: }
26:
27: $handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true, "suppress_errors" => true));
28: } else {
29: $cmd = "exec {$php_executable} -t {$doc_root} -n -S " . PHP_CLI_SERVER_ADDRESS;
30: if (!$no_router) {
31: $cmd .= " {$router}";
32: }
33: $cmd .= " 2>/dev/null";
34:
35: $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
36: }
1.1.1.2 ! misho 37:
! 38: // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
! 39: // it might not be listening yet...need to wait until fsockopen() call returns
! 40: $i = 0;
! 41: while (($i++ < 30) && !($fp = @fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT))) {
! 42: usleep(10000);
! 43: }
! 44:
! 45: if ($fp) {
! 46: fclose($fp);
! 47: }
1.1 misho 48:
49: register_shutdown_function(
50: function($handle) use($router) {
51: proc_terminate($handle);
52: @unlink(__DIR__ . "/{$router}");
53: },
54: $handle
55: );
1.1.1.2 ! misho 56: // don't bother sleeping, server is already up
! 57: // server can take a variable amount of time to be up, so just sleeping a guessed amount of time
! 58: // does not work. this is why tests sometimes pass and sometimes fail. to get a reliable pass
! 59: // sleeping doesn't work.
1.1 misho 60: }
61: ?>
62:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>