Passed
Push — master ( 6bc1c8...8ff536 )
by Morris
12:21
created
apps/dav/lib/Upload/AssemblyStream.php 2 patches
Indentation   +260 added lines, -260 removed lines patch added patch discarded remove patch
@@ -39,264 +39,264 @@
 block discarded – undo
39 39
  */
40 40
 class AssemblyStream implements \Icewind\Streams\File {
41 41
 
42
-	/** @var resource */
43
-	private $context;
44
-
45
-	/** @var IFile[] */
46
-	private $nodes;
47
-
48
-	/** @var int */
49
-	private $pos = 0;
50
-
51
-	/** @var int */
52
-	private $size = 0;
53
-
54
-	/** @var resource */
55
-	private $currentStream = null;
56
-
57
-	/** @var int */
58
-	private $currentNode = 0;
59
-
60
-	/** @var int */
61
-	private $currentNodeRead = 0;
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');
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
-	}
84
-
85
-	/**
86
-	 * @param int $offset
87
-	 * @param int $whence
88
-	 * @return bool
89
-	 */
90
-	public function stream_seek($offset, $whence = SEEK_SET) {
91
-		if ($whence === SEEK_CUR) {
92
-			$offset = $this->stream_tell() + $offset;
93
-		} else if ($whence === SEEK_END) {
94
-			$offset = $this->size + $offset;
95
-		}
96
-
97
-		if ($offset > $this->size) {
98
-			return false;
99
-		}
100
-
101
-		$nodeIndex = 0;
102
-		$nodeStart = 0;
103
-		while (true) {
104
-			if (!isset($this->nodes[$nodeIndex + 1])) {
105
-				break;
106
-			}
107
-			$node = $this->nodes[$nodeIndex];
108
-			if ($nodeStart + $node->getSize() > $offset) {
109
-				break;
110
-			}
111
-			$nodeIndex++;
112
-			$nodeStart += $node->getSize();
113
-		}
114
-
115
-		$stream = $this->getStream($this->nodes[$nodeIndex]);
116
-		$nodeOffset = $offset - $nodeStart;
117
-		if(fseek($stream, $nodeOffset) === -1) {
118
-			return false;
119
-		}
120
-		$this->currentNode = $nodeIndex;
121
-		$this->currentNodeRead = $nodeOffset;
122
-		$this->currentStream = $stream;
123
-		$this->pos = $offset;
124
-
125
-		return true;
126
-	}
127
-
128
-	/**
129
-	 * @return int
130
-	 */
131
-	public function stream_tell() {
132
-		return $this->pos;
133
-	}
134
-
135
-	/**
136
-	 * @param int $count
137
-	 * @return string
138
-	 */
139
-	public function stream_read($count) {
140
-		if (is_null($this->currentStream)) {
141
-			if ($this->currentNode < count($this->nodes)) {
142
-				$this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
143
-			} else {
144
-				return '';
145
-			}
146
-		}
147
-
148
-		do {
149
-			$data = fread($this->currentStream, $count);
150
-			$read = strlen($data);
151
-			$this->currentNodeRead += $read;
152
-
153
-			if (feof($this->currentStream)) {
154
-				fclose($this->currentStream);
155
-				$currentNodeSize = $this->nodes[$this->currentNode]->getSize();
156
-				if ($this->currentNodeRead < $currentNodeSize) {
157
-					throw new \Exception('Stream from assembly node shorter than expected, got ' . $this->currentNodeRead . ' bytes, expected ' . $currentNodeSize);
158
-				}
159
-				$this->currentNode++;
160
-				$this->currentNodeRead = 0;
161
-				if ($this->currentNode < count($this->nodes)) {
162
-					$this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
163
-				} else {
164
-					$this->currentStream = null;
165
-				}
166
-			}
167
-			// if no data read, try again with the next node because
168
-			// returning empty data can make the caller think there is no more
169
-			// data left to read
170
-		} while ($read === 0 && !is_null($this->currentStream));
171
-
172
-		// update position
173
-		$this->pos += $read;
174
-		return $data;
175
-	}
176
-
177
-	/**
178
-	 * @param string $data
179
-	 * @return int
180
-	 */
181
-	public function stream_write($data) {
182
-		return false;
183
-	}
184
-
185
-	/**
186
-	 * @param int $option
187
-	 * @param int $arg1
188
-	 * @param int $arg2
189
-	 * @return bool
190
-	 */
191
-	public function stream_set_option($option, $arg1, $arg2) {
192
-		return false;
193
-	}
194
-
195
-	/**
196
-	 * @param int $size
197
-	 * @return bool
198
-	 */
199
-	public function stream_truncate($size) {
200
-		return false;
201
-	}
202
-
203
-	/**
204
-	 * @return array
205
-	 */
206
-	public function stream_stat() {
207
-		return [
208
-			'size' => $this->size,
209
-		];
210
-	}
211
-
212
-	/**
213
-	 * @param int $operation
214
-	 * @return bool
215
-	 */
216
-	public function stream_lock($operation) {
217
-		return false;
218
-	}
219
-
220
-	/**
221
-	 * @return bool
222
-	 */
223
-	public function stream_flush() {
224
-		return false;
225
-	}
226
-
227
-	/**
228
-	 * @return bool
229
-	 */
230
-	public function stream_eof() {
231
-		return $this->pos >= $this->size || ($this->currentNode >= count($this->nodes) && $this->currentNode === null);
232
-	}
233
-
234
-	/**
235
-	 * @return bool
236
-	 */
237
-	public function stream_close() {
238
-		return true;
239
-	}
240
-
241
-
242
-	/**
243
-	 * Load the source from the stream context and return the context options
244
-	 *
245
-	 * @param string $name
246
-	 * @return array
247
-	 * @throws \BadMethodCallException
248
-	 */
249
-	protected function loadContext($name) {
250
-		$context = stream_context_get_options($this->context);
251
-		if (isset($context[$name])) {
252
-			$context = $context[$name];
253
-		} else {
254
-			throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
255
-		}
256
-		if (isset($context['nodes']) and is_array($context['nodes'])) {
257
-			$this->nodes = $context['nodes'];
258
-		} else {
259
-			throw new \BadMethodCallException('Invalid context, nodes not set');
260
-		}
261
-		return $context;
262
-	}
263
-
264
-	/**
265
-	 * @param IFile[] $nodes
266
-	 * @return resource
267
-	 *
268
-	 * @throws \BadMethodCallException
269
-	 */
270
-	public static function wrap(array $nodes) {
271
-		$context = stream_context_create([
272
-			'assembly' => [
273
-				'nodes' => $nodes
274
-			]
275
-		]);
276
-		stream_wrapper_register('assembly', self::class);
277
-		try {
278
-			$wrapped = fopen('assembly://', 'r', null, $context);
279
-		} catch (\BadMethodCallException $e) {
280
-			stream_wrapper_unregister('assembly');
281
-			throw $e;
282
-		}
283
-		stream_wrapper_unregister('assembly');
284
-		return $wrapped;
285
-	}
286
-
287
-	/**
288
-	 * @param IFile $node
289
-	 * @return resource
290
-	 */
291
-	private function getStream(IFile $node) {
292
-		$data = $node->get();
293
-		if (is_resource($data)) {
294
-			return $data;
295
-		} else {
296
-			$tmp = fopen('php://temp', 'w+');
297
-			fwrite($tmp, $data);
298
-			rewind($tmp);
299
-			return $tmp;
300
-		}
301
-	}
42
+    /** @var resource */
43
+    private $context;
44
+
45
+    /** @var IFile[] */
46
+    private $nodes;
47
+
48
+    /** @var int */
49
+    private $pos = 0;
50
+
51
+    /** @var int */
52
+    private $size = 0;
53
+
54
+    /** @var resource */
55
+    private $currentStream = null;
56
+
57
+    /** @var int */
58
+    private $currentNode = 0;
59
+
60
+    /** @var int */
61
+    private $currentNodeRead = 0;
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');
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
+    }
84
+
85
+    /**
86
+     * @param int $offset
87
+     * @param int $whence
88
+     * @return bool
89
+     */
90
+    public function stream_seek($offset, $whence = SEEK_SET) {
91
+        if ($whence === SEEK_CUR) {
92
+            $offset = $this->stream_tell() + $offset;
93
+        } else if ($whence === SEEK_END) {
94
+            $offset = $this->size + $offset;
95
+        }
96
+
97
+        if ($offset > $this->size) {
98
+            return false;
99
+        }
100
+
101
+        $nodeIndex = 0;
102
+        $nodeStart = 0;
103
+        while (true) {
104
+            if (!isset($this->nodes[$nodeIndex + 1])) {
105
+                break;
106
+            }
107
+            $node = $this->nodes[$nodeIndex];
108
+            if ($nodeStart + $node->getSize() > $offset) {
109
+                break;
110
+            }
111
+            $nodeIndex++;
112
+            $nodeStart += $node->getSize();
113
+        }
114
+
115
+        $stream = $this->getStream($this->nodes[$nodeIndex]);
116
+        $nodeOffset = $offset - $nodeStart;
117
+        if(fseek($stream, $nodeOffset) === -1) {
118
+            return false;
119
+        }
120
+        $this->currentNode = $nodeIndex;
121
+        $this->currentNodeRead = $nodeOffset;
122
+        $this->currentStream = $stream;
123
+        $this->pos = $offset;
124
+
125
+        return true;
126
+    }
127
+
128
+    /**
129
+     * @return int
130
+     */
131
+    public function stream_tell() {
132
+        return $this->pos;
133
+    }
134
+
135
+    /**
136
+     * @param int $count
137
+     * @return string
138
+     */
139
+    public function stream_read($count) {
140
+        if (is_null($this->currentStream)) {
141
+            if ($this->currentNode < count($this->nodes)) {
142
+                $this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
143
+            } else {
144
+                return '';
145
+            }
146
+        }
147
+
148
+        do {
149
+            $data = fread($this->currentStream, $count);
150
+            $read = strlen($data);
151
+            $this->currentNodeRead += $read;
152
+
153
+            if (feof($this->currentStream)) {
154
+                fclose($this->currentStream);
155
+                $currentNodeSize = $this->nodes[$this->currentNode]->getSize();
156
+                if ($this->currentNodeRead < $currentNodeSize) {
157
+                    throw new \Exception('Stream from assembly node shorter than expected, got ' . $this->currentNodeRead . ' bytes, expected ' . $currentNodeSize);
158
+                }
159
+                $this->currentNode++;
160
+                $this->currentNodeRead = 0;
161
+                if ($this->currentNode < count($this->nodes)) {
162
+                    $this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
163
+                } else {
164
+                    $this->currentStream = null;
165
+                }
166
+            }
167
+            // if no data read, try again with the next node because
168
+            // returning empty data can make the caller think there is no more
169
+            // data left to read
170
+        } while ($read === 0 && !is_null($this->currentStream));
171
+
172
+        // update position
173
+        $this->pos += $read;
174
+        return $data;
175
+    }
176
+
177
+    /**
178
+     * @param string $data
179
+     * @return int
180
+     */
181
+    public function stream_write($data) {
182
+        return false;
183
+    }
184
+
185
+    /**
186
+     * @param int $option
187
+     * @param int $arg1
188
+     * @param int $arg2
189
+     * @return bool
190
+     */
191
+    public function stream_set_option($option, $arg1, $arg2) {
192
+        return false;
193
+    }
194
+
195
+    /**
196
+     * @param int $size
197
+     * @return bool
198
+     */
199
+    public function stream_truncate($size) {
200
+        return false;
201
+    }
202
+
203
+    /**
204
+     * @return array
205
+     */
206
+    public function stream_stat() {
207
+        return [
208
+            'size' => $this->size,
209
+        ];
210
+    }
211
+
212
+    /**
213
+     * @param int $operation
214
+     * @return bool
215
+     */
216
+    public function stream_lock($operation) {
217
+        return false;
218
+    }
219
+
220
+    /**
221
+     * @return bool
222
+     */
223
+    public function stream_flush() {
224
+        return false;
225
+    }
226
+
227
+    /**
228
+     * @return bool
229
+     */
230
+    public function stream_eof() {
231
+        return $this->pos >= $this->size || ($this->currentNode >= count($this->nodes) && $this->currentNode === null);
232
+    }
233
+
234
+    /**
235
+     * @return bool
236
+     */
237
+    public function stream_close() {
238
+        return true;
239
+    }
240
+
241
+
242
+    /**
243
+     * Load the source from the stream context and return the context options
244
+     *
245
+     * @param string $name
246
+     * @return array
247
+     * @throws \BadMethodCallException
248
+     */
249
+    protected function loadContext($name) {
250
+        $context = stream_context_get_options($this->context);
251
+        if (isset($context[$name])) {
252
+            $context = $context[$name];
253
+        } else {
254
+            throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
255
+        }
256
+        if (isset($context['nodes']) and is_array($context['nodes'])) {
257
+            $this->nodes = $context['nodes'];
258
+        } else {
259
+            throw new \BadMethodCallException('Invalid context, nodes not set');
260
+        }
261
+        return $context;
262
+    }
263
+
264
+    /**
265
+     * @param IFile[] $nodes
266
+     * @return resource
267
+     *
268
+     * @throws \BadMethodCallException
269
+     */
270
+    public static function wrap(array $nodes) {
271
+        $context = stream_context_create([
272
+            'assembly' => [
273
+                'nodes' => $nodes
274
+            ]
275
+        ]);
276
+        stream_wrapper_register('assembly', self::class);
277
+        try {
278
+            $wrapped = fopen('assembly://', 'r', null, $context);
279
+        } catch (\BadMethodCallException $e) {
280
+            stream_wrapper_unregister('assembly');
281
+            throw $e;
282
+        }
283
+        stream_wrapper_unregister('assembly');
284
+        return $wrapped;
285
+    }
286
+
287
+    /**
288
+     * @param IFile $node
289
+     * @return resource
290
+     */
291
+    private function getStream(IFile $node) {
292
+        $data = $node->get();
293
+        if (is_resource($data)) {
294
+            return $data;
295
+        } else {
296
+            $tmp = fopen('php://temp', 'w+');
297
+            fwrite($tmp, $data);
298
+            rewind($tmp);
299
+            return $tmp;
300
+        }
301
+    }
302 302
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -72,11 +72,11 @@  discard block
 block discarded – undo
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;
@@ -114,7 +114,7 @@  discard block
 block discarded – undo
114 114
 
115 115
 		$stream = $this->getStream($this->nodes[$nodeIndex]);
116 116
 		$nodeOffset = $offset - $nodeStart;
117
-		if(fseek($stream, $nodeOffset) === -1) {
117
+		if (fseek($stream, $nodeOffset) === -1) {
118 118
 			return false;
119 119
 		}
120 120
 		$this->currentNode = $nodeIndex;
@@ -154,7 +154,7 @@  discard block
 block discarded – undo
154 154
 				fclose($this->currentStream);
155 155
 				$currentNodeSize = $this->nodes[$this->currentNode]->getSize();
156 156
 				if ($this->currentNodeRead < $currentNodeSize) {
157
-					throw new \Exception('Stream from assembly node shorter than expected, got ' . $this->currentNodeRead . ' bytes, expected ' . $currentNodeSize);
157
+					throw new \Exception('Stream from assembly node shorter than expected, got '.$this->currentNodeRead.' bytes, expected '.$currentNodeSize);
158 158
 				}
159 159
 				$this->currentNode++;
160 160
 				$this->currentNodeRead = 0;
@@ -251,7 +251,7 @@  discard block
 block discarded – undo
251 251
 		if (isset($context[$name])) {
252 252
 			$context = $context[$name];
253 253
 		} else {
254
-			throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
254
+			throw new \BadMethodCallException('Invalid context, "'.$name.'" options not set');
255 255
 		}
256 256
 		if (isset($context['nodes']) and is_array($context['nodes'])) {
257 257
 			$this->nodes = $context['nodes'];
Please login to merge, or discard this patch.