1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gaufrette\Adapter; |
4
|
|
|
|
5
|
|
|
use Gaufrette\Adapter; |
6
|
|
|
use Gaufrette\Adapter\MetadataSupporter; |
7
|
|
|
use Gaufrette\Adapter\ResourcesSupporter; |
8
|
|
|
use Gaufrette\Adapter\ListKeysAware; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Google Cloud Storage adapter using the Google Cloud Client Library for PHP |
12
|
|
|
* http://googlecloudplatform.github.io/google-cloud-php/ |
13
|
|
|
* |
14
|
|
|
* @package Gaufrette |
15
|
|
|
* @author Lech Buszczynski <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class GoogleCloudClientStorage implements Adapter, MetadataSupporter, ResourcesSupporter, ListKeysAware { |
18
|
|
|
|
19
|
|
|
protected $storageClient; |
20
|
|
|
protected $bucket; |
21
|
|
|
protected $bucketValidated; |
22
|
|
|
protected $options = array(); |
23
|
|
|
protected $metadata = array(); |
24
|
|
|
protected $resources = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Google\Cloud\Storage\StorageClient $service Authenticated storage client class |
|
|
|
|
28
|
|
|
* @param string $bucketName Name of the bucket |
29
|
|
|
* @param array $options Options are: "directory" and "acl" (see https://cloud.google.com/storage/docs/access-control/lists) |
30
|
|
|
*/ |
31
|
|
|
public function __construct(\Google\Cloud\Storage\StorageClient $storageClient, $bucketName, $options = array()) |
32
|
|
|
{ |
33
|
|
|
$this->storageClient = $storageClient; |
34
|
|
|
$this->setBucket($bucketName); |
35
|
|
|
$this->options = array_replace_recursive( |
36
|
|
|
array( |
37
|
|
|
'directory' => '', |
38
|
|
|
'acl' => array() |
39
|
|
|
), |
40
|
|
|
$options |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get adapter options |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getOptions() |
50
|
|
|
{ |
51
|
|
|
return $this->options; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set adapter options |
56
|
|
|
* |
57
|
|
|
* @param array $options |
58
|
|
|
*/ |
59
|
|
|
public function setOptions($options) |
60
|
|
|
{ |
61
|
|
|
$this->options = array_replace($this->options, $options); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function computePath($key) |
65
|
|
|
{ |
66
|
|
|
if (strlen($this->options['directory'])) |
67
|
|
|
{ |
68
|
|
|
if (strcmp(substr($this->options['directory'], -1), '/') == 0) |
69
|
|
|
{ |
70
|
|
|
return $this->options['directory'].$key; |
71
|
|
|
} |
72
|
|
|
return $this->options['directory'].'/'.$key; |
73
|
|
|
} |
74
|
|
|
return $key; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function isBucket() |
78
|
|
|
{ |
79
|
|
|
if ($this->bucketValidated === true) |
80
|
|
|
{ |
81
|
|
|
return true; |
82
|
|
|
} elseif (!$this->bucket->exists()) { |
83
|
|
|
throw new \RuntimeException(sprintf('Bucket %s does not exist.', $this->bucket->name())); |
84
|
|
|
} |
85
|
|
|
$this->bucketValidated = true; |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function setBucket($name) |
90
|
|
|
{ |
91
|
|
|
$this->bucketValidated = null; |
92
|
|
|
$this->bucket = $this->storageClient->bucket($name); |
93
|
|
|
$this->isBucket(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getBucket() |
97
|
|
|
{ |
98
|
|
|
return $this->bucket; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function read($key) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$this->isBucket(); |
107
|
|
|
$object = $this->bucket->object($this->computePath($key)); |
108
|
|
|
$info = $object->info(); |
109
|
|
|
$this->setResources($key, $info); |
110
|
|
|
return $object->downloadAsString(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function write($key, $content) |
117
|
|
|
{ |
118
|
|
|
$this->isBucket(); |
119
|
|
|
|
120
|
|
|
$options = array( |
121
|
|
|
'resumable' => true, |
122
|
|
|
'name' => $this->computePath($key), |
123
|
|
|
'metadata' => $this->getMetadata($key), |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$this->bucket->upload( |
127
|
|
|
$content, |
128
|
|
|
$options |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$this->updateKeyProperties($key, |
132
|
|
|
array( |
133
|
|
|
'acl' => $this->options['acl'], |
134
|
|
|
'metadata' => $this->getMetadata($key) |
135
|
|
|
) |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$size = $this->getResourceByName($key, 'size'); |
139
|
|
|
|
140
|
|
|
return $size === null ? false : $size; |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function exists($key) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$this->isBucket(); |
149
|
|
|
$object = $this->bucket->object($this->computePath($key)); |
150
|
|
|
if ($object->exists()) |
151
|
|
|
{ |
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
public function isDirectory($key) |
161
|
|
|
{ |
162
|
|
|
if ($this->exists($key . '/')) |
163
|
|
|
{ |
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function listKeys($prefix = null) |
173
|
|
|
{ |
174
|
|
|
$this->isBucket(); |
175
|
|
|
$keys = array(); |
176
|
|
|
if ($prefix === null) |
177
|
|
|
{ |
178
|
|
|
$prefix = $this->options['directory']; |
179
|
|
|
} else { |
180
|
|
|
$prefix = $this->computePath($prefix); |
181
|
|
|
} |
182
|
|
|
foreach ($this->bucket->objects(array('prefix' => $prefix)) as $e) |
183
|
|
|
{ |
184
|
|
|
$keys[] = $e->name(); |
185
|
|
|
} |
186
|
|
|
sort($keys); |
187
|
|
|
return $keys; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
|
|
public function keys() |
194
|
|
|
{ |
195
|
|
|
return $this->listKeys(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
*/ |
201
|
|
View Code Duplication |
public function mtime($key) |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
$this->isBucket(); |
204
|
|
|
$object = $object = $this->bucket->object($this->computePath($key)); |
205
|
|
|
$info = $object->info(); |
206
|
|
|
$this->setResources($key, $info); |
207
|
|
|
return strtotime($info['updated']); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritdoc} |
212
|
|
|
*/ |
213
|
|
View Code Duplication |
public function delete($key) |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
$this->isBucket(); |
216
|
|
|
$object = $this->bucket->object($this->computePath($key)); |
217
|
|
|
$object->delete(); |
218
|
|
|
$this->setMetadata($key, null); |
|
|
|
|
219
|
|
|
return true; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritdoc} |
224
|
|
|
*/ |
225
|
|
|
public function rename($sourceKey, $targetKey) |
226
|
|
|
{ |
227
|
|
|
$this->isBucket(); |
228
|
|
|
|
229
|
|
|
$metadata = $this->getMetadata($sourceKey); |
|
|
|
|
230
|
|
|
|
231
|
|
|
$sourceKey = $this->computePath($sourceKey); |
232
|
|
|
$targetKey = $this->computePath($targetKey); |
233
|
|
|
|
234
|
|
|
$object = $this->bucket->object($sourceKey); |
235
|
|
|
|
236
|
|
|
$copiedObject = $object->copy($this->bucket, |
237
|
|
|
array( |
238
|
|
|
'name' => $targetKey |
239
|
|
|
) |
240
|
|
|
); |
241
|
|
|
|
242
|
|
|
if ($copiedObject->exists()) |
243
|
|
|
{ |
244
|
|
|
$this->updateKeyProperties($targetKey, |
245
|
|
|
array( |
246
|
|
|
'acl' => $this->options['acl'], |
247
|
|
|
'metadata' => $this->getMetadata($sourceKey) |
248
|
|
|
) |
249
|
|
|
); |
250
|
|
|
$this->setMetadata($targetKey, $this->getMetadata($sourceKey)); |
251
|
|
|
$this->setMetadata($sourceKey, null); |
|
|
|
|
252
|
|
|
$object->delete(); |
253
|
|
|
return true; |
254
|
|
|
} |
255
|
|
|
return false; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* {@inheritdoc} |
260
|
|
|
*/ |
261
|
|
|
public function setMetadata($key, $metadata) |
262
|
|
|
{ |
263
|
|
|
$this->metadata[$key] = $metadata; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
|
|
public function getMetadata($key) |
270
|
|
|
{ |
271
|
|
|
return isset($this->metadata[$key]) ? $this->metadata[$key] : array(); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* {@inheritdoc} |
276
|
|
|
*/ |
277
|
|
|
public function setResources($key, $data) |
278
|
|
|
{ |
279
|
|
|
$this->resources[$key] = $data; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* {@inheritdoc} |
284
|
|
|
*/ |
285
|
|
|
public function getResources($key) |
286
|
|
|
{ |
287
|
|
|
return isset($this->resources[$key]) ? $this->resources[$key] : array(); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* {@inheritdoc} |
292
|
|
|
*/ |
293
|
|
|
public function getResourceByName($key, $resourceName) |
294
|
|
|
{ |
295
|
|
|
return isset($this->resources[$key][$resourceName]) ? $this->resources[$key][$resourceName] : null; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Sets ACL and metadata information. |
300
|
|
|
* |
301
|
|
|
* @param string $key |
302
|
|
|
* @param array $options Can contain "acl" and/or "metadata" arrays. |
303
|
|
|
* @return boolean |
304
|
|
|
*/ |
305
|
|
|
protected function updateKeyProperties($key, $options = array()) |
306
|
|
|
{ |
307
|
|
|
if ($this->exists($key) === false) |
308
|
|
|
{ |
309
|
|
|
return false; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$object = $this->bucket->object($this->computePath($key)); |
313
|
|
|
|
314
|
|
|
$properties = array_replace_recursive( |
315
|
|
|
array( |
316
|
|
|
'acl' => array(), |
317
|
|
|
'metadata' => array() |
318
|
|
|
), $options |
319
|
|
|
); |
320
|
|
|
|
321
|
|
|
$acl = $object->acl(); |
322
|
|
|
foreach ($properties['acl'] as $k => $v) |
323
|
|
|
{ |
324
|
|
|
$acl->add($k, $v); |
325
|
|
|
} |
326
|
|
|
$object->update(array('metadata' => $properties['metadata'])); |
327
|
|
|
|
328
|
|
|
$info = $object->info(); |
329
|
|
|
|
330
|
|
|
$this->setResources($key, $info); |
331
|
|
|
return true; |
332
|
|
|
} |
333
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.