| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Contracts\HttpClient\Test; |
| 13 |
|
| 14 |
use Symfony\Component\Process\PhpExecutableFinder; |
| 15 |
use Symfony\Component\Process\Process; |
| 16 |
|
| 17 |
class TestHttpServer |
| 18 |
{ |
| 19 |
private static array $process = []; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public static function start(int $port = 8057): Process |
| 25 |
{ |
| 26 |
$workingDirectory = \func_get_args()[1] ?? __DIR__.'/Fixtures/web'; |
| 27 |
|
| 28 |
if (0 > $port) { |
| 29 |
$port = -$port; |
| 30 |
$ip = '[::1]'; |
| 31 |
} else { |
| 32 |
$ip = '127.0.0.1'; |
| 33 |
} |
| 34 |
|
| 35 |
if (isset(self::$process[$port])) { |
| 36 |
self::$process[$port]->stop(); |
| 37 |
} else { |
| 38 |
register_shutdown_function(static function () use ($port) { |
| 39 |
self::$process[$port]->stop(); |
| 40 |
}); |
| 41 |
} |
| 42 |
|
| 43 |
$finder = new PhpExecutableFinder(); |
| 44 |
$process = new Process(array_merge([$finder->find(false) ?: throw new \Exception('PHP executable not found.')], $finder->findArguments(), ['-dopcache.enable=0', '-dvariables_order=EGPCS', '-S', $ip.':'.$port])); |
| 45 |
$process->setWorkingDirectory($workingDirectory); |
| 46 |
$process->start(); |
| 47 |
self::$process[$port] = $process; |
| 48 |
|
| 49 |
do { |
| 50 |
usleep(50000); |
| 51 |
} while (!@fopen('http://'.$ip.':'.$port, 'r')); |
| 52 |
|
| 53 |
return $process; |
| 54 |
} |
| 55 |
|
| 56 |
public static function stop(int $port = 8057) |
| 57 |
{ |
| 58 |
if (isset(self::$process[$port])) { |
| 59 |
self::$process[$port]->stop(); |
| 60 |
} |
| 61 |
} |
| 62 |
} |