Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace League\Flysystem;
6
 
7
use DateTimeInterface;
8
use Generator;
9
use League\Flysystem\UrlGeneration\ShardedPrefixPublicUrlGenerator;
10
use League\Flysystem\UrlGeneration\PrefixPublicUrlGenerator;
11
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
12
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
13
use Throwable;
14
 
15
use function is_array;
16
 
17
class Filesystem implements FilesystemOperator
18
{
19
    use CalculateChecksumFromStream;
20
 
21
    private Config $config;
22
    private PathNormalizer $pathNormalizer;
23
 
24
    public function __construct(
25
        private FilesystemAdapter $adapter,
26
        array $config = [],
27
        PathNormalizer $pathNormalizer = null,
28
        private ?PublicUrlGenerator $publicUrlGenerator = null,
29
        private ?TemporaryUrlGenerator $temporaryUrlGenerator = null,
30
    ) {
31
        $this->config = new Config($config);
32
        $this->pathNormalizer = $pathNormalizer ?: new WhitespacePathNormalizer();
33
    }
34
 
35
    public function fileExists(string $location): bool
36
    {
37
        return $this->adapter->fileExists($this->pathNormalizer->normalizePath($location));
38
    }
39
 
40
    public function directoryExists(string $location): bool
41
    {
42
        return $this->adapter->directoryExists($this->pathNormalizer->normalizePath($location));
43
    }
44
 
45
    public function has(string $location): bool
46
    {
47
        $path = $this->pathNormalizer->normalizePath($location);
48
 
49
        return $this->adapter->fileExists($path) || $this->adapter->directoryExists($path);
50
    }
51
 
52
    public function write(string $location, string $contents, array $config = []): void
53
    {
54
        $this->adapter->write(
55
            $this->pathNormalizer->normalizePath($location),
56
            $contents,
57
            $this->config->extend($config)
58
        );
59
    }
60
 
61
    public function writeStream(string $location, $contents, array $config = []): void
62
    {
63
        /* @var resource $contents */
64
        $this->assertIsResource($contents);
65
        $this->rewindStream($contents);
66
        $this->adapter->writeStream(
67
            $this->pathNormalizer->normalizePath($location),
68
            $contents,
69
            $this->config->extend($config)
70
        );
71
    }
72
 
73
    public function read(string $location): string
74
    {
75
        return $this->adapter->read($this->pathNormalizer->normalizePath($location));
76
    }
77
 
78
    public function readStream(string $location)
79
    {
80
        return $this->adapter->readStream($this->pathNormalizer->normalizePath($location));
81
    }
82
 
83
    public function delete(string $location): void
84
    {
85
        $this->adapter->delete($this->pathNormalizer->normalizePath($location));
86
    }
87
 
88
    public function deleteDirectory(string $location): void
89
    {
90
        $this->adapter->deleteDirectory($this->pathNormalizer->normalizePath($location));
91
    }
92
 
93
    public function createDirectory(string $location, array $config = []): void
94
    {
95
        $this->adapter->createDirectory(
96
            $this->pathNormalizer->normalizePath($location),
97
            $this->config->extend($config)
98
        );
99
    }
100
 
101
    public function listContents(string $location, bool $deep = self::LIST_SHALLOW): DirectoryListing
102
    {
103
        $path = $this->pathNormalizer->normalizePath($location);
104
        $listing = $this->adapter->listContents($path, $deep);
105
 
106
        return new DirectoryListing($this->pipeListing($location, $deep, $listing));
107
    }
108
 
109
    private function pipeListing(string $location, bool $deep, iterable $listing): Generator
110
    {
111
        try {
112
            foreach ($listing as $item) {
113
                yield $item;
114
            }
115
        } catch (Throwable $exception) {
116
            throw UnableToListContents::atLocation($location, $deep, $exception);
117
        }
118
    }
119
 
120
    public function move(string $source, string $destination, array $config = []): void
121
    {
122
        $this->adapter->move(
123
            $this->pathNormalizer->normalizePath($source),
124
            $this->pathNormalizer->normalizePath($destination),
125
            $this->config->extend($config)
126
        );
127
    }
128
 
129
    public function copy(string $source, string $destination, array $config = []): void
130
    {
131
        $this->adapter->copy(
132
            $this->pathNormalizer->normalizePath($source),
133
            $this->pathNormalizer->normalizePath($destination),
134
            $this->config->extend($config)
135
        );
136
    }
137
 
138
    public function lastModified(string $path): int
139
    {
140
        return $this->adapter->lastModified($this->pathNormalizer->normalizePath($path))->lastModified();
141
    }
142
 
143
    public function fileSize(string $path): int
144
    {
145
        return $this->adapter->fileSize($this->pathNormalizer->normalizePath($path))->fileSize();
146
    }
147
 
148
    public function mimeType(string $path): string
149
    {
150
        return $this->adapter->mimeType($this->pathNormalizer->normalizePath($path))->mimeType();
151
    }
152
 
153
    public function setVisibility(string $path, string $visibility): void
154
    {
155
        $this->adapter->setVisibility($this->pathNormalizer->normalizePath($path), $visibility);
156
    }
157
 
158
    public function visibility(string $path): string
159
    {
160
        return $this->adapter->visibility($this->pathNormalizer->normalizePath($path))->visibility();
161
    }
162
 
163
    public function publicUrl(string $path, array $config = []): string
164
    {
165
        $this->publicUrlGenerator ??= $this->resolvePublicUrlGenerator()
166
            ?: throw UnableToGeneratePublicUrl::noGeneratorConfigured($path);
167
        $config = $this->config->extend($config);
168
 
169
        return $this->publicUrlGenerator->publicUrl($path, $config);
170
    }
171
 
172
    public function temporaryUrl(string $path, DateTimeInterface $expiresAt, array $config = []): string
173
    {
174
        $generator = $this->temporaryUrlGenerator ?: $this->adapter;
175
 
176
        if ($generator instanceof TemporaryUrlGenerator) {
177
            return $generator->temporaryUrl($path, $expiresAt, $this->config->extend($config));
178
        }
179
 
180
        throw UnableToGenerateTemporaryUrl::noGeneratorConfigured($path);
181
    }
182
 
183
    public function checksum(string $path, array $config = []): string
184
    {
185
        $config = $this->config->extend($config);
186
 
187
        if ( ! $this->adapter instanceof ChecksumProvider) {
188
            return $this->calculateChecksumFromStream($path, $config);
189
        }
190
 
191
        try {
192
            return $this->adapter->checksum($path, $config);
193
        } catch (ChecksumAlgoIsNotSupported) {
194
            return $this->calculateChecksumFromStream($path, $config);
195
        }
196
    }
197
 
198
    private function resolvePublicUrlGenerator(): ?PublicUrlGenerator
199
    {
200
        if ($publicUrl = $this->config->get('public_url')) {
201
            return match (true) {
202
                is_array($publicUrl) => new ShardedPrefixPublicUrlGenerator($publicUrl),
203
                default => new PrefixPublicUrlGenerator($publicUrl),
204
            };
205
        }
206
 
207
        if ($this->adapter instanceof PublicUrlGenerator) {
208
            return $this->adapter;
209
        }
210
 
211
        return null;
212
    }
213
 
214
    /**
215
     * @param mixed $contents
216
     */
217
    private function assertIsResource($contents): void
218
    {
219
        if (is_resource($contents) === false) {
220
            throw new InvalidStreamProvided(
221
                "Invalid stream provided, expected stream resource, received " . gettype($contents)
222
            );
223
        } elseif ($type = get_resource_type($contents) !== 'stream') {
224
            throw new InvalidStreamProvided(
225
                "Invalid stream provided, expected stream resource, received resource of type " . $type
226
            );
227
        }
228
    }
229
 
230
    /**
231
     * @param resource $resource
232
     */
233
    private function rewindStream($resource): void
234
    {
235
        if (ftell($resource) !== 0 && stream_get_meta_data($resource)['seekable']) {
236
            rewind($resource);
237
        }
238
    }
239
}