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('read', $path); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritdoc} |
234
|
|
|
*/ |
235
|
3 |
|
public function readStream($path) |
236
|
|
|
{ |
237
|
3 |
|
return $this->adapter->readStream($path); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* {@inheritdoc} |
242
|
|
|
*/ |
243
|
9 |
|
public function listContents($directory = '', $recursive = false) |
244
|
|
|
{ |
245
|
9 |
|
if ($this->cache->isComplete($directory, $recursive)) { |
246
|
3 |
|
return $this->cache->listContents($directory, $recursive); |
247
|
|
|
} |
248
|
|
|
|
249
|
6 |
|
$result = $this->adapter->listContents($directory, $recursive); |
250
|
|
|
|
251
|
6 |
|
if ($result) { |
|
|
|
|
252
|
3 |
|
$this->cache->storeContents($directory, $result, $recursive); |
253
|
3 |
|
} |
254
|
|
|
|
255
|
6 |
|
return $result; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* {@inheritdoc} |
260
|
|
|
*/ |
261
|
9 |
|
public function getMetadata($path) |
262
|
|
|
{ |
263
|
9 |
|
return $this->callWithFallback('getMetadata', $path); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
9 |
|
public function getSize($path) |
270
|
|
|
{ |
271
|
9 |
|
return $this->callWithFallback('getSize', $path); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* {@inheritdoc} |
276
|
|
|
*/ |
277
|
9 |
|
public function getMimetype($path) |
278
|
|
|
{ |
279
|
9 |
|
return $this->callWithFallback('getMimetype', $path); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* {@inheritdoc} |
284
|
|
|
*/ |
285
|
9 |
|
public function getTimestamp($path) |
286
|
|
|
{ |
287
|
9 |
|
return $this->callWithFallback('getTimestamp', $path); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* {@inheritdoc} |
292
|
|
|
*/ |
293
|
9 |
|
public function getVisibility($path) |
294
|
|
|
{ |
295
|
9 |
|
return $this->callWithFallback('getVisibility', $path); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Call a method and cache the response. |
300
|
|
|
* |
301
|
|
|
* @param string $method |
302
|
|
|
* @param string $path |
303
|
|
|
* |
304
|
|
|
* @return mixed |
305
|
|
|
*/ |
306
|
54 |
|
protected function callWithFallback($method, $path) |
307
|
|
|
{ |
308
|
54 |
|
$result = $this->cache->{$method}($path); |
309
|
|
|
|
310
|
54 |
|
if ($result !== false) { |
311
|
18 |
|
return $result; |
312
|
|
|
} |
313
|
|
|
|
314
|
36 |
|
$result = $this->adapter->{$method}($path); |
315
|
|
|
|
316
|
36 |
|
if ($result) { |
317
|
18 |
|
$object = $result + compact('path'); |
318
|
18 |
|
$this->cache->updateObject($path, $object, true); |
319
|
18 |
|
} |
320
|
|
|
|
321
|
36 |
|
return $result; |
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.