1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gaufrette\Extras\Resolvable; |
4
|
|
|
|
5
|
|
|
use Gaufrette\FilesystemInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Filesystem decorator providing resolve() method to resolve an object path into URI. |
9
|
|
|
* |
10
|
|
|
* Example: |
11
|
|
|
* $client = // AwsS3 client instantiation |
12
|
|
|
* $decorated = new Filesystem(new AwsS3($client, 'my_bucket', ['directory' => 'root/dir'])); |
13
|
|
|
* $filesystem = new ResolvableFilesystem( |
14
|
|
|
* $decorated, |
15
|
|
|
* new AwsS3PresignedUrlResolver($client, 'my_bucket', 'root/dir') |
16
|
|
|
* ); |
17
|
|
|
* |
18
|
|
|
* try { |
19
|
|
|
* // should return something like "https://eu-west-1.blabla.aws.com/my_bucket/root/dir/foo/bar.png?token |
20
|
|
|
* var_dump($filesystem->resolve('foo/bar.png')); |
21
|
|
|
* } catch (ResolverException $e) { |
22
|
|
|
* |
23
|
|
|
* } |
24
|
|
|
* |
25
|
|
|
* @author Albin Kerouanton <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ResolvableFilesystem implements FilesystemInterface |
28
|
|
|
{ |
29
|
|
|
/** @var FilesystemInterface */ |
30
|
|
|
private $decorated; |
31
|
|
|
|
32
|
|
|
/** @var ResolverInterface */ |
33
|
|
|
private $resolver; |
34
|
|
|
|
35
|
|
|
public function __construct(FilesystemInterface $decorated, ResolverInterface $resolver) |
36
|
|
|
{ |
37
|
|
|
$this->decorated = $decorated; |
38
|
|
|
$this->resolver = $resolver; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $key Object path. |
43
|
|
|
* |
44
|
|
|
* @throws UnresolvableObjectException When not able to resolve the object path. Any exception thrown by underlying |
45
|
|
|
* resolver will be converted to this exception. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function resolve($key) |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
return $this->resolver->resolve($key); |
53
|
|
|
} catch (\Exception $e) { |
54
|
|
|
throw new UnresolvableObjectException($key, $e); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function has($key) |
62
|
|
|
{ |
63
|
|
|
return $this->decorated->has($key); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function rename($sourceKey, $targetKey) |
70
|
|
|
{ |
71
|
|
|
return $this->decorated->rename($sourceKey, $targetKey); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function get($key, $create = false) |
78
|
|
|
{ |
79
|
|
|
return $this->decorated->get($key, $create); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function write($key, $content, $overwrite = false) |
86
|
|
|
{ |
87
|
|
|
return $this->decorated->write($key, $content, $overwrite); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function read($key) |
94
|
|
|
{ |
95
|
|
|
return $this->decorated->read($key); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function delete($key) |
102
|
|
|
{ |
103
|
|
|
return $this->decorated->delete($key); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function keys() |
110
|
|
|
{ |
111
|
|
|
return $this->decorated->keys(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function listKeys($prefix = '') |
118
|
|
|
{ |
119
|
|
|
return $this->decorated->listKeys($prefix); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function mtime($key) |
126
|
|
|
{ |
127
|
|
|
return $this->decorated->mtime($key); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public function checksum($key) |
134
|
|
|
{ |
135
|
|
|
return $this->decorated->checksum($key); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function size($key) |
142
|
|
|
{ |
143
|
|
|
return $this->decorated->size($key); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
|
|
public function createStream($key) |
150
|
|
|
{ |
151
|
|
|
return $this->decorated->createStream($key); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function createFile($key) |
158
|
|
|
{ |
159
|
|
|
return $this->decorated->createFile($key); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function mimeType($key) |
166
|
|
|
{ |
167
|
|
|
return $this->decorated->mimeType($key); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function isDirectory($key) |
174
|
|
|
{ |
175
|
|
|
return $this->decorated->isDirectory($key); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
|
|
public function __call($name, $arguments) |
182
|
|
|
{ |
183
|
|
|
return call_user_func_array([$this->decorated, $name], $arguments); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|