bootstrap-server.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. $php_version = phpversion();
  3. $php_major = floatval(substr($php_version, 0, 3));
  4. // Define SIGKILL if pcntl is not found
  5. if (!function_exists('pcntl_signal')) {
  6. define('SIGKILL', 9);
  7. }
  8. if ($php_major < 5.4) {
  9. define('WITHOUT_SERVER', true);
  10. } else {
  11. // Command that starts the built-in web server
  12. $command = sprintf('php -S %s:%d -t %s >./server.log 2>&1 & echo $!', WEB_SERVER_HOST, WEB_SERVER_PORT, WEB_SERVER_DOCROOT);
  13. // Execute the command and store the process ID
  14. $output = array();
  15. exec($command, $output, $exit_code);
  16. // sleep for a second to let server come up
  17. sleep(1);
  18. $pid = (int) $output[0];
  19. // check server.log to see if it failed to start
  20. $server_logs = file_get_contents("./server.log");
  21. if (strpos($server_logs, "Fail") !== false) {
  22. // server failed to start for some reason
  23. print "Failed to start server! Logs:" . PHP_EOL . PHP_EOL;
  24. print_r($server_logs);
  25. exit(1);
  26. }
  27. echo sprintf('%s - Web server started on %s:%d with PID %d', date('r'), WEB_SERVER_HOST, WEB_SERVER_PORT, $pid) . PHP_EOL;
  28. register_shutdown_function(function() {
  29. // cleanup after ourselves -- remove log file, shut down server
  30. global $pid;
  31. unlink("./server.log");
  32. posix_kill($pid, SIGKILL);
  33. });
  34. }