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
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
3 |
|
public function getAdapter() |
37
|
|
|
{ |
38
|
3 |
|
return $this->adapter; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
6 |
|
public function write($path, $contents, Config $config) |
45
|
|
|
{ |
46
|
6 |
|
$result = $this->adapter->write($path, $contents, $config); |
47
|
|
|
|
48
|
6 |
|
if ($result !== false) { |
49
|
3 |
|
$this->cache->updateObject($path, $result + compact('path', 'contents'), true); |
50
|
3 |
|
} |
51
|
|
|
|
52
|
6 |
|
return $result; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
6 |
|
public function writeStream($path, $resource, Config $config) |
59
|
|
|
{ |
60
|
6 |
|
$result = $this->adapter->writeStream($path, $resource, $config); |
61
|
|
|
|
62
|
6 |
|
if ($result !== false) { |
63
|
3 |
|
$contents = false; |
64
|
3 |
|
$this->cache->updateObject($path, $result + compact('path', 'contents'), true); |
65
|
3 |
|
} |
66
|
|
|
|
67
|
6 |
|
return $result; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
6 |
|
public function update($path, $contents, Config $config) |
74
|
|
|
{ |
75
|
6 |
|
$result = $this->adapter->update($path, $contents, $config); |
76
|
|
|
|
77
|
6 |
|
if ($result !== 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 updateStream($path, $resource, Config $config) |
88
|
|
|
{ |
89
|
6 |
|
$result = $this->adapter->updateStream($path, $resource, $config); |
90
|
|
|
|
91
|
6 |
|
if ($result !== false) { |
92
|
3 |
|
$contents = false; |
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 rename($path, $newPath) |
103
|
|
|
{ |
104
|
6 |
|
$result = $this->adapter->rename($path, $newPath); |
105
|
|
|
|
106
|
6 |
|
if ($result !== false) { |
107
|
3 |
|
$this->cache->rename($path, $newPath); |
108
|
3 |
|
} |
109
|
|
|
|
110
|
6 |
|
return $result; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
6 |
|
public function copy($path, $newpath) |
117
|
|
|
{ |
118
|
6 |
|
$result = $this->adapter->copy($path, $newpath); |
119
|
|
|
|
120
|
6 |
|
if ($result !== false) { |
121
|
3 |
|
$this->cache->copy($path, $newpath); |
122
|
3 |
|
} |
123
|
|
|
|
124
|
6 |
|
return $result; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
6 |
|
public function delete($path) |
131
|
|
|
{ |
132
|
6 |
|
$result = $this->adapter->delete($path); |
133
|
|
|
|
134
|
6 |
|
if ($result !== false) { |
135
|
3 |
|
$this->cache->delete($path); |
136
|
3 |
|
} |
137
|
|
|
|
138
|
6 |
|
return $result; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
6 |
|
public function deleteDir($dirname) |
145
|
|
|
{ |
146
|
6 |
|
$result = $this->adapter->deleteDir($dirname); |
147
|
|
|
|
148
|
6 |
|
if ($result !== false) { |
149
|
3 |
|
$this->cache->deleteDir($dirname); |
150
|
3 |
|
} |
151
|
|
|
|
152
|
6 |
|
return $result; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
6 |
|
public function createDir($dirname, Config $config) |
159
|
|
|
{ |
160
|
6 |
|
$result = $this->adapter->createDir($dirname, $config); |
161
|
|
|
|
162
|
6 |
|
if ($result !== false) { |
163
|
3 |
|
$type = 'dir'; |
164
|
3 |
|
$path = $dirname; |
165
|
3 |
|
$this->cache->updateObject($dirname, compact('path', 'type'), true); |
166
|
3 |
|
} |
167
|
|
|
|
168
|
6 |
|
return $result; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
6 |
|
public function setVisibility($path, $visibility) |
175
|
|
|
{ |
176
|
6 |
|
$result = $this->adapter->setVisibility($path, $visibility); |
177
|
|
|
|
178
|
6 |
|
if ($result !== false) { |
179
|
3 |
|
$this->cache->updateObject($path, compact('path', 'visibility'), true); |
180
|
3 |
|
} |
181
|
|
|
|
182
|
6 |
|
return $result; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
12 |
|
public function has($path) |
189
|
|
|
{ |
190
|
12 |
|
$cacheHas = $this->cache->has($path); |
191
|
|
|
|
192
|
12 |
|
if ($cacheHas !== null) { |
193
|
6 |
|
return $cacheHas; |
194
|
|
|
} |
195
|
|
|
|
196
|
6 |
|
$adapterResponse = $this->adapter->has($path); |
197
|
|
|
|
198
|
6 |
|
if (! $adapterResponse) { |
199
|
3 |
|
$this->cache->storeMiss($path); |
200
|
3 |
|
} else { |
201
|
3 |
|
$cacheEntry = is_array($adapterResponse) ? $adapterResponse : compact('path'); |
202
|
3 |
|
$this->cache->updateObject($path, $cacheEntry, true); |
203
|
|
|
} |
204
|
|
|
|
205
|
6 |
|
return $adapterResponse; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
9 |
|
public function read($path) |
212
|
|
|
{ |
213
|
9 |
|
return $this->callWithFallback('read', $path); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* {@inheritdoc} |
218
|
|
|
*/ |
219
|
3 |
|
public function readStream($path) |
220
|
|
|
{ |
221
|
3 |
|
return $this->adapter->readStream($path); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* {@inheritdoc} |
226
|
|
|
*/ |
227
|
9 |
|
public function listContents($directory = '', $recursive = false) |
228
|
|
|
{ |
229
|
9 |
|
if ($this->cache->isComplete($directory, $recursive)) { |
230
|
3 |
|
return $this->cache->listContents($directory, $recursive); |
231
|
|
|
} |
232
|
|
|
|
233
|
6 |
|
$result = $this->adapter->listContents($directory, $recursive); |
234
|
|
|
|
235
|
6 |
|
if ($result) { |
|
|
|
|
236
|
3 |
|
$this->cache->storeContents($directory, $result, $recursive); |
237
|
3 |
|
} |
238
|
|
|
|
239
|
6 |
|
return $result; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* {@inheritdoc} |
244
|
|
|
*/ |
245
|
9 |
|
public function getMetadata($path) |
246
|
|
|
{ |
247
|
9 |
|
return $this->callWithFallback('getMetadata', $path); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@inheritdoc} |
252
|
|
|
*/ |
253
|
9 |
|
public function getSize($path) |
254
|
|
|
{ |
255
|
9 |
|
return $this->callWithFallback('getSize', $path); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* {@inheritdoc} |
260
|
|
|
*/ |
261
|
9 |
|
public function getMimetype($path) |
262
|
|
|
{ |
263
|
9 |
|
return $this->callWithFallback('getMimetype', $path); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
9 |
|
public function getTimestamp($path) |
270
|
|
|
{ |
271
|
9 |
|
return $this->callWithFallback('getTimestamp', $path); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* {@inheritdoc} |
276
|
|
|
*/ |
277
|
9 |
|
public function getVisibility($path) |
278
|
|
|
{ |
279
|
9 |
|
return $this->callWithFallback('getVisibility', $path); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Call a method and cache the response. |
284
|
|
|
* |
285
|
|
|
* @param string $method |
286
|
|
|
* @param string $path |
287
|
|
|
* |
288
|
|
|
* @return mixed |
289
|
|
|
*/ |
290
|
54 |
|
protected function callWithFallback($method, $path) |
291
|
|
|
{ |
292
|
54 |
|
$result = $this->cache->{$method}($path); |
293
|
|
|
|
294
|
54 |
|
if ($result !== false) { |
295
|
18 |
|
return $result; |
296
|
|
|
} |
297
|
|
|
|
298
|
36 |
|
$result = $this->adapter->{$method}($path); |
299
|
|
|
|
300
|
36 |
|
if ($result) { |
301
|
18 |
|
$object = $result + compact('path'); |
302
|
18 |
|
$this->cache->updateObject($path, $object, true); |
303
|
18 |
|
} |
304
|
|
|
|
305
|
36 |
|
return $result; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Call any method for plugin features. |
310
|
|
|
* |
311
|
|
|
* @param array $arguments [ $path ] |
312
|
|
|
* |
313
|
|
|
* @return array|false |
314
|
|
|
*/ |
315
|
|
|
public function __call($method, $arguments) |
316
|
|
|
{ |
317
|
|
|
if (method_exists($this->adapter, $method)) { |
318
|
|
|
return $this->callWithFallback($method, $arguments[0]); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
return false; |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.