ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
MountManager.php
Go to the documentation of this file.
1<?php
2
3namespace League\Flysystem;
4
5use InvalidArgumentException;
9
47{
49
53 protected $filesystems = [];
54
62 public function __construct(array $filesystems = [])
63 {
65 }
66
76 public function mountFilesystems(array $filesystems)
77 {
78 foreach ($filesystems as $prefix => $filesystem) {
79 $this->mountFilesystem($prefix, $filesystem);
80 }
81
82 return $this;
83 }
84
95 public function mountFilesystem($prefix, FilesystemInterface $filesystem)
96 {
97 if ( ! is_string($prefix)) {
98 throw new InvalidArgumentException(__METHOD__ . ' expects argument #1 to be a string.');
99 }
100
101 $this->filesystems[$prefix] = $filesystem;
102
103 return $this;
104 }
105
115 public function getFilesystem($prefix)
116 {
117 if ( ! isset($this->filesystems[$prefix])) {
118 throw new FilesystemNotFoundException('No filesystem mounted with prefix ' . $prefix);
119 }
120
121 return $this->filesystems[$prefix];
122 }
123
133 public function filterPrefix(array $arguments)
134 {
135 if (empty($arguments)) {
136 throw new InvalidArgumentException('At least one argument needed');
137 }
138
139 $path = array_shift($arguments);
140
141 if ( ! is_string($path)) {
142 throw new InvalidArgumentException('First argument should be a string');
143 }
144
145 list($prefix, $path) = $this->getPrefixAndPath($path);
146 array_unshift($arguments, $path);
147
148 return [$prefix, $arguments];
149 }
150
160 public function listContents($directory = '', $recursive = false)
161 {
162 list($prefix, $directory) = $this->getPrefixAndPath($directory);
163 $filesystem = $this->getFilesystem($prefix);
164 $result = $filesystem->listContents($directory, $recursive);
165
166 foreach ($result as &$file) {
167 $file['filesystem'] = $prefix;
168 }
169
170 return $result;
171 }
172
184 public function __call($method, $arguments)
185 {
186 list($prefix, $arguments) = $this->filterPrefix($arguments);
187
188 return $this->invokePluginOnFilesystem($method, $arguments, $prefix);
189 }
190
201 public function copy($from, $to, array $config = [])
202 {
203 list($prefixFrom, $from) = $this->getPrefixAndPath($from);
204
205 $buffer = $this->getFilesystem($prefixFrom)->readStream($from);
206
207 if ($buffer === false) {
208 return false;
209 }
210
211 list($prefixTo, $to) = $this->getPrefixAndPath($to);
212
213 $result = $this->getFilesystem($prefixTo)->writeStream($to, $buffer, $config);
214
215 if (is_resource($buffer)) {
216 fclose($buffer);
217 }
218
219 return $result;
220 }
221
234 public function listWith(array $keys = [], $directory = '', $recursive = false)
235 {
236 list($prefix, $directory) = $this->getPrefixAndPath($directory);
237 $arguments = [$keys, $directory, $recursive];
238
239 return $this->invokePluginOnFilesystem('listWith', $arguments, $prefix);
240 }
241
254 public function move($from, $to, array $config = [])
255 {
256 $copied = $this->copy($from, $to, $config);
257
258 if ($copied) {
259 return $this->delete($from);
260 }
261
262 return false;
263 }
264
276 public function invokePluginOnFilesystem($method, $arguments, $prefix)
277 {
278 $filesystem = $this->getFilesystem($prefix);
279
280 try {
281 return $this->invokePlugin($method, $arguments, $filesystem);
282 } catch (PluginNotFoundException $e) {
283 // Let it pass, it's ok, don't panic.
284 }
285
286 $callback = [$filesystem, $method];
287
288 return call_user_func_array($callback, $arguments);
289 }
290
298 protected function getPrefixAndPath($path)
299 {
300 if (strpos($path, '://') < 1) {
301 throw new InvalidArgumentException('No prefix detected in path: ' . $path);
302 }
303
304 return explode('://', $path, 2);
305 }
306}
$result
An exception for terminatinating execution or to throw for unit testing.
Thrown when the MountManager cannot find a filesystem.
move($from, $to, array $config=[])
Move a file.
copy($from, $to, array $config=[])
mountFilesystem($prefix, FilesystemInterface $filesystem)
Mount filesystems.
__call($method, $arguments)
Call forwarder.
mountFilesystems(array $filesystems)
Mount filesystems.
listContents($directory='', $recursive=false)
getFilesystem($prefix)
Get the filesystem with the corresponding prefix.
__construct(array $filesystems=[])
Constructor.
invokePluginOnFilesystem($method, $arguments, $prefix)
Invoke a plugin on a filesystem mounted on a given prefix.
filterPrefix(array $arguments)
Retrieve the prefix from an arguments array.
listWith(array $keys=[], $directory='', $recursive=false)
List with plugin adapter.
$keys
static filesystems()
Returns the loaded filesystems.
invokePlugin($method, array $arguments, FilesystemInterface $filesystem)
Invoke a plugin by method name.
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file
$from