php_cli_server.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. // TODO: Move address/port info in here?
  3. class CliServerInfo {
  4. public function __construct(
  5. public string $docRoot,
  6. ) {}
  7. }
  8. function php_cli_server_start(
  9. ?string $code = 'echo "Hello world";',
  10. ?string $router = 'index.php',
  11. array $cmd_args = []
  12. ): CliServerInfo {
  13. $php_executable = getenv('TEST_PHP_EXECUTABLE');
  14. $error = null;
  15. // Create dedicated doc root to avoid index.php clashes between tests.
  16. $doc_root = __DIR__ . '/' . basename($_SERVER['PHP_SELF'], '.php');
  17. @mkdir($doc_root);
  18. if ($code) {
  19. file_put_contents($doc_root . '/' . ($router ?: 'index.php'), '<?php ' . $code . ' ?>');
  20. }
  21. $cmd = [$php_executable, '-t', $doc_root, '-n', ...$cmd_args, '-S', 'localhost:0'];
  22. if (!is_null($router)) {
  23. $cmd[] = $router;
  24. }
  25. $descriptorspec = array(
  26. 0 => STDIN,
  27. 1 => STDOUT,
  28. 2 => ['pipe', 'w'],
  29. );
  30. $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
  31. // First, wait for the dev server to declare itself ready.
  32. $bound = null;
  33. stream_set_blocking($pipes[2], false);
  34. for ($i = 0; $i < 60; $i++) {
  35. usleep(50000); // 50ms per try
  36. $status = proc_get_status($handle);
  37. if (empty($status['running'])) {
  38. echo "Server is not running\n";
  39. proc_terminate($handle);
  40. exit(1);
  41. }
  42. while (($line = fgets($pipes[2])) !== false) {
  43. if (preg_match('@PHP \S* Development Server \(https?://(.*?:\d+)\) started@', $line, $matches)) {
  44. $bound = $matches[1];
  45. // Now that we've identified the listen address, close STDERR.
  46. // Otherwise the pipe may clog up with unread log messages.
  47. fclose($pipes[2]);
  48. break 2;
  49. }
  50. }
  51. }
  52. if ($bound === null) {
  53. echo "Server did not output startup message";
  54. proc_terminate($handle);
  55. exit(1);
  56. }
  57. // Now wait for a connection to succeed.
  58. // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
  59. // it might not be listening yet...need to wait until fsockopen() call returns
  60. $error = "Unable to connect to server\n";
  61. for ($i=0; $i < 60; $i++) {
  62. usleep(50000); // 50ms per try
  63. $status = proc_get_status($handle);
  64. $fp = @fsockopen("tcp://$bound");
  65. // Failure, the server is no longer running
  66. if (!($status && $status['running'])) {
  67. $error = "Server is not running\n";
  68. break;
  69. }
  70. // Success, Connected to servers
  71. if ($fp) {
  72. $error = '';
  73. break;
  74. }
  75. }
  76. if ($error) {
  77. echo $error;
  78. proc_terminate($handle);
  79. exit(1);
  80. }
  81. register_shutdown_function(
  82. function($handle) use($router, $doc_root) {
  83. proc_terminate($handle);
  84. @unlink(__DIR__ . "/{$router}");
  85. @rmdir($doc_root);
  86. },
  87. $handle
  88. );
  89. // Define the same "constants" we previously did.
  90. $port = (int) substr($bound, strrpos($bound, ':') + 1);
  91. define("PHP_CLI_SERVER_HOSTNAME", "localhost");
  92. define("PHP_CLI_SERVER_PORT", $port);
  93. define("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
  94. return new CliServerInfo($doc_root);
  95. }
  96. function php_cli_server_connect() {
  97. $timeout = 1.0;
  98. $fp = fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT, $errno, $errstr, $timeout);
  99. if (!$fp) {
  100. die("connect failed");
  101. }
  102. return $fp;
  103. }
  104. ?>