Completed
Pull Request — master (#5514)
by Robin
44:53 queued 24:22
created
apps/dav/lib/Upload/AssemblyStream.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -126,7 +126,7 @@
 block discarded – undo
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;
Please login to merge, or discard this patch.
Indentation   +214 added lines, -214 removed lines patch added patch discarded remove patch
@@ -36,218 +36,218 @@
 block discarded – undo
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
-		// http://stackoverflow.com/a/10985500
69
-		@usort($nodes, function (IFile $a, IFile $b) {
70
-			return strnatcmp($a->getName(), $b->getName());
71
-		});
72
-		$this->nodes = array_values($nodes);
73
-		if (count($this->nodes) > 0) {
74
-			$this->currentStream = $this->getStream($this->nodes[0]);
75
-		}
76
-		$this->size = array_reduce($this->nodes, function ($size, IFile $file) {
77
-			return $size + $file->getSize();
78
-		}, 0);
79
-		return true;
80
-	}
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
-	}
90
-
91
-	/**
92
-	 * @return int
93
-	 */
94
-	public function stream_tell() {
95
-		return $this->pos;
96
-	}
97
-
98
-	/**
99
-	 * @param int $count
100
-	 * @return string
101
-	 */
102
-	public function stream_read($count) {
103
-		if (is_null($this->currentStream)) {
104
-			return '';
105
-		}
106
-
107
-		do {
108
-			$data = fread($this->currentStream, $count);
109
-			$read = strlen($data);
110
-
111
-			if (feof($this->currentStream)) {
112
-				fclose($this->currentStream);
113
-				$this->currentNode++;
114
-				if ($this->currentNode < count($this->nodes)) {
115
-					$this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
116
-				} else {
117
-					$this->currentStream = null;
118
-				}
119
-			}
120
-			// if no data read, try again with the next node because
121
-			// returning empty data can make the caller think there is no more
122
-			// data left to read
123
-		} while ($read === 0 && !is_null($this->currentStream));
124
-
125
-		// update position
126
-		$this->pos += $read;
127
-		return $data;
128
-	}
129
-
130
-	/**
131
-	 * @param string $data
132
-	 * @return int
133
-	 */
134
-	public function stream_write($data) {
135
-		return false;
136
-	}
137
-
138
-	/**
139
-	 * @param int $option
140
-	 * @param int $arg1
141
-	 * @param int $arg2
142
-	 * @return bool
143
-	 */
144
-	public function stream_set_option($option, $arg1, $arg2) {
145
-		return false;
146
-	}
147
-
148
-	/**
149
-	 * @param int $size
150
-	 * @return bool
151
-	 */
152
-	public function stream_truncate($size) {
153
-		return false;
154
-	}
155
-
156
-	/**
157
-	 * @return array
158
-	 */
159
-	public function stream_stat() {
160
-		return [];
161
-	}
162
-
163
-	/**
164
-	 * @param int $operation
165
-	 * @return bool
166
-	 */
167
-	public function stream_lock($operation) {
168
-		return false;
169
-	}
170
-
171
-	/**
172
-	 * @return bool
173
-	 */
174
-	public function stream_flush() {
175
-		return false;
176
-	}
177
-
178
-	/**
179
-	 * @return bool
180
-	 */
181
-	public function stream_eof() {
182
-		return $this->pos >= $this->size;
183
-	}
184
-
185
-	/**
186
-	 * @return bool
187
-	 */
188
-	public function stream_close() {
189
-		return true;
190
-	}
191
-
192
-
193
-	/**
194
-	 * Load the source from the stream context and return the context options
195
-	 *
196
-	 * @param string $name
197
-	 * @return array
198
-	 * @throws \Exception
199
-	 */
200
-	protected function loadContext($name) {
201
-		$context = stream_context_get_options($this->context);
202
-		if (isset($context[$name])) {
203
-			$context = $context[$name];
204
-		} else {
205
-			throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
206
-		}
207
-		if (isset($context['nodes']) and is_array($context['nodes'])) {
208
-			$this->nodes = $context['nodes'];
209
-		} else {
210
-			throw new \BadMethodCallException('Invalid context, nodes not set');
211
-		}
212
-		return $context;
213
-	}
214
-
215
-	/**
216
-	 * @param IFile[] $nodes
217
-	 * @return resource
218
-	 *
219
-	 * @throws \BadMethodCallException
220
-	 */
221
-	public static function wrap(array $nodes) {
222
-		$context = stream_context_create([
223
-			'assembly' => [
224
-				'nodes' => $nodes
225
-			]
226
-		]);
227
-		stream_wrapper_register('assembly', self::class);
228
-		try {
229
-			$wrapped = fopen('assembly://', 'r', null, $context);
230
-		} catch (\BadMethodCallException $e) {
231
-			stream_wrapper_unregister('assembly');
232
-			throw $e;
233
-		}
234
-		stream_wrapper_unregister('assembly');
235
-		return $wrapped;
236
-	}
237
-
238
-	/**
239
-	 * @param IFile $node
240
-	 * @return resource
241
-	 */
242
-	private function getStream(IFile $node) {
243
-		$data = $node->get();
244
-		if (is_resource($data)) {
245
-			return $data;
246
-		} else {
247
-			$tmp = fopen('php://temp', 'w+');
248
-			fwrite($tmp, $data);
249
-			rewind($tmp);
250
-			return $tmp;
251
-		}
252
-	}
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
+        // http://stackoverflow.com/a/10985500
69
+        @usort($nodes, function (IFile $a, IFile $b) {
70
+            return strnatcmp($a->getName(), $b->getName());
71
+        });
72
+        $this->nodes = array_values($nodes);
73
+        if (count($this->nodes) > 0) {
74
+            $this->currentStream = $this->getStream($this->nodes[0]);
75
+        }
76
+        $this->size = array_reduce($this->nodes, function ($size, IFile $file) {
77
+            return $size + $file->getSize();
78
+        }, 0);
79
+        return true;
80
+    }
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
+    }
90
+
91
+    /**
92
+     * @return int
93
+     */
94
+    public function stream_tell() {
95
+        return $this->pos;
96
+    }
97
+
98
+    /**
99
+     * @param int $count
100
+     * @return string
101
+     */
102
+    public function stream_read($count) {
103
+        if (is_null($this->currentStream)) {
104
+            return '';
105
+        }
106
+
107
+        do {
108
+            $data = fread($this->currentStream, $count);
109
+            $read = strlen($data);
110
+
111
+            if (feof($this->currentStream)) {
112
+                fclose($this->currentStream);
113
+                $this->currentNode++;
114
+                if ($this->currentNode < count($this->nodes)) {
115
+                    $this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
116
+                } else {
117
+                    $this->currentStream = null;
118
+                }
119
+            }
120
+            // if no data read, try again with the next node because
121
+            // returning empty data can make the caller think there is no more
122
+            // data left to read
123
+        } while ($read === 0 && !is_null($this->currentStream));
124
+
125
+        // update position
126
+        $this->pos += $read;
127
+        return $data;
128
+    }
129
+
130
+    /**
131
+     * @param string $data
132
+     * @return int
133
+     */
134
+    public function stream_write($data) {
135
+        return false;
136
+    }
137
+
138
+    /**
139
+     * @param int $option
140
+     * @param int $arg1
141
+     * @param int $arg2
142
+     * @return bool
143
+     */
144
+    public function stream_set_option($option, $arg1, $arg2) {
145
+        return false;
146
+    }
147
+
148
+    /**
149
+     * @param int $size
150
+     * @return bool
151
+     */
152
+    public function stream_truncate($size) {
153
+        return false;
154
+    }
155
+
156
+    /**
157
+     * @return array
158
+     */
159
+    public function stream_stat() {
160
+        return [];
161
+    }
162
+
163
+    /**
164
+     * @param int $operation
165
+     * @return bool
166
+     */
167
+    public function stream_lock($operation) {
168
+        return false;
169
+    }
170
+
171
+    /**
172
+     * @return bool
173
+     */
174
+    public function stream_flush() {
175
+        return false;
176
+    }
177
+
178
+    /**
179
+     * @return bool
180
+     */
181
+    public function stream_eof() {
182
+        return $this->pos >= $this->size;
183
+    }
184
+
185
+    /**
186
+     * @return bool
187
+     */
188
+    public function stream_close() {
189
+        return true;
190
+    }
191
+
192
+
193
+    /**
194
+     * Load the source from the stream context and return the context options
195
+     *
196
+     * @param string $name
197
+     * @return array
198
+     * @throws \Exception
199
+     */
200
+    protected function loadContext($name) {
201
+        $context = stream_context_get_options($this->context);
202
+        if (isset($context[$name])) {
203
+            $context = $context[$name];
204
+        } else {
205
+            throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
206
+        }
207
+        if (isset($context['nodes']) and is_array($context['nodes'])) {
208
+            $this->nodes = $context['nodes'];
209
+        } else {
210
+            throw new \BadMethodCallException('Invalid context, nodes not set');
211
+        }
212
+        return $context;
213
+    }
214
+
215
+    /**
216
+     * @param IFile[] $nodes
217
+     * @return resource
218
+     *
219
+     * @throws \BadMethodCallException
220
+     */
221
+    public static function wrap(array $nodes) {
222
+        $context = stream_context_create([
223
+            'assembly' => [
224
+                'nodes' => $nodes
225
+            ]
226
+        ]);
227
+        stream_wrapper_register('assembly', self::class);
228
+        try {
229
+            $wrapped = fopen('assembly://', 'r', null, $context);
230
+        } catch (\BadMethodCallException $e) {
231
+            stream_wrapper_unregister('assembly');
232
+            throw $e;
233
+        }
234
+        stream_wrapper_unregister('assembly');
235
+        return $wrapped;
236
+    }
237
+
238
+    /**
239
+     * @param IFile $node
240
+     * @return resource
241
+     */
242
+    private function getStream(IFile $node) {
243
+        $data = $node->get();
244
+        if (is_resource($data)) {
245
+            return $data;
246
+        } else {
247
+            $tmp = fopen('php://temp', 'w+');
248
+            fwrite($tmp, $data);
249
+            rewind($tmp);
250
+            return $tmp;
251
+        }
252
+    }
253 253
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -66,14 +66,14 @@  discard block
 block discarded – undo
66 66
 
67 67
 		$nodes = $this->nodes;
68 68
 		// http://stackoverflow.com/a/10985500
69
-		@usort($nodes, function (IFile $a, IFile $b) {
69
+		@usort($nodes, function(IFile $a, IFile $b) {
70 70
 			return strnatcmp($a->getName(), $b->getName());
71 71
 		});
72 72
 		$this->nodes = array_values($nodes);
73 73
 		if (count($this->nodes) > 0) {
74 74
 			$this->currentStream = $this->getStream($this->nodes[0]);
75 75
 		}
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;
@@ -202,7 +202,7 @@  discard block
 block discarded – undo
202 202
 		if (isset($context[$name])) {
203 203
 			$context = $context[$name];
204 204
 		} else {
205
-			throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
205
+			throw new \BadMethodCallException('Invalid context, "'.$name.'" options not set');
206 206
 		}
207 207
 		if (isset($context['nodes']) and is_array($context['nodes'])) {
208 208
 			$this->nodes = $context['nodes'];
Please login to merge, or discard this patch.