|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BePark\Flysystem\ReadOnlyFallback; |
|
4
|
|
|
|
|
5
|
|
|
use League\Flysystem\AdapterInterface; |
|
6
|
|
|
use League\Flysystem\Config; |
|
7
|
|
|
use League\Flysystem\Util; |
|
8
|
|
|
|
|
9
|
|
|
class ReadOnlyFallbackAdapter implements AdapterInterface |
|
10
|
|
|
{ |
|
11
|
|
|
protected $_readOnlyAdapter; |
|
12
|
|
|
|
|
13
|
|
|
protected $_mainAdapter; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(AdapterInterface $mainAdapter, AdapterInterface $readOnlyAdapter) |
|
16
|
|
|
{ |
|
17
|
3 |
|
$this->_mainAdapter = $mainAdapter; |
|
18
|
3 |
|
$this->_readOnlyAdapter = $readOnlyAdapter; |
|
19
|
3 |
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Returns the main adapter |
|
23
|
|
|
* |
|
24
|
|
|
* @return AdapterInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getMainAdapter() |
|
27
|
|
|
{ |
|
28
|
3 |
|
return $this->_mainAdapter; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Returns the fallback adapter |
|
33
|
|
|
* |
|
34
|
|
|
* @return AdapterInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getReadOnlyAdapter() |
|
37
|
|
|
{ |
|
38
|
3 |
|
return $this->_readOnlyAdapter; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function write($path, $contents, Config $config) |
|
45
|
2 |
|
{ |
|
46
|
3 |
|
return $this->_mainAdapter->write($path, $contents, $config); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function writeStream($path, $resource, Config $config) |
|
53
|
|
|
{ |
|
54
|
3 |
|
return $this->_mainAdapter->writeStream($path, $resource, $config); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function update($path, $contents, Config $config) |
|
61
|
|
|
{ |
|
62
|
3 |
|
$this->_backportFromReadOnly($path); |
|
63
|
|
|
|
|
64
|
3 |
|
return $this->_mainAdapter->update($path, $contents, $config); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function updateStream($path, $resource, Config $config) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->_backportFromReadOnly($path); |
|
73
|
|
|
|
|
74
|
|
|
return $this->_mainAdapter->updateStream($path, $resource, $config); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function rename($path, $newpath) |
|
81
|
|
|
{ |
|
82
|
|
|
// XXX we could probably make some improvements here without duplicate the source |
|
83
|
|
|
$this->_backportFromReadOnly($path); |
|
84
|
|
|
|
|
85
|
2 |
|
return $this->_mainAdapter->rename($path, $newpath); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function copy($path, $newpath) |
|
92
|
|
|
{ |
|
93
|
|
|
// XXX we could probably make some improvements here without duplicate the source |
|
94
|
|
|
$this->_backportFromReadOnly($path); |
|
95
|
|
|
|
|
96
|
|
|
return $this->_mainAdapter->copy($path, $newpath); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function delete($path) |
|
103
|
|
|
{ |
|
104
|
3 |
|
if ($this->_readOnlyAdapter->has($path) && !$this->_mainAdapter->has($path)) |
|
105
|
2 |
|
{ |
|
106
|
|
|
// will always be find but yeah except if we have an adapter that retains the delete information, it's impossible to |
|
107
|
|
|
// do something. So enjoy a weird thing |
|
108
|
3 |
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
3 |
|
return $this->_mainAdapter->delete($path); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public function deleteDir($dirname) |
|
118
|
|
|
{ |
|
119
|
|
|
if ($this->_readOnlyAdapter->has($dirname) && !$this->_mainAdapter->has($dirname)) |
|
120
|
|
|
{ |
|
121
|
|
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $this->_mainAdapter->deleteDir($dirname); |
|
125
|
2 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritdoc} |
|
129
|
|
|
*/ |
|
130
|
|
|
public function createDir($dirname, Config $config) |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->_mainAdapter->createDir($dirname, $config); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritdoc} |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setVisibility($path, $visibility) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->_backportFromReadOnly($path); |
|
141
|
|
|
|
|
142
|
|
|
return $this->_mainAdapter->setVisibility($path, $visibility); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritdoc} |
|
147
|
|
|
*/ |
|
148
|
|
|
public function has($path) |
|
149
|
|
|
{ |
|
150
|
3 |
|
return $this->_mainAdapter->has($path) || $this->_readOnlyAdapter->has($path); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* {@inheritdoc} |
|
155
|
|
|
*/ |
|
156
|
|
|
public function read($path) |
|
157
|
|
|
{ |
|
158
|
3 |
|
$result = $this->_mainAdapter->read($path); |
|
159
|
3 |
|
if (false !== $result) |
|
160
|
2 |
|
{ |
|
161
|
3 |
|
return $result; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
3 |
|
return $this->_readOnlyAdapter->read($path); |
|
165
|
2 |
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* {@inheritdoc} |
|
169
|
|
|
*/ |
|
170
|
|
|
public function readStream($path) |
|
171
|
|
|
{ |
|
172
|
|
|
$result = $this->_mainAdapter->readStream($path); |
|
173
|
|
|
if (false !== $result) |
|
174
|
|
|
{ |
|
175
|
|
|
return $result; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $this->_readOnlyAdapter->readStream($path); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
//TODO |
|
182
|
|
|
/** |
|
183
|
|
|
* {@inheritdoc} |
|
184
|
|
|
* @see https://github.com/Litipk/flysystem-fallback-adapter/blob/master/src/FallbackAdapter.php#L259 |
|
185
|
|
|
*/ |
|
186
|
|
|
public function listContents($directory = '', $recursive = false) |
|
187
|
|
|
{ |
|
188
|
|
|
// taken from https://github.com/Litipk/flysystem-fallback-adapter/blob/master/src/FallbackAdapter.php#L259 |
|
189
|
|
|
// listContents |
|
190
|
3 |
|
$tmpResult = $this->_mainAdapter->listContents($directory, $recursive); |
|
191
|
|
|
|
|
192
|
3 |
|
$inverseRef = []; |
|
193
|
3 |
|
foreach ($tmpResult as $index => $mainContent) |
|
194
|
|
|
{ |
|
195
|
3 |
|
$inverseRef[$mainContent['path']] = $index; |
|
196
|
2 |
|
} |
|
197
|
|
|
|
|
198
|
3 |
|
$fallbackContents = $this->_readOnlyAdapter->listContents($directory, $recursive); |
|
199
|
3 |
|
foreach ($fallbackContents as $fallbackContent) |
|
200
|
|
|
{ |
|
201
|
3 |
|
if (!isset($inverseRef[$fallbackContent['path']])) |
|
202
|
2 |
|
{ |
|
203
|
1 |
|
$tmpResult[] = $fallbackContent; |
|
204
|
|
|
} |
|
205
|
2 |
|
} |
|
206
|
|
|
|
|
207
|
3 |
|
return $tmpResult; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* {@inheritdoc} |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getMetadata($path) |
|
214
|
|
|
{ |
|
215
|
3 |
|
$result = $this->_mainAdapter->getMetadata($path); |
|
216
|
3 |
|
if (false !== $result) |
|
217
|
2 |
|
{ |
|
218
|
3 |
|
return $result; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return $this->_readOnlyAdapter->getMetadata($path); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* {@inheritdoc} |
|
226
|
|
|
*/ |
|
227
|
|
|
public function getSize($path) |
|
228
|
|
|
{ |
|
229
|
|
|
$result = $this->_mainAdapter->getSize($path); |
|
230
|
|
|
if (false !== $result) |
|
231
|
|
|
{ |
|
232
|
|
|
return $result; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
return $this->_readOnlyAdapter->getSize($path); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* {@inheritdoc} |
|
240
|
|
|
*/ |
|
241
|
|
|
public function getMimetype($path) |
|
242
|
|
|
{ |
|
243
|
|
|
$result = $this->_mainAdapter->getMimetype($path); |
|
244
|
|
|
if (false !== $result) |
|
245
|
|
|
{ |
|
246
|
|
|
return $result; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return $this->_readOnlyAdapter->getMimetype($path); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* {@inheritdoc} |
|
254
|
|
|
*/ |
|
255
|
|
|
public function getTimestamp($path) |
|
256
|
|
|
{ |
|
257
|
|
|
$result = $this->_mainAdapter->getTimestamp($path); |
|
258
|
|
|
if (false !== $result) |
|
259
|
|
|
{ |
|
260
|
|
|
return $result; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
return $this->_readOnlyAdapter->getTimestamp($path); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* {@inheritdoc} |
|
268
|
|
|
*/ |
|
269
|
|
|
public function getVisibility($path) |
|
270
|
|
|
{ |
|
271
|
|
|
$result = $this->_mainAdapter->getVisibility($path); |
|
272
|
|
|
if (false !== $result) |
|
273
|
|
|
{ |
|
274
|
|
|
return $result; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
return $this->_readOnlyAdapter->getVisibility($path); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Make resource available for modification on the new adapter |
|
282
|
|
|
* @param string $path |
|
283
|
|
|
* @return bool |
|
284
|
|
|
*/ |
|
285
|
|
|
protected function _backportFromReadOnly($path) |
|
286
|
|
|
{ |
|
287
|
|
|
// do nothing if it exist on the main or if the read only have none |
|
288
|
3 |
|
if ($this->_mainAdapter->has($path) || !$this->_readOnlyAdapter->has($path)) |
|
289
|
2 |
|
{ |
|
290
|
3 |
|
return true; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
// because we change something we need to be sure to have it on the main adapter before anything |
|
294
|
3 |
|
$buffer = $this->_readOnlyAdapter->readStream($path); |
|
295
|
3 |
|
if ($buffer === $buffer) |
|
296
|
2 |
|
{ |
|
297
|
3 |
|
return false; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
$result = $this->_mainAdapter->writeStream($path, $buffer['stream'], new Config()); |
|
301
|
|
|
if (is_resource($buffer['stream'])) |
|
302
|
|
|
{ |
|
303
|
|
|
fclose($buffer['stream']); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
return (false !== $result); |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|