Skip to content

Commit 0d8f449

Browse files
committed
Back port descriptor fix
1 parent f632832 commit 0d8f449

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/MockWebServer.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class MockWebServer {
2727
*/
2828
private $process;
2929

30+
/**
31+
* Contains the descriptors for the process after it has been started
32+
*
33+
* @var resource[]
34+
*/
35+
private $descriptors = [];
36+
3037
/**
3138
* TestWebServer constructor.
3239
*
@@ -67,7 +74,7 @@ public function start() : void {
6774

6875
InternalServer::incrementRequestCounter($this->tmpDir, 0);
6976

70-
$this->process = $this->startServer($fullCmd);
77+
[ $this->process, $this->descriptors ] = $this->startServer($fullCmd);
7178

7279
for( $i = 0; $i <= 20; $i++ ) {
7380
usleep(100000);
@@ -123,6 +130,10 @@ public function stop() : void {
123130
usleep(10000);
124131
}
125132
}
133+
134+
foreach( $this->descriptors as $descriptor ) {
135+
@fclose($descriptor);
136+
}
126137
}
127138

128139
/**
@@ -275,9 +286,9 @@ private function isWindowsPlatform() : bool {
275286
}
276287

277288
/**
278-
* @return resource
289+
* @return array{resource, resource[]}
279290
*/
280-
private function startServer( string $fullCmd ) {
291+
private function startServer( string $fullCmd ) : array {
281292
if( !$this->isWindowsPlatform() ) {
282293
// We need to prefix exec to get the correct process http://php.net/manual/ru/function.proc-get-status.php#93382
283294
$fullCmd = 'exec ' . $fullCmd;
@@ -287,14 +298,13 @@ private function startServer( string $fullCmd ) {
287298
$env = null;
288299
$cwd = null;
289300

290-
$stdin = fopen('php://stdin', 'rb');
291301
$stdoutf = tempnam(sys_get_temp_dir(), 'MockWebServer.stdout');
292302
$stderrf = tempnam(sys_get_temp_dir(), 'MockWebServer.stderr');
293303

294304
$descriptorSpec = [
295-
0 => $stdin,
296-
1 => [ 'file', $stdoutf, 'a' ],
297-
2 => [ 'file', $stderrf, 'a' ],
305+
0 => fopen('php://stdin', 'rb'),
306+
1 => fopen($stdoutf, 'a'),
307+
2 => fopen($stderrf, 'a'),
298308
];
299309

300310
$process = proc_open($fullCmd, $descriptorSpec, $pipes, $cwd, $env, [
@@ -303,7 +313,7 @@ private function startServer( string $fullCmd ) {
303313
]);
304314

305315
if( is_resource($process) ) {
306-
return $process;
316+
return [ $process, $descriptorSpec ];
307317
}
308318

309319
throw new Exceptions\ServerException('Error starting server');

0 commit comments

Comments
 (0)