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