1 | <?php |
||
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) |
||
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) |
||
100 | |||
101 | /** |
||
102 | * @param string $name |
||
103 | * @param DownloadStrategyInterface $security |
||
104 | */ |
||
105 | public function addDownloadStrategy($name, DownloadStrategyInterface $security) |
||
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() |
||
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) |
||
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() |
||
306 | |||
307 | /** |
||
308 | * @param ErrorElement $errorElement |
||
309 | * @param MediaInterface $media |
||
310 | */ |
||
311 | public function validate(ErrorElement $errorElement, MediaInterface $media) |
||
321 | } |
||
322 |
If you suppress an error, we recommend checking for the error condition explicitly: