-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebServer.php
168 lines (131 loc) · 4.79 KB
/
WebServer.php
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace VisualCraft\Bundle\WebServerBundle;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
/**
* Manages a local HTTP web server.
*/
class WebServer
{
const STARTED = 0;
const STOPPED = 1;
private $pidFileDirectory;
public function __construct(string $pidFileDirectory = null)
{
$this->pidFileDirectory = $pidFileDirectory;
}
public function run(WebServerConfig $config, $disableOutput = true, callable $callback = null)
{
if ($this->isRunning()) {
throw new \RuntimeException(sprintf('A process is already listening on http://%s.', $config->getAddress()));
}
$process = $this->createServerProcess($config);
if ($disableOutput) {
$process->disableOutput();
$callback = null;
} else {
try {
$process->setTty(true);
$callback = null;
} catch (RuntimeException $e) {
}
}
$process->run($callback);
if (!$process->isSuccessful()) {
$error = 'Server terminated unexpectedly.';
if ($process->isOutputDisabled()) {
$error .= ' Run the command again with -v option for more details.';
}
throw new \RuntimeException($error);
}
}
public function start(WebServerConfig $config, $pidFile = null)
{
$pidFile = $pidFile ?: $this->getDefaultPidFile();
if ($this->isRunning($pidFile)) {
throw new \RuntimeException(sprintf('A process is already listening on http://%s.', $config->getAddress()));
}
$pid = pcntl_fork();
if ($pid < 0) {
throw new \RuntimeException('Unable to start the server process.');
}
if ($pid > 0) {
return self::STARTED;
}
if (posix_setsid() < 0) {
throw new \RuntimeException('Unable to set the child process as session leader.');
}
$process = $this->createServerProcess($config);
$process->disableOutput();
$process->start();
if (!$process->isRunning()) {
throw new \RuntimeException('Unable to start the server process.');
}
file_put_contents($pidFile, $config->getAddress());
// stop the web server when the lock file is removed
while ($process->isRunning()) {
if (!file_exists($pidFile)) {
$process->stop();
}
sleep(1);
}
return self::STOPPED;
}
public function stop($pidFile = null)
{
$pidFile = $pidFile ?: $this->getDefaultPidFile();
if (!file_exists($pidFile)) {
throw new \RuntimeException('No web server is listening.');
}
unlink($pidFile);
}
public function getAddress($pidFile = null)
{
$pidFile = $pidFile ?: $this->getDefaultPidFile();
if (!file_exists($pidFile)) {
return false;
}
return file_get_contents($pidFile);
}
public function isRunning($pidFile = null)
{
$pidFile = $pidFile ?: $this->getDefaultPidFile();
if (!file_exists($pidFile)) {
return false;
}
$address = file_get_contents($pidFile);
$pos = strrpos($address, ':');
$hostname = substr($address, 0, $pos);
$port = substr($address, $pos + 1);
if (false !== $fp = @fsockopen($hostname, $port, $errno, $errstr, 1)) {
fclose($fp);
return true;
}
unlink($pidFile);
return false;
}
private function createServerProcess(WebServerConfig $config): Process
{
$finder = new PhpExecutableFinder();
if (false === $binary = $finder->find(false)) {
throw new \RuntimeException('Unable to find the PHP binary.');
}
$xdebugArgs = ini_get('xdebug.profiler_enable_trigger') ? ['-dxdebug.profiler_enable_trigger=1'] : [];
$process = new Process(array_merge([$binary], $finder->findArguments(), $xdebugArgs, ['-dvariables_order=EGPCS', '-S', $config->getAddress(), $config->getRouter()]));
$process->setWorkingDirectory($config->getDocumentRoot());
$process->setTimeout(null);
if (\in_array('APP_ENV', explode(',', getenv('SYMFONY_DOTENV_VARS')))) {
$process->setEnv(['APP_ENV' => false]);
if (!method_exists(Process::class, 'fromShellCommandline')) {
// Symfony 3.4 does not inherit env vars by default:
$process->inheritEnvironmentVariables();
}
}
return $process;
}
private function getDefaultPidFile(): string
{
return ($this->pidFileDirectory ?? getcwd()).'/.web-server-pid';
}
}