1 | <?php |
||
7 | class DockerRawStream |
||
8 | { |
||
9 | const HEADER = 'application/vnd.docker.raw-stream'; |
||
10 | |||
11 | /** @var StreamInterface Stream for the response */ |
||
12 | protected $stream; |
||
13 | |||
14 | /** @var callable[] A list of callable to call when there is a stdin output */ |
||
15 | protected $onStdinCallables = []; |
||
16 | |||
17 | /** @var callable[] A list of callable to call when there is a stdout output */ |
||
18 | protected $onStdoutCallables = []; |
||
19 | |||
20 | /** @var callable[] A list of callable to call when there is a stderr output */ |
||
21 | protected $onStderrCallables = []; |
||
22 | |||
23 | 2 | public function __construct(StreamInterface $stream) |
|
27 | |||
28 | /** |
||
29 | * Add a callable to read stdin |
||
30 | * |
||
31 | * @param callable $callback |
||
32 | */ |
||
33 | public function onStdin(callable $callback) |
||
37 | |||
38 | /** |
||
39 | * Add a callable to read stdout |
||
40 | * |
||
41 | * @param callable $callback |
||
42 | */ |
||
43 | 2 | public function onStdout(callable $callback) |
|
47 | |||
48 | /** |
||
49 | * Add a callable to read stderr |
||
50 | * |
||
51 | * @param callable $callback |
||
52 | */ |
||
53 | public function onStderr(callable $callback) |
||
57 | |||
58 | /** |
||
59 | * Read a frame in the stream |
||
60 | */ |
||
61 | 2 | protected function readFrame() |
|
62 | { |
||
63 | 2 | $header = $this->forceRead(8); |
|
64 | |||
65 | 2 | if (strlen($header) < 8) { |
|
66 | return; |
||
67 | } |
||
68 | |||
69 | 2 | $decoded = unpack('C1type/C3/N1size', $header); |
|
70 | 2 | $output = $this->forceRead($decoded['size']); |
|
71 | 2 | $callbackList = []; |
|
72 | |||
73 | 2 | if ($decoded['type'] == 0) { |
|
74 | $callbackList = $this->onStdinCallables; |
||
75 | } |
||
76 | |||
77 | 2 | if ($decoded['type'] == 1) { |
|
78 | 2 | $callbackList = $this->onStdoutCallables; |
|
79 | 2 | } |
|
80 | |||
81 | 2 | if ($decoded['type'] == 2) { |
|
82 | $callbackList = $this->onStderrCallables; |
||
83 | } |
||
84 | |||
85 | 2 | foreach ($callbackList as $callback) { |
|
86 | 2 | $callback($output); |
|
87 | 2 | } |
|
88 | 2 | } |
|
89 | |||
90 | /** |
||
91 | * Force to have something of the expected size (block) |
||
92 | * |
||
93 | * @param $length |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | 2 | private function forceRead($length) |
|
107 | |||
108 | /** |
||
109 | * Wait for stream to finish and call callables if defined |
||
110 | */ |
||
111 | 2 | public function wait() |
|
117 | } |
||
118 |