1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gaufrette\Adapter; |
4
|
|
|
|
5
|
|
|
use Gaufrette\Adapter; |
6
|
|
|
use Gaufrette\Adapter\MetadataSupporter; |
7
|
|
|
use Gaufrette\Adapter\ListKeysAware; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Google Cloud Storage adapter using the Google Cloud Client Library for PHP |
11
|
|
|
* http://googlecloudplatform.github.io/google-cloud-php/ |
12
|
|
|
* |
13
|
|
|
* @package Gaufrette |
14
|
|
|
* @author Lech Buszczynski <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class GoogleCloudClientStorage implements Adapter, MetadataSupporter, ResourceSupporter, ListKeysAware { |
17
|
|
|
|
18
|
|
|
protected $storageClient; |
19
|
|
|
protected $bucket; |
20
|
|
|
protected $bucketValidated; |
21
|
|
|
protected $options = array(); |
22
|
|
|
protected $metadata = array(); |
23
|
|
|
protected $resource = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Google\Cloud\Storage\StorageClient $service Authenticated storage client class |
|
|
|
|
27
|
|
|
* @param string $bucketName Name of the bucket |
28
|
|
|
* @param array $options Options are: "directory" and "acl" (see https://cloud.google.com/storage/docs/access-control/lists) |
29
|
|
|
*/ |
30
|
|
|
public function __construct(\Google\Cloud\Storage\StorageClient $storageClient, $bucketName, $options = array()) |
31
|
|
|
{ |
32
|
|
|
$this->storageClient = $storageClient; |
33
|
|
|
$this->setBucket($bucketName); |
34
|
|
|
$this->options = array_replace_recursive( |
35
|
|
|
array( |
36
|
|
|
'directory' => '', |
37
|
|
|
'acl' => array() |
38
|
|
|
), |
39
|
|
|
$options |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get adapter options |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function getOptions() |
49
|
|
|
{ |
50
|
|
|
return $this->options; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set adapter options |
55
|
|
|
* |
56
|
|
|
* @param array $options |
57
|
|
|
*/ |
58
|
|
|
public function setOptions($options) |
59
|
|
|
{ |
60
|
|
|
$this->options = array_replace($this->options, $options); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function computePath($key) |
64
|
|
|
{ |
65
|
|
|
if (strlen($this->options['directory'])) |
66
|
|
|
{ |
67
|
|
|
if (strcmp(substr($this->options['directory'], -1), '/') == 0) |
68
|
|
|
{ |
69
|
|
|
return $this->options['directory'].$key; |
70
|
|
|
} |
71
|
|
|
return $this->options['directory'].'/'.$key; |
72
|
|
|
} |
73
|
|
|
return $key; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function isBucket() |
77
|
|
|
{ |
78
|
|
|
if ($this->bucketValidated === true) |
79
|
|
|
{ |
80
|
|
|
return true; |
81
|
|
|
} elseif (!$this->bucket->exists()) { |
82
|
|
|
throw new \RuntimeException(sprintf('Bucket %s does not exist.', $this->bucket->name())); |
83
|
|
|
} |
84
|
|
|
$this->bucketValidated = true; |
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function setBucket($name) |
89
|
|
|
{ |
90
|
|
|
$this->bucketValidated = null; |
91
|
|
|
$this->bucket = $this->storageClient->bucket($name); |
92
|
|
|
$this->isBucket(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getBucket() |
96
|
|
|
{ |
97
|
|
|
return $this->bucket; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function read($key) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$this->isBucket(); |
106
|
|
|
$object = $this->bucket->object($this->computePath($key)); |
107
|
|
|
$info = $object->info(); |
108
|
|
|
$this->setResource($key, $info); |
109
|
|
|
return $object->downloadAsString(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function write($key, $content) |
116
|
|
|
{ |
117
|
|
|
$this->isBucket(); |
118
|
|
|
|
119
|
|
|
$options = array( |
120
|
|
|
'resumable' => true, |
121
|
|
|
'name' => $this->computePath($key), |
122
|
|
|
'metadata' => $this->getMetadata($key), |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$object = $this->bucket->upload( |
126
|
|
|
$content, |
127
|
|
|
$options |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$acl = $object->acl(); |
131
|
|
|
foreach ($this->options['acl'] as $k => $v) |
132
|
|
|
{ |
133
|
|
|
$acl->add($k, $v); |
134
|
|
|
} |
135
|
|
|
$object->update(array('metadata' => $this->getMetadata($key))); |
136
|
|
|
|
137
|
|
|
$info = $object->info(); |
138
|
|
|
$this->setResource($key, $info); |
139
|
|
|
return $info['size']; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function exists($key) |
146
|
|
|
{ |
147
|
|
|
$this->isBucket(); |
148
|
|
|
$object = $this->bucket->object($this->computePath($key)); |
149
|
|
|
if ($object->exists()) |
150
|
|
|
{ |
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
|
|
public function isDirectory($key) |
160
|
|
|
{ |
161
|
|
|
if ($this->exists($key . '/')) |
162
|
|
|
{ |
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
public function listKeys($prefix = null) |
172
|
|
|
{ |
173
|
|
|
$this->isBucket(); |
174
|
|
|
$keys = array(); |
175
|
|
|
if ($prefix === null) |
176
|
|
|
{ |
177
|
|
|
$prefix = $this->options['directory']; |
178
|
|
|
} else { |
179
|
|
|
$prefix = $this->computePath($prefix); |
180
|
|
|
} |
181
|
|
|
foreach ($this->bucket->objects(array('prefix' => $prefix)) as $e) |
182
|
|
|
{ |
183
|
|
|
$keys[] = $e->name(); |
184
|
|
|
} |
185
|
|
|
sort($keys); |
186
|
|
|
return $keys; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
|
|
public function keys() |
193
|
|
|
{ |
194
|
|
|
return $this->listKeys(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
|
View Code Duplication |
public function mtime($key) |
|
|
|
|
201
|
|
|
{ |
202
|
|
|
$this->isBucket(); |
203
|
|
|
$object = $object = $this->bucket->object($this->computePath($key)); |
204
|
|
|
$info = $object->info(); |
205
|
|
|
$this->setResource($key, $info); |
206
|
|
|
return strtotime($info['updated']); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritdoc} |
211
|
|
|
*/ |
212
|
|
|
public function delete($key) |
213
|
|
|
{ |
214
|
|
|
$object = $this->bucket->object($this->computePath($key)); |
215
|
|
|
$object->delete(); |
216
|
|
|
return true; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* {@inheritdoc} |
221
|
|
|
*/ |
222
|
|
|
public function rename($sourceKey, $targetKey) |
223
|
|
|
{ |
224
|
|
|
# there is no support for rename currently in Google Cloud Client Library 0.7.1 - it will be added in v0.8 any time now |
225
|
|
|
return false; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* {@inheritdoc} |
230
|
|
|
*/ |
231
|
|
|
public function setMetadata($key, $metadata) |
232
|
|
|
{ |
233
|
|
|
$this->metadata[$key] = $metadata; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* {@inheritdoc} |
238
|
|
|
*/ |
239
|
|
|
public function getMetadata($key) |
240
|
|
|
{ |
241
|
|
|
return isset($this->metadata[$key]) ? $this->metadata[$key] : array(); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* {@inheritdoc} |
246
|
|
|
*/ |
247
|
|
|
public function setResource($key, $data) |
248
|
|
|
{ |
249
|
|
|
$this->resource[$key] = $data; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* {@inheritdoc} |
254
|
|
|
*/ |
255
|
|
|
public function getResource($key, $name = null) |
256
|
|
|
{ |
257
|
|
|
if (isset($this->resource[$key])) |
258
|
|
|
{ |
259
|
|
|
if ($name) |
260
|
|
|
{ |
261
|
|
|
return isset($this->resource[$key][$name]) ? $this->resource[$key][$name] : null; |
262
|
|
|
} elseif (isset($this->resource[$key])) { |
263
|
|
|
return isset($this->resource[$key]) ? $this->resource[$key] : array(); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
return array(); |
267
|
|
|
} |
268
|
|
|
} |
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.