This repository was archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.php
More file actions
70 lines (58 loc) · 1.42 KB
/
Server.php
File metadata and controls
70 lines (58 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace x;
use RuntimeException;
class Server
{
/**
* @var string
*/
protected $lumenDirectory = __DIR__ . '/public/lumen.php';
/**
* @var int
*/
protected $port;
/**
* @var int
*/
protected $failures = 0;
/**
* @param $port
*/
public function __construct($port)
{
$this->port = $port;
}
public function start()
{
$pid = exec("php -S localhost:{$this->port} {$this->lumenDirectory} > /dev/null 2>&1 & echo $!");
$this->onShutdown($pid);
while (@file_get_contents("http://localhost:{$this->port}/") === false) {
if ($this->failures >= 3) {
print "Failed to listen on http://localhost:{$this->port}\n";
exit(1);
}
$this->failures++;
print "No response from test server.\n";
sleep(1);
}
}
/**
* @param int $pid
*/
public function onShutdown($pid)
{
register_shutdown_function(function () use ($pid) {
exec("kill $pid");
});
}
/**
* @param int $port
*/
public static function boot($port = 8888)
{
if (stripos(PHP_OS, 'WIN') !== false) {
throw new RuntimeException("You'll need to run the test server manually on Windows with 'php -S localhost:8888 /path/to/lumen'");
}
(new static($port))->start();
}
}