server.inc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php declare(strict_types=1);
  2. function curl_cli_server_start() {
  3. $php_executable = getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY;
  4. $doc_root = __DIR__;
  5. $router = "responder/get.inc";
  6. $cmd = [$php_executable, '-t', $doc_root, '-n', '-S', 'localhost:0', $router];
  7. $descriptorspec = array(
  8. 0 => STDIN,
  9. 1 => STDOUT,
  10. 2 => ['pipe', 'w'],
  11. );
  12. $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
  13. // First, wait for the dev server to declare itself ready.
  14. $bound = null;
  15. stream_set_blocking($pipes[2], false);
  16. for ($i = 0; $i < 60; $i++) {
  17. usleep(50000); // 50ms per try
  18. $status = proc_get_status($handle);
  19. if (empty($status['running'])) {
  20. echo "Server is not running\n";
  21. proc_terminate($handle);
  22. exit(1);
  23. }
  24. while (($line = fgets($pipes[2])) !== false) {
  25. if (preg_match('@PHP \S* Development Server \(https?://(.*?:\d+)\) started@', $line, $matches)) {
  26. $bound = $matches[1];
  27. // Now that we've identified the listen address, close STDERR.
  28. // Otherwise the pipe may clog up with unread log messages.
  29. fclose($pipes[2]);
  30. break 2;
  31. }
  32. }
  33. }
  34. if ($bound === null) {
  35. echo "Server did not output startup message";
  36. proc_terminate($handle);
  37. exit(1);
  38. }
  39. // Now wait for a connection to succeed.
  40. // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
  41. // it might not be listening yet...need to wait until fsockopen() call returns
  42. $error = "Unable to connect to server\n";
  43. for ($i=0; $i < 60; $i++) {
  44. usleep(50000); // 50ms per try
  45. $status = proc_get_status($handle);
  46. $fp = @fsockopen("tcp://$bound");
  47. // Failure, the server is no longer running
  48. if (!($status && $status['running'])) {
  49. $error = "Server is not running\n";
  50. break;
  51. }
  52. // Success, Connected to servers
  53. if ($fp) {
  54. $error = '';
  55. break;
  56. }
  57. }
  58. if ($fp) {
  59. fclose($fp);
  60. }
  61. if ($error) {
  62. echo $error;
  63. proc_terminate($handle);
  64. exit(1);
  65. }
  66. register_shutdown_function(
  67. function($handle) use($router) {
  68. proc_terminate($handle);
  69. /* Wait for server to shutdown */
  70. for ($i = 0; $i < 60; $i++) {
  71. $status = proc_get_status($handle);
  72. if (!($status && $status['running'])) {
  73. break;
  74. }
  75. usleep(50000);
  76. }
  77. },
  78. $handle
  79. );
  80. return $bound;
  81. }