|
14 | 14 |
|
15 | 15 | use React\ChildProcess\Process; |
16 | 16 | use React\EventLoop\LoopInterface; |
17 | | -use React\Promise\Deferred; |
| 17 | +use React\Promise\Promise; |
18 | 18 | use React\Promise\PromiseInterface; |
19 | 19 |
|
20 | 20 | const proc = __NAMESPACE__ . '\\proc'; |
|
41 | 41 | */ |
42 | 42 | function proc(string $process, ?LoopInterface $loop = null): PromiseInterface |
43 | 43 | { |
44 | | - $proc = new Process($process); |
45 | | - $result = new Deferred(); |
| 44 | + $proc = new Process($process); |
46 | 45 | $proc->start($loop); |
47 | 46 |
|
48 | | - if (!$proc->stdout->isReadable()) { |
49 | | - $result->reject( |
50 | | - new \Exception( |
51 | | - \sprintf('Could not process "%s"', $process) |
52 | | - ) |
53 | | - ); |
| 47 | + $data = ''; |
| 48 | + $action = function (string $chunk) use (&$data) { |
| 49 | + $data .= $chunk; |
| 50 | + }; |
54 | 51 |
|
55 | | - return $result; |
56 | | - } |
| 52 | + $proc->stdout->on('data', $action); |
57 | 53 |
|
58 | | - $proc->stdout->on( |
59 | | - 'data', |
60 | | - function ($chunk) use (&$result) { |
61 | | - $result->resolve($chunk); |
62 | | - } |
63 | | - ); |
64 | | - |
65 | | - // reject promise in the event of failure |
66 | | - $proc->stdout->on( |
67 | | - 'error', |
68 | | - function (\Throwable $err) use (&$result, &$proc) { |
69 | | - $result->reject($err); |
70 | | - } |
71 | | - ); |
72 | | - |
73 | | - // handle successful closure of the process stream |
74 | | - $proc->stdout->on( |
75 | | - 'end', |
76 | | - function () use (&$result) { |
77 | | - $result->resolve(true); |
78 | | - } |
79 | | - ); |
| 54 | + return new Promise( |
| 55 | + function (callable $resolve, callable $reject) use (&$data, $proc) { |
| 56 | + $proc->stdout->on( |
| 57 | + 'error', |
| 58 | + function (\Throwable $err) use ($reject) { |
| 59 | + $reject($err); |
| 60 | + } |
| 61 | + ); |
80 | 62 |
|
81 | | - // handle unsuccessful closure of process stream |
82 | | - $proc->stdout->on( |
83 | | - 'close', |
84 | | - function () use (&$result, $process) { |
85 | | - $result->reject( |
86 | | - new \Exception( |
87 | | - \sprintf('Closed process "%s"', $process) |
88 | | - ) |
| 63 | + $proc->stdout->on( |
| 64 | + 'end', |
| 65 | + function () use (&$data, $resolve) { |
| 66 | + $resolve($data); |
| 67 | + } |
89 | 68 | ); |
90 | 69 | } |
91 | 70 | ); |
92 | | - |
93 | | - return $result->promise(); |
94 | 71 | } |
0 commit comments