-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathInMemoryFilesystemAdapter.php
258 lines (200 loc) · 8.06 KB
/
InMemoryFilesystemAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
declare(strict_types=1);
namespace League\Flysystem\InMemory;
use League\Flysystem\Config;
use League\Flysystem\DirectoryAttributes;
use League\Flysystem\FileAttributes;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\UnableToCopyFile;
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToRetrieveMetadata;
use League\Flysystem\UnableToSetVisibility;
use League\Flysystem\Visibility;
use League\MimeTypeDetection\FinfoMimeTypeDetector;
use League\MimeTypeDetection\MimeTypeDetector;
use function array_keys;
use function rtrim;
use function strpos;
class InMemoryFilesystemAdapter implements FilesystemAdapter
{
const DUMMY_FILE_FOR_FORCED_LISTING_IN_FLYSYSTEM_TEST = '______DUMMY_FILE_FOR_FORCED_LISTING_IN_FLYSYSTEM_TEST';
/**
* @var InMemoryFile[]
*/
private array $files = [];
private MimeTypeDetector $mimeTypeDetector;
public function __construct(
private string $defaultVisibility = Visibility::PUBLIC,
MimeTypeDetector $mimeTypeDetector = null
) {
$this->mimeTypeDetector = $mimeTypeDetector ?: new FinfoMimeTypeDetector();
}
public function fileExists(string $path): bool
{
return array_key_exists($this->preparePath($path), $this->files);
}
public function write(string $path, string $contents, Config $config): void
{
$path = $this->preparePath($path);
$file = $this->files[$path] = $this->files[$path] ?? new InMemoryFile();
$file->updateContents($contents, $config->get('timestamp'));
$visibility = $config->get(Config::OPTION_VISIBILITY, $this->defaultVisibility);
$file->setVisibility($visibility);
}
public function writeStream(string $path, $contents, Config $config): void
{
$this->write($path, (string) stream_get_contents($contents), $config);
}
public function read(string $path): string
{
$path = $this->preparePath($path);
if (array_key_exists($path, $this->files) === false) {
throw UnableToReadFile::fromLocation($path, 'file does not exist');
}
return $this->files[$path]->read();
}
public function readStream(string $path)
{
$path = $this->preparePath($path);
if (array_key_exists($path, $this->files) === false) {
throw UnableToReadFile::fromLocation($path, 'file does not exist');
}
return $this->files[$path]->readStream();
}
public function delete(string $path): void
{
unset($this->files[$this->preparePath($path)]);
}
public function deleteDirectory(string $prefix): void
{
$prefix = $this->preparePath($prefix);
$prefix = rtrim($prefix, '/') . '/';
foreach (array_keys($this->files) as $path) {
if (strpos($path, $prefix) === 0) {
unset($this->files[$path]);
}
}
}
public function createDirectory(string $path, Config $config): void
{
$filePath = rtrim($path, '/') . '/' . self::DUMMY_FILE_FOR_FORCED_LISTING_IN_FLYSYSTEM_TEST;
$this->write($filePath, '', $config);
}
public function directoryExists(string $path): bool
{
$prefix = $this->preparePath($path);
$prefix = rtrim($prefix, '/') . '/';
foreach (array_keys($this->files) as $path) {
if (strpos($path, $prefix) === 0) {
return true;
}
}
return false;
}
public function setVisibility(string $path, string $visibility): void
{
$path = $this->preparePath($path);
if (array_key_exists($path, $this->files) === false) {
throw UnableToSetVisibility::atLocation($path, 'file does not exist');
}
$this->files[$path]->setVisibility($visibility);
}
public function visibility(string $path): FileAttributes
{
$path = $this->preparePath($path);
if (array_key_exists($path, $this->files) === false) {
throw UnableToRetrieveMetadata::visibility($path, 'file does not exist');
}
return new FileAttributes($path, null, $this->files[$path]->visibility());
}
public function mimeType(string $path): FileAttributes
{
$preparedPath = $this->preparePath($path);
if (array_key_exists($preparedPath, $this->files) === false) {
throw UnableToRetrieveMetadata::mimeType($path, 'file does not exist');
}
$mimeType = $this->mimeTypeDetector->detectMimeType($path, $this->files[$preparedPath]->read());
if ($mimeType === null) {
throw UnableToRetrieveMetadata::mimeType($path);
}
return new FileAttributes($preparedPath, null, null, null, $mimeType);
}
public function lastModified(string $path): FileAttributes
{
$path = $this->preparePath($path);
if (array_key_exists($path, $this->files) === false) {
throw UnableToRetrieveMetadata::lastModified($path, 'file does not exist');
}
return new FileAttributes($path, null, null, $this->files[$path]->lastModified());
}
public function fileSize(string $path): FileAttributes
{
$path = $this->preparePath($path);
if (array_key_exists($path, $this->files) === false) {
throw UnableToRetrieveMetadata::fileSize($path, 'file does not exist');
}
return new FileAttributes($path, $this->files[$path]->fileSize());
}
public function listContents(string $path, bool $deep): iterable
{
$prefix = rtrim($this->preparePath($path), '/') . '/';
$prefixLength = strlen($prefix);
$listedDirectories = [];
foreach ($this->files as $path => $file) {
if (substr($path, 0, $prefixLength) === $prefix) {
$subPath = substr($path, $prefixLength);
$dirname = dirname($subPath);
if ($dirname !== '.') {
$parts = explode('/', $dirname);
$dirPath = '';
foreach ($parts as $index => $part) {
if ($deep === false && $index >= 1) {
break;
}
$dirPath .= $part . '/';
if ( ! in_array($dirPath, $listedDirectories)) {
$listedDirectories[] = $dirPath;
yield new DirectoryAttributes(trim($prefix . $dirPath, '/'));
}
}
}
$dummyFilename = self::DUMMY_FILE_FOR_FORCED_LISTING_IN_FLYSYSTEM_TEST;
if (substr($path, -strlen($dummyFilename)) === $dummyFilename) {
continue;
}
if ($deep === true || strpos($subPath, '/') === false) {
yield new FileAttributes(ltrim($path, '/'), $file->fileSize(), $file->visibility(), $file->lastModified(), $file->mimeType());
}
}
}
}
public function move(string $source, string $destination, Config $config): void
{
$source = $this->preparePath($source);
$destination = $this->preparePath($destination);
if ( ! $this->fileExists($source) || $this->fileExists($destination)) {
throw UnableToMoveFile::fromLocationTo($source, $destination);
}
$this->files[$destination] = $this->files[$source];
unset($this->files[$source]);
}
public function copy(string $source, string $destination, Config $config): void
{
$source = $this->preparePath($source);
$destination = $this->preparePath($destination);
if ( ! $this->fileExists($source)) {
throw UnableToCopyFile::fromLocationTo($source, $destination);
}
$lastModified = $config->get('timestamp', time());
$this->files[$destination] = $this->files[$source]->withLastModified($lastModified);
}
private function preparePath(string $path): string
{
return '/' . ltrim($path, '/');
}
public function deleteEverything(): void
{
$this->files = [];
}
}