Total Complexity | 11 |
Total Lines | 70 |
Duplicated Lines | 0 % |
Coverage | 76.67% |
Changes | 0 |
1 | <?php |
||
9 | class Process implements ProcessInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var resource A 'process' resource |
||
13 | */ |
||
14 | private $process; |
||
15 | |||
16 | /** |
||
17 | * @var IPC - |
||
18 | */ |
||
19 | private $ipc; |
||
20 | |||
21 | /** |
||
22 | * @var bool |
||
23 | */ |
||
24 | private $running = true; |
||
25 | |||
26 | 1 | public function __construct(string $executable, array $overrideDescriptors = []) |
|
27 | { |
||
28 | 1 | $pipes = []; |
|
29 | 1 | $process = proc_open($executable, IPC::buildDescriptors($overrideDescriptors), $pipes); |
|
30 | 1 | if (!(is_resource($process) && get_resource_type($process) === "process")) { |
|
31 | throw new Exception\PinEntryException("Failed to start process"); |
||
32 | } |
||
33 | |||
34 | 1 | $this->ipc = new IPC($pipes); |
|
35 | 1 | $this->process = $process; |
|
36 | 1 | } |
|
37 | |||
38 | 1 | public function __destruct() |
|
39 | { |
||
40 | 1 | $this->close(); |
|
41 | 1 | } |
|
42 | |||
43 | 1 | public function close(): bool |
|
44 | { |
||
45 | 1 | if ($this->running) { |
|
46 | 1 | $this->stopRunning(); |
|
47 | } |
||
48 | 1 | return true; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * Called during shutdown routine. Per documentation, |
||
53 | * file handles should be closed before we call proc_close |
||
54 | */ |
||
55 | 1 | private function stopRunning() |
|
56 | { |
||
57 | 1 | $this->ipc->close(); |
|
58 | 1 | proc_close($this->process); |
|
59 | 1 | $this->running = false; |
|
60 | 1 | } |
|
61 | |||
62 | 1 | public function getStatus(): array |
|
69 | } |
||
70 | |||
71 | public function send(string $data) |
||
72 | { |
||
73 | $this->ipc->send($data); |
||
74 | } |
||
75 | |||
76 | public function recv(): string |
||
79 | } |
||
80 | } |
||
81 |