|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gaufrette\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use Gaufrette\Adapter; |
|
6
|
|
|
use Gaufrette\Exception\FileNotFound; |
|
7
|
|
|
use Gaufrette\Exception\StorageFailure; |
|
8
|
|
|
use Gaufrette\Exception\UnsupportedAdapterMethodException; |
|
9
|
|
|
use League\Flysystem\AdapterInterface; |
|
10
|
|
|
use League\Flysystem\FileNotFoundException; |
|
11
|
|
|
use League\Flysystem\Util; |
|
12
|
|
|
|
|
13
|
|
|
class Flysystem implements Adapter, ListKeysAware |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var AdapterInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $adapter; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Config |
|
22
|
|
|
*/ |
|
23
|
|
|
private $config; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param AdapterInterface $adapter |
|
27
|
|
|
* @param Config|array|null $config |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(AdapterInterface $adapter, $config = null) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->adapter = $adapter; |
|
32
|
|
|
$this->config = Util::ensureConfig($config); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
View Code Duplication |
public function read($key) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
try { |
|
41
|
|
|
$result = $this->adapter->read($key); |
|
42
|
|
|
} catch (\Exception $e) { |
|
43
|
|
|
if ($e instanceof FileNotFoundException) { |
|
44
|
|
|
throw new FileNotFound($key, $e->getCode(), $e); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
throw StorageFailure::unexpectedFailure('read', ['key' => $key], $e); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (false === $result) { |
|
51
|
|
|
throw StorageFailure::unexpectedFailure('read', ['key' => $key]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $result['contents']; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function write($key, $content) |
|
61
|
|
|
{ |
|
62
|
|
|
try { |
|
63
|
|
|
$result = $this->adapter->write($key, $content, $this->config); |
|
64
|
|
|
} catch (\Exception $e) { |
|
65
|
|
|
throw StorageFailure::unexpectedFailure( |
|
66
|
|
|
'write', |
|
67
|
|
|
['key' => $key, 'content' => $content], |
|
68
|
|
|
$e |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (false === $result) { |
|
73
|
|
|
throw StorageFailure::unexpectedFailure( |
|
74
|
|
|
'write', |
|
75
|
|
|
['key' => $key, 'content' => $content] |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
public function exists($key) |
|
84
|
|
|
{ |
|
85
|
|
|
try { |
|
86
|
|
|
return $this->adapter->has($key); |
|
|
|
|
|
|
87
|
|
|
} catch (\Exception $e) { |
|
88
|
|
|
throw StorageFailure::unexpectedFailure('exists', ['key' => $key], $e); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function keys() |
|
96
|
|
|
{ |
|
97
|
|
|
try { |
|
98
|
|
|
return array_map(function ($content) { |
|
99
|
|
|
return $content['path']; |
|
100
|
|
|
}, $this->adapter->listContents()); |
|
101
|
|
|
} catch (\Exception $e) { |
|
102
|
|
|
throw StorageFailure::unexpectedFailure('keys', [], $e); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function listKeys($prefix = '') |
|
110
|
|
|
{ |
|
111
|
|
|
$dirs = []; |
|
112
|
|
|
$keys = []; |
|
113
|
|
|
|
|
114
|
|
|
try { |
|
115
|
|
|
foreach ($this->adapter->listContents() as $content) { |
|
116
|
|
|
if (empty($prefix) || 0 === strpos($content['path'], $prefix)) { |
|
117
|
|
|
if ('dir' === $content['type']) { |
|
118
|
|
|
$dirs[] = $content['path']; |
|
119
|
|
|
} else { |
|
120
|
|
|
$keys[] = $content['path']; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} catch (\Exception $e) { |
|
125
|
|
|
throw StorageFailure::unexpectedFailure( |
|
126
|
|
|
'listKeys', |
|
127
|
|
|
['prefix' => $prefix], |
|
128
|
|
|
$e |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return [ |
|
133
|
|
|
'keys' => $keys, |
|
134
|
|
|
'dirs' => $dirs, |
|
135
|
|
|
]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
|
View Code Duplication |
public function mtime($key) |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
|
|
try { |
|
144
|
|
|
$result = $this->adapter->getTimestamp($key); |
|
145
|
|
|
} catch (\Exception $e) { |
|
146
|
|
|
if ($e instanceof FileNotFoundException) { |
|
147
|
|
|
throw new FileNotFound($key, $e->getCode(), $e); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
throw StorageFailure::unexpectedFailure('mtime', ['key' => $key], $e); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if (false === $result) { |
|
154
|
|
|
throw StorageFailure::unexpectedFailure('mtime', ['key' => $key]); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $result; |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* {@inheritdoc} |
|
162
|
|
|
*/ |
|
163
|
|
View Code Duplication |
public function delete($key) |
|
|
|
|
|
|
164
|
|
|
{ |
|
165
|
|
|
try { |
|
166
|
|
|
$result = $this->adapter->delete($key); |
|
167
|
|
|
} catch (\Exception $e) { |
|
168
|
|
|
if ($e instanceof FileNotFoundException) { |
|
169
|
|
|
throw new FileNotFound($key, $e->getCode(), $e); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
throw StorageFailure::unexpectedFailure('delete', ['key' => $key], $e); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if (false === $result) { |
|
176
|
|
|
throw StorageFailure::unexpectedFailure('delete', ['key' => $key]); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* {@inheritdoc} |
|
182
|
|
|
*/ |
|
183
|
|
|
public function rename($sourceKey, $targetKey) |
|
184
|
|
|
{ |
|
185
|
|
|
try { |
|
186
|
|
|
$result = $this->adapter->rename($sourceKey, $targetKey); |
|
187
|
|
|
} catch (\Exception $e) { |
|
188
|
|
|
if ($e instanceof FileNotFoundException) { |
|
189
|
|
|
throw new FileNotFound($sourceKey, $e->getCode(), $e); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
throw StorageFailure::unexpectedFailure( |
|
193
|
|
|
'rename', |
|
194
|
|
|
['sourceKey' => $sourceKey, 'targetKey' => $targetKey], |
|
195
|
|
|
$e |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
if (false === $result) { |
|
200
|
|
|
throw StorageFailure::unexpectedFailure( |
|
201
|
|
|
'rename', |
|
202
|
|
|
['sourceKey' => $sourceKey, 'targetKey' => $targetKey] |
|
203
|
|
|
); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* {@inheritdoc} |
|
209
|
|
|
*/ |
|
210
|
|
|
public function isDirectory($key) |
|
211
|
|
|
{ |
|
212
|
|
|
throw new UnsupportedAdapterMethodException('isDirectory is not supported by this adapter.'); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..