|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Flysystem\Cached; |
|
4
|
|
|
|
|
5
|
|
|
use League\Flysystem\AdapterInterface; |
|
6
|
|
|
use League\Flysystem\Config; |
|
7
|
|
|
|
|
8
|
|
|
class CachedAdapter implements AdapterInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var AdapterInterface |
|
12
|
|
|
*/ |
|
13
|
|
|
private $adapter; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var CacheInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $cache; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param AdapterInterface $adapter |
|
24
|
|
|
* @param CacheInterface $cache |
|
25
|
|
|
*/ |
|
26
|
144 |
|
public function __construct(AdapterInterface $adapter, CacheInterface $cache) |
|
27
|
|
|
{ |
|
28
|
144 |
|
$this->adapter = $adapter; |
|
29
|
144 |
|
$this->cache = $cache; |
|
30
|
144 |
|
$this->cache->load(); |
|
31
|
144 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get the underlying Adapter implementation. |
|
35
|
|
|
* |
|
36
|
|
|
* @return AdapterInterface |
|
37
|
|
|
*/ |
|
38
|
3 |
|
public function getAdapter() |
|
39
|
|
|
{ |
|
40
|
3 |
|
return $this->adapter; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the used Cache implementation. |
|
45
|
|
|
* |
|
46
|
|
|
* @return CacheInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getCache() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->cache; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
6 |
|
public function write($path, $contents, Config $config) |
|
57
|
|
|
{ |
|
58
|
6 |
|
$result = $this->adapter->write($path, $contents, $config); |
|
59
|
|
|
|
|
60
|
6 |
|
if ($result !== false) { |
|
61
|
3 |
|
$result['type'] = 'file'; |
|
62
|
3 |
|
$this->cache->updateObject($path, $result + compact('path', 'contents'), true); |
|
63
|
3 |
|
} |
|
64
|
|
|
|
|
65
|
6 |
|
return $result; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
6 |
|
public function writeStream($path, $resource, Config $config) |
|
72
|
|
|
{ |
|
73
|
6 |
|
$result = $this->adapter->writeStream($path, $resource, $config); |
|
74
|
|
|
|
|
75
|
6 |
|
if ($result !== false) { |
|
76
|
3 |
|
$result['type'] = 'file'; |
|
77
|
3 |
|
$contents = false; |
|
78
|
3 |
|
$this->cache->updateObject($path, $result + compact('path', 'contents'), true); |
|
79
|
3 |
|
} |
|
80
|
|
|
|
|
81
|
6 |
|
return $result; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
6 |
|
public function update($path, $contents, Config $config) |
|
88
|
|
|
{ |
|
89
|
6 |
|
$result = $this->adapter->update($path, $contents, $config); |
|
90
|
|
|
|
|
91
|
6 |
|
if ($result !== false) { |
|
92
|
3 |
|
$result['type'] = 'file'; |
|
93
|
3 |
|
$this->cache->updateObject($path, $result + compact('path', 'contents'), true); |
|
94
|
3 |
|
} |
|
95
|
|
|
|
|
96
|
6 |
|
return $result; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
6 |
|
public function updateStream($path, $resource, Config $config) |
|
103
|
|
|
{ |
|
104
|
6 |
|
$result = $this->adapter->updateStream($path, $resource, $config); |
|
105
|
|
|
|
|
106
|
6 |
|
if ($result !== false) { |
|
107
|
3 |
|
$result['type'] = 'file'; |
|
108
|
3 |
|
$contents = false; |
|
109
|
3 |
|
$this->cache->updateObject($path, $result + compact('path', 'contents'), true); |
|
110
|
3 |
|
} |
|
111
|
|
|
|
|
112
|
6 |
|
return $result; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
6 |
|
public function rename($path, $newPath) |
|
119
|
|
|
{ |
|
120
|
6 |
|
$result = $this->adapter->rename($path, $newPath); |
|
121
|
|
|
|
|
122
|
6 |
|
if ($result !== false) { |
|
123
|
3 |
|
$this->cache->rename($path, $newPath); |
|
124
|
3 |
|
} |
|
125
|
|
|
|
|
126
|
6 |
|
return $result; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
6 |
|
public function copy($path, $newpath) |
|
133
|
|
|
{ |
|
134
|
6 |
|
$result = $this->adapter->copy($path, $newpath); |
|
135
|
|
|
|
|
136
|
6 |
|
if ($result !== false) { |
|
137
|
3 |
|
$this->cache->copy($path, $newpath); |
|
138
|
3 |
|
} |
|
139
|
|
|
|
|
140
|
6 |
|
return $result; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* {@inheritdoc} |
|
145
|
|
|
*/ |
|
146
|
6 |
|
public function delete($path) |
|
147
|
|
|
{ |
|
148
|
6 |
|
$result = $this->adapter->delete($path); |
|
149
|
|
|
|
|
150
|
6 |
|
if ($result !== false) { |
|
151
|
3 |
|
$this->cache->delete($path); |
|
152
|
3 |
|
} |
|
153
|
|
|
|
|
154
|
6 |
|
return $result; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
*/ |
|
160
|
6 |
|
public function deleteDir($dirname) |
|
161
|
|
|
{ |
|
162
|
6 |
|
$result = $this->adapter->deleteDir($dirname); |
|
163
|
|
|
|
|
164
|
6 |
|
if ($result !== false) { |
|
165
|
3 |
|
$this->cache->deleteDir($dirname); |
|
166
|
3 |
|
} |
|
167
|
|
|
|
|
168
|
6 |
|
return $result; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* {@inheritdoc} |
|
173
|
|
|
*/ |
|
174
|
6 |
|
public function createDir($dirname, Config $config) |
|
175
|
|
|
{ |
|
176
|
6 |
|
$result = $this->adapter->createDir($dirname, $config); |
|
177
|
|
|
|
|
178
|
6 |
|
if ($result !== false) { |
|
179
|
3 |
|
$type = 'dir'; |
|
180
|
3 |
|
$path = $dirname; |
|
181
|
3 |
|
$this->cache->updateObject($dirname, compact('path', 'type'), true); |
|
182
|
3 |
|
} |
|
183
|
|
|
|
|
184
|
6 |
|
return $result; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* {@inheritdoc} |
|
189
|
|
|
*/ |
|
190
|
6 |
|
public function setVisibility($path, $visibility) |
|
191
|
|
|
{ |
|
192
|
6 |
|
$result = $this->adapter->setVisibility($path, $visibility); |
|
193
|
|
|
|
|
194
|
6 |
|
if ($result !== false) { |
|
195
|
3 |
|
$this->cache->updateObject($path, compact('path', 'visibility'), true); |
|
196
|
3 |
|
} |
|
197
|
|
|
|
|
198
|
6 |
|
return $result; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* {@inheritdoc} |
|
203
|
|
|
*/ |
|
204
|
12 |
|
public function has($path) |
|
205
|
|
|
{ |
|
206
|
12 |
|
$cacheHas = $this->cache->has($path); |
|
207
|
|
|
|
|
208
|
12 |
|
if ($cacheHas !== null) { |
|
209
|
6 |
|
return $cacheHas; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
6 |
|
$adapterResponse = $this->adapter->has($path); |
|
213
|
|
|
|
|
214
|
6 |
|
if (! $adapterResponse) { |
|
215
|
3 |
|
$this->cache->storeMiss($path); |
|
216
|
3 |
|
} else { |
|
217
|
3 |
|
$cacheEntry = is_array($adapterResponse) ? $adapterResponse : compact('path'); |
|
218
|
3 |
|
$this->cache->updateObject($path, $cacheEntry, true); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
6 |
|
return $adapterResponse; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* {@inheritdoc} |
|
226
|
|
|
*/ |
|
227
|
9 |
|
public function read($path) |
|
228
|
|
|
{ |
|
229
|
9 |
|
return $this->callWithFallback('contents', $path, 'read'); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* {@inheritdoc} |
|
234
|
|
|
*/ |
|
235
|
3 |
|
public function readStream($path) |
|
236
|
|
|
{ |
|
237
|
3 |
|
return $this->adapter->readStream($path); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Get the path prefix. |
|
242
|
|
|
* |
|
243
|
9 |
|
* @return string|null path prefix or null if pathPrefix is empty |
|
244
|
|
|
*/ |
|
245
|
9 |
|
public function getPathPrefix() |
|
246
|
3 |
|
{ |
|
247
|
|
|
return $this->adapter->getPathPrefix(); |
|
248
|
|
|
} |
|
249
|
6 |
|
|
|
250
|
|
|
/** |
|
251
|
6 |
|
* Prefix a path. |
|
252
|
3 |
|
* |
|
253
|
3 |
|
* @param string $path |
|
254
|
|
|
* |
|
255
|
6 |
|
* @return string prefixed path |
|
256
|
|
|
*/ |
|
257
|
|
|
public function applyPathPrefix($path) |
|
258
|
|
|
{ |
|
259
|
|
|
return $this->adapter->applyPathPrefix($path); |
|
260
|
|
|
} |
|
261
|
9 |
|
|
|
262
|
|
|
/** |
|
263
|
9 |
|
* {@inheritdoc} |
|
264
|
|
|
*/ |
|
265
|
|
|
public function listContents($directory = '', $recursive = false) |
|
266
|
|
|
{ |
|
267
|
|
|
if ($this->cache->isComplete($directory, $recursive)) { |
|
268
|
|
|
return $this->cache->listContents($directory, $recursive); |
|
269
|
9 |
|
} |
|
270
|
|
|
|
|
271
|
9 |
|
$result = $this->adapter->listContents($directory, $recursive); |
|
272
|
|
|
|
|
273
|
|
|
if ($result !== false) { |
|
274
|
|
|
$this->cache->storeContents($directory, $result, $recursive); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
9 |
|
return $result; |
|
278
|
|
|
} |
|
279
|
9 |
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* {@inheritdoc} |
|
282
|
|
|
*/ |
|
283
|
|
|
public function getMetadata($path) |
|
284
|
|
|
{ |
|
285
|
9 |
|
return $this->callWithFallback(null, $path, 'getMetadata'); |
|
286
|
|
|
} |
|
287
|
9 |
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* {@inheritdoc} |
|
290
|
|
|
*/ |
|
291
|
|
|
public function getSize($path) |
|
292
|
|
|
{ |
|
293
|
9 |
|
return $this->callWithFallback('size', $path, 'getSize'); |
|
294
|
|
|
} |
|
295
|
9 |
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* {@inheritdoc} |
|
298
|
|
|
*/ |
|
299
|
|
|
public function getMimetype($path) |
|
300
|
|
|
{ |
|
301
|
|
|
return $this->callWithFallback('mimetype', $path, 'getMimetype'); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* {@inheritdoc} |
|
306
|
54 |
|
*/ |
|
307
|
|
|
public function getTimestamp($path) |
|
308
|
54 |
|
{ |
|
309
|
|
|
return $this->callWithFallback('timestamp', $path, 'getTimestamp'); |
|
310
|
54 |
|
} |
|
311
|
18 |
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* {@inheritdoc} |
|
314
|
36 |
|
*/ |
|
315
|
|
|
public function getVisibility($path) |
|
316
|
36 |
|
{ |
|
317
|
18 |
|
return $this->callWithFallback('visibility', $path, 'getVisibility'); |
|
318
|
18 |
|
} |
|
319
|
18 |
|
|
|
320
|
|
|
/** |
|
321
|
36 |
|
* Call a method and cache the response. |
|
322
|
|
|
* |
|
323
|
|
|
* @param string $property |
|
324
|
|
|
* @param string $path |
|
325
|
|
|
* @param string $method |
|
326
|
|
|
* |
|
327
|
|
|
* @return mixed |
|
328
|
|
|
*/ |
|
329
|
|
|
protected function callWithFallback($property, $path, $method) |
|
330
|
|
|
{ |
|
331
|
|
|
$result = $this->cache->{$method}($path); |
|
332
|
|
|
|
|
333
|
|
|
if ($result !== false && ($property === null || array_key_exists($property, $result))) { |
|
334
|
|
|
return $result; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
$result = $this->adapter->{$method}($path); |
|
338
|
|
|
|
|
339
|
|
|
if ($result) { |
|
340
|
|
|
$object = $result + compact('path'); |
|
341
|
|
|
$this->cache->updateObject($path, $object, true); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
return $result; |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
|