BuiltinServerFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 78
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A createInternalAsync() 0 36 2
A createInternalWithRetryAsync() 0 11 2
A createAsync() 0 4 1
A createMultipleAsync() 0 8 2
1
<?php
2
3
namespace mpyw\HyperBuiltinServer;
4
use mpyw\HyperBuiltinServer\Internal\BuiltinServer;
5
use React\EventLoop\LoopInterface;
6
use React\Stream\Stream;
7
use React\Promise\Deferred;
8
use React\Promise\RejectedPromise;
9
10
class BuiltinServerFactory
11
{
12
    protected $loop;
13
    protected $stderr;
14
    protected $php = PHP_BINARY;
15
    protected $retry = 5;
16
17
    public function __construct(LoopInterface $loop, $retry_count = 5, $php_command = PHP_BINARY)
18
    {
19
        $this->loop = $loop;
20
        $this->stderr = new Stream(fopen('php://stderr', 'wb'), $this->loop);
21
        $this->php = $php_command;
22
        $this->retry = $retry_count;
23
    }
24
25
    protected function createInternalAsync($host, $docroot, $router)
26
    {
27
        $deferred = new Deferred;
28
        $process = new BuiltinServer($host, $docroot, $router, $this->php);
29
30
        $process->start($this->loop);
31
        $process->on('exit', function ($code) use ($deferred) {
32
            $this->stderr->write("Process exit with code $code\n");
33
            $deferred->reject();
34
        });
35
36
        $process->stdin->close();
37
        $process->stdout->close();
38
        $process->stderr->on('data', function ($output) use ($deferred) {
39
            $this->stderr->write($output);
40
            $deferred->reject();
41
        });
42
43
        $timer = new Deferred;
44
        $this->loop->addTimer(0.05, function () use ($timer, $process) {
45
            if (DIRECTORY_SEPARATOR === '\\') {
46
                // Pipes opened by proc_open() can break stream_select() loop in Windows.
47
                // This fix might do the trick...
48
                $process->stderr->close();
49
            }
50
            $timer->resolve($process);
51
        });
52
53
        return \React\Promise\race([
54
            $deferred->promise(),
55
            $timer->promise(),
56
        ])->then(null, function () use ($process) {
57
            $process->terminate();
58
            return new RejectedPromise;
0 ignored issues
show
Deprecated Code introduced by
The class React\Promise\RejectedPromise has been deprecated with message: 2.8.0 External usage of RejectedPromise is deprecated, use `reject()` instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
59
        });
60
    }
61
62
    protected function createInternalWithRetryAsync($host, $docroot, $router, $retry)
63
    {
64
        return $this
65
        ->createInternalAsync($host, $docroot, $router)
66
        ->then(null, function () use ($host, $docroot, $router, $retry) {
67
            if ($retry < 1) {
68
                throw new \RuntimeException('Failed to launch server.');
69
            }
70
            return $this->createInternalWithRetryAsync($host, $docroot, $router, $retry - 1);
71
        });
72
    }
73
74
    public function createAsync($host = '127.0.0.1', $docroot = null, $router = null)
75
    {
76
        return $this->createInternalWithRetryAsync($host, $docroot, $router, $this->retry);
77
    }
78
79
    public function createMultipleAsync($n, $host = '127.0.0.1', $docroot = null, $router = null)
80
    {
81
        $promises = [];
82
        for ($i = 0; $i < $n; ++$i) {
83
            $promises[] = $this->createAsync($host, $docroot, $router);
84
        }
85
        return \React\Promise\all($promises);
86
    }
87
}
88