-
Notifications
You must be signed in to change notification settings - Fork 3
/
ExceptionThrowingFilesystemAdapter.php
166 lines (122 loc) · 4.28 KB
/
ExceptionThrowingFilesystemAdapter.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
<?php
declare(strict_types=1);
namespace League\Flysystem\AdapterTestUtilities;
use League\Flysystem\Config;
use League\Flysystem\FileAttributes;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\FilesystemOperationFailed;
class ExceptionThrowingFilesystemAdapter implements FilesystemAdapter
{
/**
* @var FilesystemAdapter
*/
private $adapter;
/**
* @var array<string, FilesystemOperationFailed>
*/
private $stagedExceptions = [];
public function __construct(FilesystemAdapter $adapter)
{
$this->adapter = $adapter;
}
public function stageException(string $method, string $path, FilesystemOperationFailed $exception): void
{
$this->stagedExceptions[join('@', [$method, $path])] = $exception;
}
private function throwStagedException(string $method, $path): void
{
$method = preg_replace('~.+::~', '', $method);
$key = join('@', [$method, $path]);
if ( ! array_key_exists($key, $this->stagedExceptions)) {
return;
}
$exception = $this->stagedExceptions[$key];
unset($this->stagedExceptions[$key]);
throw $exception;
}
public function fileExists(string $path): bool
{
$this->throwStagedException(__METHOD__, $path);
return $this->adapter->fileExists($path);
}
public function write(string $path, string $contents, Config $config): void
{
$this->throwStagedException(__METHOD__, $path);
$this->adapter->write($path, $contents, $config);
}
public function writeStream(string $path, $contents, Config $config): void
{
$this->throwStagedException(__METHOD__, $path);
$this->adapter->writeStream($path, $contents, $config);
}
public function read(string $path): string
{
$this->throwStagedException(__METHOD__, $path);
return $this->adapter->read($path);
}
public function readStream(string $path)
{
$this->throwStagedException(__METHOD__, $path);
return $this->adapter->readStream($path);
}
public function delete(string $path): void
{
$this->throwStagedException(__METHOD__, $path);
$this->adapter->delete($path);
}
public function deleteDirectory(string $path): void
{
$this->throwStagedException(__METHOD__, $path);
$this->adapter->deleteDirectory($path);
}
public function createDirectory(string $path, Config $config): void
{
$this->throwStagedException(__METHOD__, $path);
$this->adapter->createDirectory($path, $config);
}
public function setVisibility(string $path, string $visibility): void
{
$this->throwStagedException(__METHOD__, $path);
$this->adapter->setVisibility($path, $visibility);
}
public function visibility(string $path): FileAttributes
{
$this->throwStagedException(__METHOD__, $path);
return $this->adapter->visibility($path);
}
public function mimeType(string $path): FileAttributes
{
$this->throwStagedException(__METHOD__, $path);
return $this->adapter->mimeType($path);
}
public function lastModified(string $path): FileAttributes
{
$this->throwStagedException(__METHOD__, $path);
return $this->adapter->lastModified($path);
}
public function fileSize(string $path): FileAttributes
{
$this->throwStagedException(__METHOD__, $path);
return $this->adapter->fileSize($path);
}
public function listContents(string $path, bool $deep): iterable
{
$this->throwStagedException(__METHOD__, $path);
return $this->adapter->listContents($path, $deep);
}
public function move(string $source, string $destination, Config $config): void
{
$this->throwStagedException(__METHOD__, $source);
$this->adapter->move($source, $destination, $config);
}
public function copy(string $source, string $destination, Config $config): void
{
$this->throwStagedException(__METHOD__, $source);
$this->adapter->copy($source, $destination, $config);
}
public function directoryExists(string $path): bool
{
$this->throwStagedException(__METHOD__, $path);
return $this->adapter->directoryExists($path);
}
}