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