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