TestHttpServer.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Contracts\HttpClient\Test;
  11. use Symfony\Component\Process\PhpExecutableFinder;
  12. use Symfony\Component\Process\Process;
  13. class TestHttpServer
  14. {
  15. private static $process = [];
  16. /**
  17. * @return Process
  18. */
  19. public static function start(int $port = 8057)
  20. {
  21. if (isset(self::$process[$port])) {
  22. self::$process[$port]->stop();
  23. } else {
  24. register_shutdown_function(static function () use ($port) {
  25. self::$process[$port]->stop();
  26. });
  27. }
  28. $finder = new PhpExecutableFinder();
  29. $process = new Process(array_merge([$finder->find(false)], $finder->findArguments(), ['-dopcache.enable=0', '-dvariables_order=EGPCS', '-S', '127.0.0.1:'.$port]));
  30. $process->setWorkingDirectory(__DIR__.'/Fixtures/web');
  31. $process->start();
  32. self::$process[$port] = $process;
  33. do {
  34. usleep(50000);
  35. } while (!@fopen('http://127.0.0.1:'.$port, 'r'));
  36. return $process;
  37. }
  38. }