1 | <?php |
||
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 |
||
118 | |||
119 | /** |
||
120 | * @param array $providers |
||
121 | */ |
||
122 | public function setProviders($providers): void |
||
126 | |||
127 | /** |
||
128 | * @return MediaProviderInterface[] |
||
129 | */ |
||
130 | public function getProviders() |
||
134 | |||
135 | /** |
||
136 | * @param string $name |
||
137 | */ |
||
138 | public function addContext($name, array $providers = [], array $formats = [], array $download = []): void |
||
152 | |||
153 | /** |
||
154 | * @param string $name |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function hasContext($name) |
||
162 | |||
163 | /** |
||
164 | * @param string $name |
||
165 | * |
||
166 | * @return array|null |
||
167 | */ |
||
168 | public function getContext($name) |
||
176 | |||
177 | /** |
||
178 | * Returns the context list. |
||
179 | * |
||
180 | * @return array |
||
181 | */ |
||
182 | public function getContexts() |
||
186 | |||
187 | /** |
||
188 | * @param string $name |
||
189 | * |
||
190 | * @return array|null |
||
191 | */ |
||
192 | public function getProviderNamesByContext($name) |
||
202 | |||
203 | /** |
||
204 | * @param string $name |
||
205 | * |
||
206 | * @return array|null |
||
207 | */ |
||
208 | public function getFormatNamesByContext($name) |
||
218 | |||
219 | /** |
||
220 | * @param string $name |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | public function getProvidersByContext($name) |
||
238 | |||
239 | /** |
||
240 | * @return array |
||
241 | */ |
||
242 | public function getProviderList() |
||
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) |
||
267 | |||
268 | /** |
||
269 | * @throws \RuntimeException |
||
270 | * |
||
271 | * @return DownloadStrategyInterface |
||
272 | */ |
||
273 | public function getDownloadStrategy(MediaInterface $media) |
||
290 | |||
291 | /** |
||
292 | * @return string |
||
293 | */ |
||
294 | public function getDownloadMode(MediaInterface $media) |
||
300 | |||
301 | /** |
||
302 | * @return string |
||
303 | */ |
||
304 | public function getDefaultContext() |
||
308 | |||
309 | public function validate(ErrorElement $errorElement, MediaInterface $media): void |
||
319 | } |
||
320 |
If you suppress an error, we recommend checking for the error condition explicitly: