|
3 | 3 | namespace Contributte\Imagist\Filesystem; |
4 | 4 |
|
5 | 5 | use Contributte\Imagist\Exceptions\FileException; |
| 6 | +use Contributte\Imagist\Exceptions\FileNotFoundException; |
6 | 7 | use Contributte\Imagist\PathInfo\PathInfoInterface; |
7 | | -use League\Flysystem\FileNotFoundException; |
8 | | -use League\Flysystem\Filesystem; |
9 | | -use League\Flysystem\FilesystemInterface as LeagueFilesystemInterface; |
| 8 | +use League\Flysystem\Config; |
| 9 | +use League\Flysystem\FilesystemAdapter; |
| 10 | +use League\Flysystem\FilesystemException; |
| 11 | +use League\Flysystem\StorageAttributes; |
| 12 | +use League\Flysystem\UnableToDeleteFile; |
| 13 | +use League\Flysystem\UnableToReadFile; |
| 14 | +use League\Flysystem\UnableToRetrieveMetadata; |
10 | 15 |
|
11 | 16 | abstract class FilesystemAbstract implements FilesystemInterface |
12 | 17 | { |
13 | 18 |
|
14 | | - protected LeagueFilesystemInterface $adapter; |
| 19 | + protected FilesystemAdapter $adapter; |
15 | 20 |
|
16 | | - public function __construct(LeagueFilesystemInterface $adapter) |
| 21 | + public function __construct(FilesystemAdapter $adapter) |
17 | 22 | { |
18 | 23 | $this->adapter = $adapter; |
19 | 24 | } |
20 | 25 |
|
21 | 26 | /** |
22 | | - * @inheritDoc |
| 27 | + * {@inheritDoc} |
23 | 28 | */ |
24 | 29 | public function exists(PathInfoInterface $path): bool |
25 | 30 | { |
26 | | - return $this->adapter->has($path->toString()); |
| 31 | + return $this->adapter->fileExists($path->toString()); |
27 | 32 | } |
28 | 33 |
|
29 | 34 | /** |
30 | | - * @inheritDoc |
| 35 | + * {@inheritDoc} |
31 | 36 | */ |
32 | 37 | public function delete(PathInfoInterface $path): bool |
33 | 38 | { |
34 | | - return $this->withTemporaryConfig( |
35 | | - 'disable_asserts', |
36 | | - true, |
37 | | - false, |
38 | | - fn (): bool => $this->adapter->delete($path->toString()) |
39 | | - ); |
| 39 | + try { |
| 40 | + $this->adapter->delete($path->toString()); |
| 41 | + } catch (UnableToDeleteFile $e) { |
| 42 | + throw new FileException(sprintf('Cannot delete file %s', $path->toString()), 0, $e); |
| 43 | + } |
| 44 | + |
| 45 | + return true; |
40 | 46 | } |
41 | 47 |
|
42 | 48 | /** |
43 | | - * @inheritDoc |
| 49 | + * @return array<StorageAttributes> |
44 | 50 | */ |
45 | | - public function listContents(string $path): array |
| 51 | + public function listContents(string $path): iterable |
46 | 52 | { |
47 | | - return $this->adapter->listContents($path); |
| 53 | + return $this->adapter->listContents($path, false); |
48 | 54 | } |
49 | 55 |
|
50 | 56 | /** |
51 | | - * @inheritDoc |
| 57 | + * {@inheritDoc} |
52 | 58 | */ |
53 | 59 | public function put(PathInfoInterface $path, string $content, array $config = []): void |
54 | 60 | { |
55 | | - $this->adapter->put($path->toString(), $content, $config); |
| 61 | + $this->adapter->write($path->toString(), $content, new Config($config)); |
56 | 62 | } |
57 | 63 |
|
58 | 64 | /** |
59 | | - * @inheritDoc |
| 65 | + * {@inheritDoc} |
60 | 66 | */ |
61 | 67 | public function putWithMkdir(PathInfoInterface $path, string $content, array $config = []): void |
62 | 68 | { |
63 | | - $this->adapter->createDir($path->toString($path::BUCKET | $path::SCOPE | $path::FILTER)); |
| 69 | + $this->adapter->createDirectory($path->toString($path::BUCKET | $path::SCOPE | $path::FILTER), new Config()); |
64 | 70 |
|
65 | 71 | $this->put($path, $content, $config); |
66 | 72 | } |
67 | 73 |
|
68 | 74 | /** |
69 | | - * @inheritDoc |
| 75 | + * {@inheritDoc} |
70 | 76 | */ |
71 | 77 | public function read(PathInfoInterface $path): string |
72 | 78 | { |
73 | 79 | try { |
74 | 80 | $content = $this->adapter->read($path->toString()); |
75 | | - } catch (FileNotFoundException $exception) { |
76 | | - throw new \Contributte\Imagist\Exceptions\FileNotFoundException($exception->getMessage()); |
77 | | - } |
78 | | - |
79 | | - if ($content === false) { |
80 | | - throw new FileException(sprintf('Cannot read file %s', $path->toString())); |
| 81 | + } catch (UnableToReadFile $e) { |
| 82 | + throw new FileNotFoundException(sprintf('Cannot read file %s', $path->toString()), 0, $e); |
| 83 | + } catch (FilesystemException $e) { |
| 84 | + throw new FileNotFoundException($e->getMessage(), 0, $e); |
81 | 85 | } |
82 | 86 |
|
83 | 87 | return $content; |
84 | 88 | } |
85 | 89 |
|
86 | 90 | /** |
87 | | - * @inheritDoc |
| 91 | + * {@inheritDoc} |
88 | 92 | */ |
89 | 93 | public function mimeType(PathInfoInterface $path): ?string |
90 | 94 | { |
91 | | - $mimeType = $this->adapter->getMimetype($path->toString()); |
92 | | - |
93 | | - return $mimeType === false ? null : $mimeType; |
94 | | - } |
95 | | - |
96 | | - /** |
97 | | - * @template T |
98 | | - * @param mixed $value |
99 | | - * @param mixed $default |
100 | | - * @param callable(): T $callback |
101 | | - * @return T |
102 | | - */ |
103 | | - protected function withTemporaryConfig(string $name, $value, $default, callable $callback) // phpcs:ignore -- phpcs bug |
104 | | - { |
105 | | - if (!$this->adapter instanceof Filesystem) { |
106 | | - return $callback(); |
| 95 | + try { |
| 96 | + $mimeType = $this->adapter->mimeType($path->toString()); |
| 97 | + } catch (UnableToRetrieveMetadata $e) { |
| 98 | + throw new FileException(sprintf('Cannot get mime type of file %s', $path->toString()), 0, $e); |
| 99 | + } catch (FilesystemException $e) { |
| 100 | + throw new FileNotFoundException($e->getMessage(), 0, $e); |
107 | 101 | } |
108 | 102 |
|
109 | | - $default = $this->adapter->getConfig()->get($name, $default); |
110 | | - |
111 | | - $this->adapter->getConfig()->set($name, $value); |
112 | | - $result = $callback(); |
113 | | - $this->adapter->getConfig()->set($name, $default); |
114 | | - |
115 | | - return $result; |
| 103 | + return $mimeType->mimeType(); |
116 | 104 | } |
117 | 105 |
|
118 | 106 | } |
0 commit comments