@@ -39,219 +39,219 @@ |
||
| 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 | - } |
|
| 160 | + /** |
|
| 161 | + * @return array |
|
| 162 | + */ |
|
| 163 | + public function stream_stat() { |
|
| 164 | + return []; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * @param int $operation |
|
| 169 | - * @return bool |
|
| 170 | - */ |
|
| 171 | - public function stream_lock($operation) { |
|
| 172 | - return false; |
|
| 173 | - } |
|
| 167 | + /** |
|
| 168 | + * @param int $operation |
|
| 169 | + * @return bool |
|
| 170 | + */ |
|
| 171 | + public function stream_lock($operation) { |
|
| 172 | + return false; |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - /** |
|
| 176 | - * @return bool |
|
| 177 | - */ |
|
| 178 | - public function stream_flush() { |
|
| 179 | - return false; |
|
| 180 | - } |
|
| 175 | + /** |
|
| 176 | + * @return bool |
|
| 177 | + */ |
|
| 178 | + public function stream_flush() { |
|
| 179 | + return false; |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * @return bool |
|
| 184 | - */ |
|
| 185 | - public function stream_eof() { |
|
| 186 | - return $this->pos >= $this->size || ($this->currentNode >= count($this->nodes) && $this->currentNode === null); |
|
| 187 | - } |
|
| 182 | + /** |
|
| 183 | + * @return bool |
|
| 184 | + */ |
|
| 185 | + public function stream_eof() { |
|
| 186 | + return $this->pos >= $this->size || ($this->currentNode >= count($this->nodes) && $this->currentNode === null); |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - /** |
|
| 190 | - * @return bool |
|
| 191 | - */ |
|
| 192 | - public function stream_close() { |
|
| 193 | - return true; |
|
| 194 | - } |
|
| 189 | + /** |
|
| 190 | + * @return bool |
|
| 191 | + */ |
|
| 192 | + public function stream_close() { |
|
| 193 | + return true; |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | 196 | |
| 197 | - /** |
|
| 198 | - * Load the source from the stream context and return the context options |
|
| 199 | - * |
|
| 200 | - * @param string $name |
|
| 201 | - * @return array |
|
| 202 | - * @throws \Exception |
|
| 203 | - */ |
|
| 204 | - protected function loadContext($name) { |
|
| 205 | - $context = stream_context_get_options($this->context); |
|
| 206 | - if (isset($context[$name])) { |
|
| 207 | - $context = $context[$name]; |
|
| 208 | - } else { |
|
| 209 | - throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set'); |
|
| 210 | - } |
|
| 211 | - if (isset($context['nodes']) and is_array($context['nodes'])) { |
|
| 212 | - $this->nodes = $context['nodes']; |
|
| 213 | - } else { |
|
| 214 | - throw new \BadMethodCallException('Invalid context, nodes not set'); |
|
| 215 | - } |
|
| 216 | - return $context; |
|
| 217 | - } |
|
| 197 | + /** |
|
| 198 | + * Load the source from the stream context and return the context options |
|
| 199 | + * |
|
| 200 | + * @param string $name |
|
| 201 | + * @return array |
|
| 202 | + * @throws \Exception |
|
| 203 | + */ |
|
| 204 | + protected function loadContext($name) { |
|
| 205 | + $context = stream_context_get_options($this->context); |
|
| 206 | + if (isset($context[$name])) { |
|
| 207 | + $context = $context[$name]; |
|
| 208 | + } else { |
|
| 209 | + throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set'); |
|
| 210 | + } |
|
| 211 | + if (isset($context['nodes']) and is_array($context['nodes'])) { |
|
| 212 | + $this->nodes = $context['nodes']; |
|
| 213 | + } else { |
|
| 214 | + throw new \BadMethodCallException('Invalid context, nodes not set'); |
|
| 215 | + } |
|
| 216 | + return $context; |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | - /** |
|
| 220 | - * @param IFile[] $nodes |
|
| 221 | - * @return resource |
|
| 222 | - * |
|
| 223 | - * @throws \BadMethodCallException |
|
| 224 | - */ |
|
| 225 | - public static function wrap(array $nodes) { |
|
| 226 | - $context = stream_context_create([ |
|
| 227 | - 'assembly' => [ |
|
| 228 | - 'nodes' => $nodes |
|
| 229 | - ] |
|
| 230 | - ]); |
|
| 231 | - stream_wrapper_register('assembly', self::class); |
|
| 232 | - try { |
|
| 233 | - $wrapped = fopen('assembly://', 'r', null, $context); |
|
| 234 | - } catch (\BadMethodCallException $e) { |
|
| 235 | - stream_wrapper_unregister('assembly'); |
|
| 236 | - throw $e; |
|
| 237 | - } |
|
| 238 | - stream_wrapper_unregister('assembly'); |
|
| 239 | - return $wrapped; |
|
| 240 | - } |
|
| 219 | + /** |
|
| 220 | + * @param IFile[] $nodes |
|
| 221 | + * @return resource |
|
| 222 | + * |
|
| 223 | + * @throws \BadMethodCallException |
|
| 224 | + */ |
|
| 225 | + public static function wrap(array $nodes) { |
|
| 226 | + $context = stream_context_create([ |
|
| 227 | + 'assembly' => [ |
|
| 228 | + 'nodes' => $nodes |
|
| 229 | + ] |
|
| 230 | + ]); |
|
| 231 | + stream_wrapper_register('assembly', self::class); |
|
| 232 | + try { |
|
| 233 | + $wrapped = fopen('assembly://', 'r', null, $context); |
|
| 234 | + } catch (\BadMethodCallException $e) { |
|
| 235 | + stream_wrapper_unregister('assembly'); |
|
| 236 | + throw $e; |
|
| 237 | + } |
|
| 238 | + stream_wrapper_unregister('assembly'); |
|
| 239 | + return $wrapped; |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - /** |
|
| 243 | - * @param IFile $node |
|
| 244 | - * @return resource |
|
| 245 | - */ |
|
| 246 | - private function getStream(IFile $node) { |
|
| 247 | - $data = $node->get(); |
|
| 248 | - if (is_resource($data)) { |
|
| 249 | - return $data; |
|
| 250 | - } else { |
|
| 251 | - $tmp = fopen('php://temp', 'w+'); |
|
| 252 | - fwrite($tmp, $data); |
|
| 253 | - rewind($tmp); |
|
| 254 | - return $tmp; |
|
| 255 | - } |
|
| 256 | - } |
|
| 242 | + /** |
|
| 243 | + * @param IFile $node |
|
| 244 | + * @return resource |
|
| 245 | + */ |
|
| 246 | + private function getStream(IFile $node) { |
|
| 247 | + $data = $node->get(); |
|
| 248 | + if (is_resource($data)) { |
|
| 249 | + return $data; |
|
| 250 | + } else { |
|
| 251 | + $tmp = fopen('php://temp', 'w+'); |
|
| 252 | + fwrite($tmp, $data); |
|
| 253 | + rewind($tmp); |
|
| 254 | + return $tmp; |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | 257 | } |
@@ -69,11 +69,11 @@ discard block |
||
| 69 | 69 | |
| 70 | 70 | $nodes = $this->nodes; |
| 71 | 71 | // http://stackoverflow.com/a/10985500 |
| 72 | - @usort($nodes, function (IFile $a, IFile $b) { |
|
| 72 | + @usort($nodes, function(IFile $a, IFile $b) { |
|
| 73 | 73 | return strnatcmp($a->getName(), $b->getName()); |
| 74 | 74 | }); |
| 75 | 75 | $this->nodes = array_values($nodes); |
| 76 | - $this->size = array_reduce($this->nodes, function ($size, IFile $file) { |
|
| 76 | + $this->size = array_reduce($this->nodes, function($size, IFile $file) { |
|
| 77 | 77 | return $size + $file->getSize(); |
| 78 | 78 | }, 0); |
| 79 | 79 | return true; |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | if (isset($context[$name])) { |
| 207 | 207 | $context = $context[$name]; |
| 208 | 208 | } else { |
| 209 | - throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set'); |
|
| 209 | + throw new \BadMethodCallException('Invalid context, "'.$name.'" options not set'); |
|
| 210 | 210 | } |
| 211 | 211 | if (isset($context['nodes']) and is_array($context['nodes'])) { |
| 212 | 212 | $this->nodes = $context['nodes']; |