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