@@ -39,218 +39,218 @@ |
||
39 | 39 | */ |
40 | 40 | class AssemblyStream implements \Icewind\Streams\File { |
41 | 41 | |
42 | - /** @var resource */ |
|
43 | - private $context; |
|
42 | + /** @var resource */ |
|
43 | + private $context; |
|
44 | 44 | |
45 | - /** @var IFile[] */ |
|
46 | - private $nodes; |
|
45 | + /** @var IFile[] */ |
|
46 | + private $nodes; |
|
47 | 47 | |
48 | - /** @var int */ |
|
49 | - private $pos = 0; |
|
48 | + /** @var int */ |
|
49 | + private $pos = 0; |
|
50 | 50 | |
51 | - /** @var int */ |
|
52 | - private $size = 0; |
|
51 | + /** @var int */ |
|
52 | + private $size = 0; |
|
53 | 53 | |
54 | - /** @var resource */ |
|
55 | - private $currentStream = null; |
|
54 | + /** @var resource */ |
|
55 | + private $currentStream = null; |
|
56 | 56 | |
57 | - /** @var int */ |
|
58 | - private $currentNode = 0; |
|
57 | + /** @var int */ |
|
58 | + private $currentNode = 0; |
|
59 | 59 | |
60 | - /** |
|
61 | - * @param string $path |
|
62 | - * @param string $mode |
|
63 | - * @param int $options |
|
64 | - * @param string &$opened_path |
|
65 | - * @return bool |
|
66 | - */ |
|
67 | - public function stream_open($path, $mode, $options, &$opened_path) { |
|
68 | - $this->loadContext('assembly'); |
|
60 | + /** |
|
61 | + * @param string $path |
|
62 | + * @param string $mode |
|
63 | + * @param int $options |
|
64 | + * @param string &$opened_path |
|
65 | + * @return bool |
|
66 | + */ |
|
67 | + public function stream_open($path, $mode, $options, &$opened_path) { |
|
68 | + $this->loadContext('assembly'); |
|
69 | 69 | |
70 | - $nodes = $this->nodes; |
|
71 | - // http://stackoverflow.com/a/10985500 |
|
72 | - @usort($nodes, function (IFile $a, IFile $b) { |
|
73 | - return strnatcmp($a->getName(), $b->getName()); |
|
74 | - }); |
|
75 | - $this->nodes = array_values($nodes); |
|
76 | - if (count($this->nodes) > 0) { |
|
77 | - $this->currentStream = $this->getStream($this->nodes[0]); |
|
78 | - } |
|
79 | - $this->size = array_reduce($this->nodes, function ($size, IFile $file) { |
|
80 | - return $size + $file->getSize(); |
|
81 | - }, 0); |
|
82 | - return true; |
|
83 | - } |
|
70 | + $nodes = $this->nodes; |
|
71 | + // http://stackoverflow.com/a/10985500 |
|
72 | + @usort($nodes, function (IFile $a, IFile $b) { |
|
73 | + return strnatcmp($a->getName(), $b->getName()); |
|
74 | + }); |
|
75 | + $this->nodes = array_values($nodes); |
|
76 | + if (count($this->nodes) > 0) { |
|
77 | + $this->currentStream = $this->getStream($this->nodes[0]); |
|
78 | + } |
|
79 | + $this->size = array_reduce($this->nodes, function ($size, IFile $file) { |
|
80 | + return $size + $file->getSize(); |
|
81 | + }, 0); |
|
82 | + return true; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * @param string $offset |
|
87 | - * @param int $whence |
|
88 | - * @return bool |
|
89 | - */ |
|
90 | - public function stream_seek($offset, $whence = SEEK_SET) { |
|
91 | - return false; |
|
92 | - } |
|
85 | + /** |
|
86 | + * @param string $offset |
|
87 | + * @param int $whence |
|
88 | + * @return bool |
|
89 | + */ |
|
90 | + public function stream_seek($offset, $whence = SEEK_SET) { |
|
91 | + return false; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @return int |
|
96 | - */ |
|
97 | - public function stream_tell() { |
|
98 | - return $this->pos; |
|
99 | - } |
|
94 | + /** |
|
95 | + * @return int |
|
96 | + */ |
|
97 | + public function stream_tell() { |
|
98 | + return $this->pos; |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @param int $count |
|
103 | - * @return string |
|
104 | - */ |
|
105 | - public function stream_read($count) { |
|
106 | - if (is_null($this->currentStream)) { |
|
107 | - return ''; |
|
108 | - } |
|
101 | + /** |
|
102 | + * @param int $count |
|
103 | + * @return string |
|
104 | + */ |
|
105 | + public function stream_read($count) { |
|
106 | + if (is_null($this->currentStream)) { |
|
107 | + return ''; |
|
108 | + } |
|
109 | 109 | |
110 | - do { |
|
111 | - $data = fread($this->currentStream, $count); |
|
112 | - $read = strlen($data); |
|
110 | + do { |
|
111 | + $data = fread($this->currentStream, $count); |
|
112 | + $read = strlen($data); |
|
113 | 113 | |
114 | - if (feof($this->currentStream)) { |
|
115 | - fclose($this->currentStream); |
|
116 | - $this->currentNode++; |
|
117 | - if ($this->currentNode < count($this->nodes)) { |
|
118 | - $this->currentStream = $this->getStream($this->nodes[$this->currentNode]); |
|
119 | - } else { |
|
120 | - $this->currentStream = null; |
|
121 | - } |
|
122 | - } |
|
123 | - // if no data read, try again with the next node because |
|
124 | - // returning empty data can make the caller think there is no more |
|
125 | - // data left to read |
|
126 | - } while ($read === 0 && !is_null($this->currentStream)); |
|
114 | + if (feof($this->currentStream)) { |
|
115 | + fclose($this->currentStream); |
|
116 | + $this->currentNode++; |
|
117 | + if ($this->currentNode < count($this->nodes)) { |
|
118 | + $this->currentStream = $this->getStream($this->nodes[$this->currentNode]); |
|
119 | + } else { |
|
120 | + $this->currentStream = null; |
|
121 | + } |
|
122 | + } |
|
123 | + // if no data read, try again with the next node because |
|
124 | + // returning empty data can make the caller think there is no more |
|
125 | + // data left to read |
|
126 | + } while ($read === 0 && !is_null($this->currentStream)); |
|
127 | 127 | |
128 | - // update position |
|
129 | - $this->pos += $read; |
|
130 | - return $data; |
|
131 | - } |
|
128 | + // update position |
|
129 | + $this->pos += $read; |
|
130 | + return $data; |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * @param string $data |
|
135 | - * @return int |
|
136 | - */ |
|
137 | - public function stream_write($data) { |
|
138 | - return false; |
|
139 | - } |
|
133 | + /** |
|
134 | + * @param string $data |
|
135 | + * @return int |
|
136 | + */ |
|
137 | + public function stream_write($data) { |
|
138 | + return false; |
|
139 | + } |
|
140 | 140 | |
141 | - /** |
|
142 | - * @param int $option |
|
143 | - * @param int $arg1 |
|
144 | - * @param int $arg2 |
|
145 | - * @return bool |
|
146 | - */ |
|
147 | - public function stream_set_option($option, $arg1, $arg2) { |
|
148 | - return false; |
|
149 | - } |
|
141 | + /** |
|
142 | + * @param int $option |
|
143 | + * @param int $arg1 |
|
144 | + * @param int $arg2 |
|
145 | + * @return bool |
|
146 | + */ |
|
147 | + public function stream_set_option($option, $arg1, $arg2) { |
|
148 | + return false; |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * @param int $size |
|
153 | - * @return bool |
|
154 | - */ |
|
155 | - public function stream_truncate($size) { |
|
156 | - return false; |
|
157 | - } |
|
151 | + /** |
|
152 | + * @param int $size |
|
153 | + * @return bool |
|
154 | + */ |
|
155 | + public function stream_truncate($size) { |
|
156 | + return false; |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * @return array |
|
161 | - */ |
|
162 | - public function stream_stat() { |
|
163 | - return []; |
|
164 | - } |
|
159 | + /** |
|
160 | + * @return array |
|
161 | + */ |
|
162 | + public function stream_stat() { |
|
163 | + return []; |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * @param int $operation |
|
168 | - * @return bool |
|
169 | - */ |
|
170 | - public function stream_lock($operation) { |
|
171 | - return false; |
|
172 | - } |
|
166 | + /** |
|
167 | + * @param int $operation |
|
168 | + * @return bool |
|
169 | + */ |
|
170 | + public function stream_lock($operation) { |
|
171 | + return false; |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * @return bool |
|
176 | - */ |
|
177 | - public function stream_flush() { |
|
178 | - return false; |
|
179 | - } |
|
174 | + /** |
|
175 | + * @return bool |
|
176 | + */ |
|
177 | + public function stream_flush() { |
|
178 | + return false; |
|
179 | + } |
|
180 | 180 | |
181 | - /** |
|
182 | - * @return bool |
|
183 | - */ |
|
184 | - public function stream_eof() { |
|
185 | - return $this->pos >= $this->size || $this->currentStream === null; |
|
186 | - } |
|
181 | + /** |
|
182 | + * @return bool |
|
183 | + */ |
|
184 | + public function stream_eof() { |
|
185 | + return $this->pos >= $this->size || $this->currentStream === null; |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * @return bool |
|
190 | - */ |
|
191 | - public function stream_close() { |
|
192 | - return true; |
|
193 | - } |
|
188 | + /** |
|
189 | + * @return bool |
|
190 | + */ |
|
191 | + public function stream_close() { |
|
192 | + return true; |
|
193 | + } |
|
194 | 194 | |
195 | 195 | |
196 | - /** |
|
197 | - * Load the source from the stream context and return the context options |
|
198 | - * |
|
199 | - * @param string $name |
|
200 | - * @return array |
|
201 | - * @throws \Exception |
|
202 | - */ |
|
203 | - protected function loadContext($name) { |
|
204 | - $context = stream_context_get_options($this->context); |
|
205 | - if (isset($context[$name])) { |
|
206 | - $context = $context[$name]; |
|
207 | - } else { |
|
208 | - throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set'); |
|
209 | - } |
|
210 | - if (isset($context['nodes']) and is_array($context['nodes'])) { |
|
211 | - $this->nodes = $context['nodes']; |
|
212 | - } else { |
|
213 | - throw new \BadMethodCallException('Invalid context, nodes not set'); |
|
214 | - } |
|
215 | - return $context; |
|
216 | - } |
|
196 | + /** |
|
197 | + * Load the source from the stream context and return the context options |
|
198 | + * |
|
199 | + * @param string $name |
|
200 | + * @return array |
|
201 | + * @throws \Exception |
|
202 | + */ |
|
203 | + protected function loadContext($name) { |
|
204 | + $context = stream_context_get_options($this->context); |
|
205 | + if (isset($context[$name])) { |
|
206 | + $context = $context[$name]; |
|
207 | + } else { |
|
208 | + throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set'); |
|
209 | + } |
|
210 | + if (isset($context['nodes']) and is_array($context['nodes'])) { |
|
211 | + $this->nodes = $context['nodes']; |
|
212 | + } else { |
|
213 | + throw new \BadMethodCallException('Invalid context, nodes not set'); |
|
214 | + } |
|
215 | + return $context; |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
219 | - * @param IFile[] $nodes |
|
220 | - * @return resource |
|
221 | - * |
|
222 | - * @throws \BadMethodCallException |
|
223 | - */ |
|
224 | - public static function wrap(array $nodes) { |
|
225 | - $context = stream_context_create([ |
|
226 | - 'assembly' => [ |
|
227 | - 'nodes' => $nodes |
|
228 | - ] |
|
229 | - ]); |
|
230 | - stream_wrapper_register('assembly', self::class); |
|
231 | - try { |
|
232 | - $wrapped = fopen('assembly://', 'r', null, $context); |
|
233 | - } catch (\BadMethodCallException $e) { |
|
234 | - stream_wrapper_unregister('assembly'); |
|
235 | - throw $e; |
|
236 | - } |
|
237 | - stream_wrapper_unregister('assembly'); |
|
238 | - return $wrapped; |
|
239 | - } |
|
218 | + /** |
|
219 | + * @param IFile[] $nodes |
|
220 | + * @return resource |
|
221 | + * |
|
222 | + * @throws \BadMethodCallException |
|
223 | + */ |
|
224 | + public static function wrap(array $nodes) { |
|
225 | + $context = stream_context_create([ |
|
226 | + 'assembly' => [ |
|
227 | + 'nodes' => $nodes |
|
228 | + ] |
|
229 | + ]); |
|
230 | + stream_wrapper_register('assembly', self::class); |
|
231 | + try { |
|
232 | + $wrapped = fopen('assembly://', 'r', null, $context); |
|
233 | + } catch (\BadMethodCallException $e) { |
|
234 | + stream_wrapper_unregister('assembly'); |
|
235 | + throw $e; |
|
236 | + } |
|
237 | + stream_wrapper_unregister('assembly'); |
|
238 | + return $wrapped; |
|
239 | + } |
|
240 | 240 | |
241 | - /** |
|
242 | - * @param IFile $node |
|
243 | - * @return resource |
|
244 | - */ |
|
245 | - private function getStream(IFile $node) { |
|
246 | - $data = $node->get(); |
|
247 | - if (is_resource($data)) { |
|
248 | - return $data; |
|
249 | - } else { |
|
250 | - $tmp = fopen('php://temp', 'w+'); |
|
251 | - fwrite($tmp, $data); |
|
252 | - rewind($tmp); |
|
253 | - return $tmp; |
|
254 | - } |
|
255 | - } |
|
241 | + /** |
|
242 | + * @param IFile $node |
|
243 | + * @return resource |
|
244 | + */ |
|
245 | + private function getStream(IFile $node) { |
|
246 | + $data = $node->get(); |
|
247 | + if (is_resource($data)) { |
|
248 | + return $data; |
|
249 | + } else { |
|
250 | + $tmp = fopen('php://temp', 'w+'); |
|
251 | + fwrite($tmp, $data); |
|
252 | + rewind($tmp); |
|
253 | + return $tmp; |
|
254 | + } |
|
255 | + } |
|
256 | 256 | } |