|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\MediaBundle\Provider; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\CoreBundle\Validator\ErrorElement; |
|
15
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
|
16
|
|
|
use Sonata\MediaBundle\Security\DownloadStrategyInterface; |
|
17
|
|
|
|
|
18
|
|
|
class Pool |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $providers = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $contexts = array(); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @deprecated Deprecated since version 3.1 and will be removed in 4.0. Use $downloadStrategies instead |
|
32
|
|
|
* |
|
33
|
|
|
* @var DownloadStrategyInterface[] |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $downloadSecurities = array(); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var DownloadStrategyInterface[] |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $downloadStrategies = array(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $defaultContext; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $context |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct($context) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->defaultContext = $context; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @throws \RuntimeException |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
* |
|
60
|
|
|
* @return MediaProviderInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getProvider($name) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!$name) { |
|
65
|
|
|
throw new \InvalidArgumentException('Provider name cannot be empty, did you forget to call setProviderName() in your Media object?'); |
|
66
|
|
|
} |
|
67
|
|
|
if (empty($this->providers)) { |
|
68
|
|
|
throw new \RuntimeException(sprintf('Unable to retrieve provider named "%s" since there are no providers configured yet.', $name)); |
|
69
|
|
|
} |
|
70
|
|
|
if (!isset($this->providers[$name])) { |
|
71
|
|
|
throw new \InvalidArgumentException(sprintf('Unable to retrieve the provider named "%s". Available providers are %s.', $name, '"'.implode('", "', $this->getProviderList()).'"')); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->providers[$name]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $name |
|
79
|
|
|
* @param MediaProviderInterface $instance |
|
80
|
|
|
*/ |
|
81
|
|
|
public function addProvider($name, MediaProviderInterface $instance) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->providers[$name] = $instance; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @deprecated Deprecated since version 3.1, to be removed in 4.0 |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $name |
|
90
|
|
|
* @param DownloadStrategyInterface $security |
|
91
|
|
|
*/ |
|
92
|
|
|
public function addDownloadSecurity($name, DownloadStrategyInterface $security) |
|
93
|
|
|
{ |
|
94
|
|
|
@trigger_error('The '.__METHOD__.' method is deprecated since version 3.1 and will be removed in 4.0.', E_USER_DEPRECATED); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
$this->downloadSecurities[$name] = $security; |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
$this->addDownloadStrategy($name, $security); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $name |
|
103
|
|
|
* @param DownloadStrategyInterface $security |
|
104
|
|
|
*/ |
|
105
|
|
|
public function addDownloadStrategy($name, DownloadStrategyInterface $security) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->downloadStrategies[$name] = $security; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param array $providers |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setProviders($providers) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->providers = $providers; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return MediaProviderInterface[] |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getProviders() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->providers; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string $name |
|
128
|
|
|
* @param array $providers |
|
129
|
|
|
* @param array $formats |
|
130
|
|
|
* @param array $download |
|
131
|
|
|
*/ |
|
132
|
|
|
public function addContext($name, array $providers = array(), array $formats = array(), array $download = array()) |
|
133
|
|
|
{ |
|
134
|
|
|
if (!$this->hasContext($name)) { |
|
135
|
|
|
$this->contexts[$name] = array( |
|
136
|
|
|
'providers' => array(), |
|
137
|
|
|
'formats' => array(), |
|
138
|
|
|
'download' => array(), |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$this->contexts[$name]['providers'] = $providers; |
|
143
|
|
|
$this->contexts[$name]['formats'] = $formats; |
|
144
|
|
|
$this->contexts[$name]['download'] = $download; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string $name |
|
149
|
|
|
* |
|
150
|
|
|
* @return bool |
|
151
|
|
|
*/ |
|
152
|
|
|
public function hasContext($name) |
|
153
|
|
|
{ |
|
154
|
|
|
return isset($this->contexts[$name]); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param string $name |
|
159
|
|
|
* |
|
160
|
|
|
* @return array|null |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getContext($name) |
|
163
|
|
|
{ |
|
164
|
|
|
if (!$this->hasContext($name)) { |
|
165
|
|
|
return; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $this->contexts[$name]; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Returns the context list. |
|
173
|
|
|
* |
|
174
|
|
|
* @return array |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getContexts() |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->contexts; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param string $name |
|
183
|
|
|
* |
|
184
|
|
|
* @return array |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getProviderNamesByContext($name) |
|
187
|
|
|
{ |
|
188
|
|
|
$context = $this->getContext($name); |
|
189
|
|
|
|
|
190
|
|
|
if (!$context) { |
|
191
|
|
|
return; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return $context['providers']; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param string $name |
|
199
|
|
|
* |
|
200
|
|
|
* @return array |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getFormatNamesByContext($name) |
|
203
|
|
|
{ |
|
204
|
|
|
$context = $this->getContext($name); |
|
205
|
|
|
|
|
206
|
|
|
if (!$context) { |
|
207
|
|
|
return; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return $context['formats']; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param string $name |
|
215
|
|
|
* |
|
216
|
|
|
* @return array |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getProvidersByContext($name) |
|
219
|
|
|
{ |
|
220
|
|
|
$providers = array(); |
|
221
|
|
|
|
|
222
|
|
|
if (!$this->hasContext($name)) { |
|
223
|
|
|
return $providers; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
foreach ($this->getProviderNamesByContext($name) as $name) { |
|
227
|
|
|
$providers[] = $this->getProvider($name); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return $providers; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return array |
|
235
|
|
|
*/ |
|
236
|
|
|
public function getProviderList() |
|
237
|
|
|
{ |
|
238
|
|
|
$choices = array(); |
|
239
|
|
|
foreach (array_keys($this->providers) as $name) { |
|
240
|
|
|
$choices[$name] = $name; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
return $choices; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @deprecated Deprecated since version 3.1, to be removed in 4.0 |
|
248
|
|
|
* |
|
249
|
|
|
* @param MediaInterface $media |
|
250
|
|
|
* |
|
251
|
|
|
* @return DownloadStrategyInterface |
|
252
|
|
|
* |
|
253
|
|
|
* @throws \RuntimeException |
|
254
|
|
|
*/ |
|
255
|
|
|
public function getDownloadSecurity(MediaInterface $media) |
|
256
|
|
|
{ |
|
257
|
|
|
@trigger_error('The '.__METHOD__.' method is deprecated since version 3.1 and will be removed in 4.0.', E_USER_DEPRECATED); |
|
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
return array_merge($this->getDownloadSecurity($media), $this->getDownloadStrategy($media)); |
|
|
|
|
|
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @param MediaInterface $media |
|
264
|
|
|
* |
|
265
|
|
|
* @return DownloadStrategyInterface |
|
266
|
|
|
* |
|
267
|
|
|
* @throws \RuntimeException |
|
268
|
|
|
*/ |
|
269
|
|
|
public function getDownloadStrategy(MediaInterface $media) |
|
270
|
|
|
{ |
|
271
|
|
|
$context = $this->getContext($media->getContext()); |
|
272
|
|
|
|
|
273
|
|
|
$id = $context['download']['strategy']; |
|
274
|
|
|
|
|
275
|
|
|
// NEXT_MAJOR: remove this line with the next major release. |
|
276
|
|
|
if (isset($this->downloadSecurities[$id])) { |
|
|
|
|
|
|
277
|
|
|
return $this->downloadSecurities[$id]; |
|
|
|
|
|
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
if (!isset($this->downloadStrategies[$id])) { |
|
281
|
|
|
throw new \RuntimeException('Unable to retrieve the download security : '.$id); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
return $this->downloadStrategies[$id]; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* @param MediaInterface $media |
|
289
|
|
|
* |
|
290
|
|
|
* @return string |
|
291
|
|
|
*/ |
|
292
|
|
|
public function getDownloadMode(MediaInterface $media) |
|
293
|
|
|
{ |
|
294
|
|
|
$context = $this->getContext($media->getContext()); |
|
295
|
|
|
|
|
296
|
|
|
return $context['download']['mode']; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* @return string |
|
301
|
|
|
*/ |
|
302
|
|
|
public function getDefaultContext() |
|
303
|
|
|
{ |
|
304
|
|
|
return $this->defaultContext; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @param ErrorElement $errorElement |
|
309
|
|
|
* @param MediaInterface $media |
|
310
|
|
|
*/ |
|
311
|
|
|
public function validate(ErrorElement $errorElement, MediaInterface $media) |
|
312
|
|
|
{ |
|
313
|
|
|
if (!$media->getProviderName()) { |
|
314
|
|
|
return; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
$provider = $this->getProvider($media->getProviderName()); |
|
318
|
|
|
|
|
319
|
|
|
$provider->validate($errorElement, $media); |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: