PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.13.3
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.13.3
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / vendor / league / flysystem / src / MountManager.php

MountManager.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.13.3, at vendor/league/flysystem/src/MountManager.php

435 lines 15.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace League\Flysystem;
6
7 use DateTimeInterface;
8 use Throwable;
9
10 use function compact;
11 use function method_exists;
12 use function sprintf;
13
14 class MountManager implements FilesystemOperator
15 {
16 /**
17 * @var array<string, FilesystemOperator>
18 */
19 private $filesystems = [];
20
21 /**
22 * @var Config
23 */
24 private $config;
25
26 /**
27 * MountManager constructor.
28 *
29 * @param array<string,FilesystemOperator> $filesystems
30 */
31 public function __construct(array $filesystems = [], array $config = [])
32 {
33 $this->mountFilesystems($filesystems);
34 $this->config = new Config($config);
35 }
36
37 /**
38 * It is not recommended to mount filesystems after creation because interacting
39 * with the Mount Manager becomes unpredictable. Use this as an escape hatch.
40 */
41 public function dangerouslyMountFilesystems(string $key, FilesystemOperator $filesystem): void
42 {
43 $this->mountFilesystem($key, $filesystem);
44 }
45
46 /**
47 * @param array<string,FilesystemOperator> $filesystems
48 */
49 public function extend(array $filesystems, array $config = []): MountManager
50 {
51 $clone = clone $this;
52 $clone->config = $this->config->extend($config);
53 $clone->mountFilesystems($filesystems);
54
55 return $clone;
56 }
57
58 public function fileExists(string $location): bool
59 {
60 /** @var FilesystemOperator $filesystem */
61 [$filesystem, $path] = $this->determineFilesystemAndPath($location);
62
63 try {
64 return $filesystem->fileExists($path);
65 } catch (Throwable $exception) {
66 throw UnableToCheckFileExistence::forLocation($location, $exception);
67 }
68 }
69
70 public function has(string $location): bool
71 {
72 /** @var FilesystemOperator $filesystem */
73 [$filesystem, $path] = $this->determineFilesystemAndPath($location);
74
75 try {
76 return $filesystem->fileExists($path) || $filesystem->directoryExists($path);
77 } catch (Throwable $exception) {
78 throw UnableToCheckExistence::forLocation($location, $exception);
79 }
80 }
81
82 public function directoryExists(string $location): bool
83 {
84 /** @var FilesystemOperator $filesystem */
85 [$filesystem, $path] = $this->determineFilesystemAndPath($location);
86
87 try {
88 return $filesystem->directoryExists($path);
89 } catch (Throwable $exception) {
90 throw UnableToCheckDirectoryExistence::forLocation($location, $exception);
91 }
92 }
93
94 public function read(string $location): string
95 {
96 /** @var FilesystemOperator $filesystem */
97 [$filesystem, $path] = $this->determineFilesystemAndPath($location);
98
99 try {
100 return $filesystem->read($path);
101 } catch (UnableToReadFile $exception) {
102 throw UnableToReadFile::fromLocation($location, $exception->reason(), $exception);
103 }
104 }
105
106 public function readStream(string $location)
107 {
108 /** @var FilesystemOperator $filesystem */
109 [$filesystem, $path] = $this->determineFilesystemAndPath($location);
110
111 try {
112 return $filesystem->readStream($path);
113 } catch (UnableToReadFile $exception) {
114 throw UnableToReadFile::fromLocation($location, $exception->reason(), $exception);
115 }
116 }
117
118 public function listContents(string $location, bool $deep = self::LIST_SHALLOW): DirectoryListing
119 {
120 /** @var FilesystemOperator $filesystem */
121 [$filesystem, $path, $mountIdentifier] = $this->determineFilesystemAndPath($location);
122
123 return
124 $filesystem
125 ->listContents($path, $deep)
126 ->map(
127 function (StorageAttributes $attributes) use ($mountIdentifier) {
128 return $attributes->withPath(sprintf('%s://%s', $mountIdentifier, $attributes->path()));
129 }
130 );
131 }
132
133 public function lastModified(string $location): int
134 {
135 /** @var FilesystemOperator $filesystem */
136 [$filesystem, $path] = $this->determineFilesystemAndPath($location);
137
138 try {
139 return $filesystem->lastModified($path);
140 } catch (UnableToRetrieveMetadata $exception) {
141 throw UnableToRetrieveMetadata::lastModified($location, $exception->reason(), $exception);
142 }
143 }
144
145 public function fileSize(string $location): int
146 {
147 /** @var FilesystemOperator $filesystem */
148 [$filesystem, $path] = $this->determineFilesystemAndPath($location);
149
150 try {
151 return $filesystem->fileSize($path);
152 } catch (UnableToRetrieveMetadata $exception) {
153 throw UnableToRetrieveMetadata::fileSize($location, $exception->reason(), $exception);
154 }
155 }
156
157 public function mimeType(string $location): string
158 {
159 /** @var FilesystemOperator $filesystem */
160 [$filesystem, $path] = $this->determineFilesystemAndPath($location);
161
162 try {
163 return $filesystem->mimeType($path);
164 } catch (UnableToRetrieveMetadata $exception) {
165 throw UnableToRetrieveMetadata::mimeType($location, $exception->reason(), $exception);
166 }
167 }
168
169 public function visibility(string $path): string
170 {
171 /** @var FilesystemOperator $filesystem */
172 [$filesystem, $location] = $this->determineFilesystemAndPath($path);
173
174 try {
175 return $filesystem->visibility($location);
176 } catch (UnableToRetrieveMetadata $exception) {
177 throw UnableToRetrieveMetadata::visibility($path, $exception->reason(), $exception);
178 }
179 }
180
181 public function write(string $location, string $contents, array $config = []): void
182 {
183 /** @var FilesystemOperator $filesystem */
184 [$filesystem, $path] = $this->determineFilesystemAndPath($location);
185
186 try {
187 $filesystem->write($path, $contents, $this->config->extend($config)->toArray());
188 } catch (UnableToWriteFile $exception) {
189 throw UnableToWriteFile::atLocation($location, $exception->reason(), $exception);
190 }
191 }
192
193 public function writeStream(string $location, $contents, array $config = []): void
194 {
195 /** @var FilesystemOperator $filesystem */
196 [$filesystem, $path] = $this->determineFilesystemAndPath($location);
197 $filesystem->writeStream($path, $contents, $this->config->extend($config)->toArray());
198 }
199
200 public function setVisibility(string $path, string $visibility): void
201 {
202 /** @var FilesystemOperator $filesystem */
203 [$filesystem, $path] = $this->determineFilesystemAndPath($path);
204 $filesystem->setVisibility($path, $visibility);
205 }
206
207 public function delete(string $location): void
208 {
209 /** @var FilesystemOperator $filesystem */
210 [$filesystem, $path] = $this->determineFilesystemAndPath($location);
211
212 try {
213 $filesystem->delete($path);
214 } catch (UnableToDeleteFile $exception) {
215 throw UnableToDeleteFile::atLocation($location, $exception->reason(), $exception);
216 }
217 }
218
219 public function deleteDirectory(string $location): void
220 {
221 /** @var FilesystemOperator $filesystem */
222 [$filesystem, $path] = $this->determineFilesystemAndPath($location);
223
224 try {
225 $filesystem->deleteDirectory($path);
226 } catch (UnableToDeleteDirectory $exception) {
227 throw UnableToDeleteDirectory::atLocation($location, $exception->reason(), $exception);
228 }
229 }
230
231 public function createDirectory(string $location, array $config = []): void
232 {
233 /** @var FilesystemOperator $filesystem */
234 [$filesystem, $path] = $this->determineFilesystemAndPath($location);
235
236 try {
237 $filesystem->createDirectory($path, $this->config->extend($config)->toArray());
238 } catch (UnableToCreateDirectory $exception) {
239 throw UnableToCreateDirectory::dueToFailure($location, $exception);
240 }
241 }
242
243 public function move(string $source, string $destination, array $config = []): void
244 {
245 /** @var FilesystemOperator $sourceFilesystem */
246 /* @var FilesystemOperator $destinationFilesystem */
247 [$sourceFilesystem, $sourcePath] = $this->determineFilesystemAndPath($source);
248 [$destinationFilesystem, $destinationPath] = $this->determineFilesystemAndPath($destination);
249
250 $sourceFilesystem === $destinationFilesystem ? $this->moveInTheSameFilesystem(
251 $sourceFilesystem,
252 $sourcePath,
253 $destinationPath,
254 $source,
255 $destination,
256 $config,
257 ) : $this->moveAcrossFilesystems($source, $destination, $config);
258 }
259
260 public function copy(string $source, string $destination, array $config = []): void
261 {
262 /** @var FilesystemOperator $sourceFilesystem */
263 /* @var FilesystemOperator $destinationFilesystem */
264 [$sourceFilesystem, $sourcePath] = $this->determineFilesystemAndPath($source);
265 [$destinationFilesystem, $destinationPath] = $this->determineFilesystemAndPath($destination);
266
267 $sourceFilesystem === $destinationFilesystem ? $this->copyInSameFilesystem(
268 $sourceFilesystem,
269 $sourcePath,
270 $destinationPath,
271 $source,
272 $destination,
273 $config,
274 ) : $this->copyAcrossFilesystem(
275 $sourceFilesystem,
276 $sourcePath,
277 $destinationFilesystem,
278 $destinationPath,
279 $source,
280 $destination,
281 $config,
282 );
283 }
284
285 public function publicUrl(string $path, array $config = []): string
286 {
287 /** @var FilesystemOperator $filesystem */
288 [$filesystem, $path] = $this->determineFilesystemAndPath($path);
289
290 if ( ! method_exists($filesystem, 'publicUrl')) {
291 throw new UnableToGeneratePublicUrl(sprintf('%s does not support generating public urls.', $filesystem::class), $path);
292 }
293
294 return $filesystem->publicUrl($path, $config);
295 }
296
297 public function temporaryUrl(string $path, DateTimeInterface $expiresAt, array $config = []): string
298 {
299 /** @var FilesystemOperator $filesystem */
300 [$filesystem, $path] = $this->determineFilesystemAndPath($path);
301
302 if ( ! method_exists($filesystem, 'temporaryUrl')) {
303 throw new UnableToGenerateTemporaryUrl(sprintf('%s does not support generating public urls.', $filesystem::class), $path);
304 }
305
306 return $filesystem->temporaryUrl($path, $expiresAt, $this->config->extend($config)->toArray());
307 }
308
309 public function checksum(string $path, array $config = []): string
310 {
311 /** @var FilesystemOperator $filesystem */
312 [$filesystem, $path] = $this->determineFilesystemAndPath($path);
313
314 if ( ! method_exists($filesystem, 'checksum')) {
315 throw new UnableToProvideChecksum(sprintf('%s does not support providing checksums.', $filesystem::class), $path);
316 }
317
318 return $filesystem->checksum($path, $this->config->extend($config)->toArray());
319 }
320
321 private function mountFilesystems(array $filesystems): void
322 {
323 foreach ($filesystems as $key => $filesystem) {
324 $this->guardAgainstInvalidMount($key, $filesystem);
325 /* @var string $key */
326 /* @var FilesystemOperator $filesystem */
327 $this->mountFilesystem($key, $filesystem);
328 }
329 }
330
331 private function guardAgainstInvalidMount(mixed $key, mixed $filesystem): void
332 {
333 if ( ! is_string($key)) {
334 throw UnableToMountFilesystem::becauseTheKeyIsNotValid($key);
335 }
336
337 if ( ! $filesystem instanceof FilesystemOperator) {
338 throw UnableToMountFilesystem::becauseTheFilesystemWasNotValid($filesystem);
339 }
340 }
341
342 private function mountFilesystem(string $key, FilesystemOperator $filesystem): void
343 {
344 $this->filesystems[$key] = $filesystem;
345 }
346
347 /**
348 * @param string $path
349 *
350 * @return array{0:FilesystemOperator, 1:string, 2:string}
351 */
352 private function determineFilesystemAndPath(string $path): array
353 {
354 if (strpos($path, '://') < 1) {
355 throw UnableToResolveFilesystemMount::becauseTheSeparatorIsMissing($path);
356 }
357
358 /** @var string $mountIdentifier */
359 /** @var string $mountPath */
360 [$mountIdentifier, $mountPath] = explode('://', $path, 2);
361
362 if ( ! array_key_exists($mountIdentifier, $this->filesystems)) {
363 throw UnableToResolveFilesystemMount::becauseTheMountWasNotRegistered($mountIdentifier);
364 }
365
366 return [$this->filesystems[$mountIdentifier], $mountPath, $mountIdentifier];
367 }
368
369 private function copyInSameFilesystem(
370 FilesystemOperator $sourceFilesystem,
371 string $sourcePath,
372 string $destinationPath,
373 string $source,
374 string $destination,
375 array $config,
376 ): void {
377 try {
378 $sourceFilesystem->copy($sourcePath, $destinationPath, $this->config->extend($config)->toArray());
379 } catch (UnableToCopyFile $exception) {
380 throw UnableToCopyFile::fromLocationTo($source, $destination, $exception);
381 }
382 }
383
384 private function copyAcrossFilesystem(
385 FilesystemOperator $sourceFilesystem,
386 string $sourcePath,
387 FilesystemOperator $destinationFilesystem,
388 string $destinationPath,
389 string $source,
390 string $destination,
391 array $config,
392 ): void {
393 $config = $this->config->extend($config);
394 $retainVisibility = (bool) $config->get(Config::OPTION_RETAIN_VISIBILITY, true);
395 $visibility = $config->get(Config::OPTION_VISIBILITY);
396
397 try {
398 if ($visibility == null && $retainVisibility) {
399 $visibility = $sourceFilesystem->visibility($sourcePath);
400 $config = $config->extend(compact('visibility'));
401 }
402
403 $stream = $sourceFilesystem->readStream($sourcePath);
404 $destinationFilesystem->writeStream($destinationPath, $stream, $config->toArray());
405 } catch (UnableToRetrieveMetadata |UnableToReadFile |UnableToWriteFile $exception) {
406 throw UnableToCopyFile::fromLocationTo($source, $destination, $exception);
407 }
408 }
409
410 private function moveInTheSameFilesystem(
411 FilesystemOperator $sourceFilesystem,
412 string $sourcePath,
413 string $destinationPath,
414 string $source,
415 string $destination,
416 array $config,
417 ): void {
418 try {
419 $sourceFilesystem->move($sourcePath, $destinationPath, $this->config->extend($config)->toArray());
420 } catch (UnableToMoveFile $exception) {
421 throw UnableToMoveFile::fromLocationTo($source, $destination, $exception);
422 }
423 }
424
425 private function moveAcrossFilesystems(string $source, string $destination, array $config = []): void
426 {
427 try {
428 $this->copy($source, $destination, $config);
429 $this->delete($source);
430 } catch (UnableToCopyFile |UnableToDeleteFile $exception) {
431 throw UnableToMoveFile::fromLocationTo($source, $destination, $exception);
432 }
433 }
434 }
435