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