1 | <?php |
||
20 | class Configuration implements ConfigurationInterface |
||
21 | { |
||
22 | /** |
||
23 | * Whether to use the debug mode. |
||
24 | * |
||
25 | * @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41 |
||
26 | * |
||
27 | * @var bool |
||
28 | */ |
||
29 | private $debug; |
||
30 | |||
31 | /** |
||
32 | * @param bool $debug |
||
33 | */ |
||
34 | 14 | public function __construct($debug) |
|
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | 14 | public function getConfigTreeBuilder() |
|
153 | |||
154 | 14 | private function configureClients(ArrayNodeDefinition $root) |
|
155 | { |
||
156 | 14 | $pluginNode = $root->children() |
|
157 | 14 | ->arrayNode('clients') |
|
158 | 14 | ->validate() |
|
159 | ->ifTrue(function ($clients) { |
||
160 | 5 | foreach ($clients as $name => $config) { |
|
161 | // Make sure we only allow one of these to be true |
||
162 | 5 | return (bool) $config['flexible_client'] + (bool) $config['http_methods_client'] + (bool) $config['batch_client'] >= 2; |
|
163 | } |
||
164 | |||
165 | return false; |
||
166 | 14 | }) |
|
167 | 14 | ->thenInvalid('A http client can\'t be decorated with both FlexibleHttpClient and HttpMethodsClient. Only one of the following options can be true. ("flexible_client", "http_methods_client")')->end() |
|
168 | 14 | ->useAttributeAsKey('name') |
|
169 | 14 | ->prototype('array') |
|
170 | 14 | ->fixXmlConfig('plugin') |
|
171 | 14 | ->children() |
|
172 | 14 | ->scalarNode('factory') |
|
173 | 14 | ->isRequired() |
|
174 | 14 | ->cannotBeEmpty() |
|
175 | 14 | ->info('The service id of a factory to use when creating the adapter.') |
|
176 | 14 | ->end() |
|
177 | 14 | ->booleanNode('flexible_client') |
|
178 | 14 | ->defaultFalse() |
|
179 | 14 | ->info('Set to true to get the client wrapped in a FlexibleHttpClient which emulates async or sync behavior.') |
|
180 | 14 | ->end() |
|
181 | 14 | ->booleanNode('http_methods_client') |
|
182 | 14 | ->defaultFalse() |
|
183 | 14 | ->info('Set to true to get the client wrapped in a HttpMethodsClient which emulates provides functions for HTTP verbs.') |
|
184 | 14 | ->end() |
|
185 | 14 | ->booleanNode('batch_client') |
|
186 | 14 | ->defaultFalse() |
|
187 | 14 | ->info('Set to true to get the client wrapped in a BatchClient which allows you to send multiple request at the same time.') |
|
188 | 14 | ->end() |
|
189 | 14 | ->variableNode('config')->defaultValue([])->end() |
|
190 | 14 | ->arrayNode('plugins') |
|
191 | 14 | ->info('A list of plugin service ids and client specific plugin definitions. The order is important.') |
|
192 | 14 | ->prototype('array') |
|
193 | 14 | ; |
|
194 | 14 | ||
195 | 14 | $this->configureClientPlugins($pluginNode); |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param ArrayNodeDefinition $root |
||
200 | 14 | */ |
|
201 | private function configureSharedPlugins(ArrayNodeDefinition $root) |
||
202 | 14 | { |
|
203 | 14 | $pluginsNode = $root |
|
204 | 14 | ->children() |
|
205 | 14 | ->arrayNode('plugins') |
|
206 | 14 | ->info('Global plugin configuration. Plugins need to be explicitly added to clients.') |
|
207 | ->addDefaultsIfNotSet() |
||
208 | 14 | ; |
|
209 | 14 | $this->addSharedPluginNodes($pluginsNode); |
|
210 | 14 | } |
|
211 | 14 | ||
212 | 14 | /** |
|
213 | 14 | * Configure plugins node of a client. |
|
214 | 14 | * |
|
215 | 14 | * @param ArrayNodeDefinition $pluginNode The node to add plugin definitions to. |
|
216 | 14 | */ |
|
217 | 14 | private function configureClientPlugins(ArrayNodeDefinition $pluginNode) |
|
218 | 14 | { |
|
219 | 14 | $pluginNode |
|
220 | 14 | // support having just a service id in the list |
|
221 | 14 | ->beforeNormalization() |
|
222 | 14 | ->always(function ($plugin) { |
|
223 | 14 | if (is_string($plugin)) { |
|
224 | 14 | return [ |
|
225 | 14 | 'reference' => [ |
|
226 | 14 | 'enabled' => true, |
|
227 | 14 | 'id' => $plugin, |
|
228 | 14 | ], |
|
229 | 14 | ]; |
|
230 | 14 | } |
|
231 | |||
232 | 14 | return $plugin; |
|
233 | 14 | }) |
|
234 | 14 | ->end() |
|
235 | 14 | ||
236 | 14 | ->validate() |
|
237 | 14 | ->always(function ($plugins) { |
|
238 | 14 | foreach ($plugins as $name => $definition) { |
|
239 | 14 | if ('authentication' === $name) { |
|
240 | 14 | if (!count($definition)) { |
|
241 | 14 | unset($plugins['authentication']); |
|
242 | } |
||
243 | 14 | } elseif (!$definition['enabled']) { |
|
244 | 14 | unset($plugins[$name]); |
|
245 | 14 | } |
|
246 | 14 | } |
|
247 | 14 | ||
248 | 14 | return $plugins; |
|
249 | 14 | }) |
|
250 | ->end() |
||
251 | 14 | ; |
|
252 | 14 | $this->addSharedPluginNodes($pluginNode, true); |
|
253 | 14 | ||
254 | 14 | $pluginNode |
|
255 | 14 | ->children() |
|
256 | 14 | ->arrayNode('reference') |
|
257 | 14 | ->canBeEnabled() |
|
258 | 14 | ->info('Reference to a plugin service') |
|
259 | 14 | ->children() |
|
260 | 14 | ->scalarNode('id') |
|
261 | ->info('Service id of a plugin') |
||
262 | 14 | ->isRequired() |
|
263 | 14 | ->cannotBeEmpty() |
|
264 | 14 | ->end() |
|
265 | 14 | ->end() |
|
266 | 14 | ->end() |
|
267 | 14 | ->arrayNode('add_host') |
|
268 | 14 | ->canBeEnabled() |
|
269 | 14 | ->addDefaultsIfNotSet() |
|
270 | 14 | ->info('Configure the AddHostPlugin for this client.') |
|
271 | 14 | ->children() |
|
272 | 14 | ->scalarNode('host') |
|
273 | 14 | ->info('Host name including protocol and optionally the port number, e.g. https://api.local:8000') |
|
274 | 14 | ->isRequired() |
|
275 | 14 | ->cannotBeEmpty() |
|
276 | 14 | ->end() |
|
277 | ->scalarNode('replace') |
||
278 | 14 | ->info('Whether to replace the host if request already specifies it') |
|
279 | 14 | ->defaultValue(false) |
|
280 | 14 | ->end() |
|
281 | 14 | ->end() |
|
282 | 14 | ->end() |
|
283 | 14 | ->arrayNode('header_append') |
|
284 | 14 | ->canBeEnabled() |
|
285 | 14 | ->info('Append headers to the request. If the header already exists the value will be appended to the current value.') |
|
286 | ->fixXmlConfig('header') |
||
287 | 14 | ->children() |
|
288 | 14 | ->arrayNode('headers') |
|
289 | 14 | ->info('Keys are the header names, values the header values') |
|
290 | 14 | ->normalizeKeys(false) |
|
291 | 14 | ->useAttributeAsKey('name') |
|
292 | 14 | ->prototype('scalar')->end() |
|
293 | 14 | ->end() |
|
294 | ->end() |
||
295 | 14 | ->end() |
|
296 | 14 | ->arrayNode('header_defaults') |
|
297 | 14 | ->canBeEnabled() |
|
298 | 14 | ->info('Set header to default value if it does not exist.') |
|
299 | 14 | ->fixXmlConfig('header') |
|
300 | 14 | ->children() |
|
301 | 14 | ->arrayNode('headers') |
|
302 | 14 | ->info('Keys are the header names, values the header values') |
|
303 | 14 | ->normalizeKeys(false) |
|
304 | 14 | ->useAttributeAsKey('name') |
|
305 | 14 | ->prototype('scalar')->end() |
|
306 | ->end() |
||
307 | 14 | ->end() |
|
308 | 14 | ->end() |
|
309 | 14 | ->arrayNode('header_set') |
|
310 | 14 | ->canBeEnabled() |
|
311 | ->info('Set headers to requests. If the header does not exist it wil be set, if the header already exists it will be replaced.') |
||
312 | ->fixXmlConfig('header') |
||
313 | ->children() |
||
314 | ->arrayNode('headers') |
||
315 | ->info('Keys are the header names, values the header values') |
||
316 | ->normalizeKeys(false) |
||
317 | 14 | ->useAttributeAsKey('name') |
|
318 | ->prototype('scalar')->end() |
||
319 | 14 | ->end() |
|
320 | 14 | ->end() |
|
321 | ->end() |
||
322 | 14 | ->arrayNode('header_remove') |
|
323 | 14 | ->canBeEnabled() |
|
324 | 14 | ->info('Remove headers from requests.') |
|
325 | 14 | ->fixXmlConfig('header') |
|
326 | 14 | ->children() |
|
327 | 2 | ->arrayNode('headers') |
|
328 | 2 | ->info('List of header names to remove') |
|
329 | 1 | ->prototype('scalar')->end() |
|
330 | 1 | ->end() |
|
331 | 2 | ->end() |
|
332 | 1 | ->end() |
|
333 | 1 | ->end() |
|
334 | 2 | ->end(); |
|
335 | 2 | } |
|
336 | 1 | ||
337 | 1 | /** |
|
338 | 1 | * Add the definitions for shared plugin configurations. |
|
339 | 1 | * |
|
340 | 1 | * @param ArrayNodeDefinition $pluginNode The node to add to. |
|
341 | * @param bool $disableAll Some shared plugins are enabled by default. On the client, all are disabled by default. |
||
342 | 1 | */ |
|
343 | 14 | private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableAll = false) |
|
344 | 14 | { |
|
345 | 14 | $children = $pluginNode->children(); |
|
346 | 14 | ||
347 | 14 | $children->append($this->createAuthenticationPluginNode()); |
|
348 | 14 | ||
349 | 14 | $children->arrayNode('cache') |
|
350 | 14 | ->canBeEnabled() |
|
351 | 14 | ->addDefaultsIfNotSet() |
|
352 | 14 | ->children() |
|
353 | 14 | ->scalarNode('cache_pool') |
|
354 | 14 | ->info('This must be a service id to a service implementing Psr\Cache\CacheItemPoolInterface') |
|
355 | 14 | ->isRequired() |
|
356 | 14 | ->cannotBeEmpty() |
|
357 | 14 | ->end() |
|
358 | ->scalarNode('stream_factory') |
||
359 | 14 | ->info('This must be a service id to a service implementing Http\Message\StreamFactory') |
|
360 | ->defaultValue('httplug.stream_factory') |
||
361 | ->cannotBeEmpty() |
||
362 | ->end() |
||
363 | ->arrayNode('config') |
||
364 | ->addDefaultsIfNotSet() |
||
365 | ->children() |
||
366 | ->scalarNode('default_ttl')->defaultNull()->end() |
||
367 | ->scalarNode('respect_cache_headers')->defaultTrue()->end() |
||
368 | ->end() |
||
369 | ->end() |
||
370 | ->end() |
||
371 | 2 | ->end(); |
|
372 | // End cache plugin |
||
373 | 2 | ||
374 | 2 | $children->arrayNode('cookie') |
|
375 | 2 | ->canBeEnabled() |
|
376 | 2 | ->children() |
|
377 | ->scalarNode('cookie_jar') |
||
378 | 2 | ->info('This must be a service id to a service implementing Http\Message\CookieJar') |
|
379 | 1 | ->isRequired() |
|
380 | ->cannotBeEmpty() |
||
381 | ->end() |
||
382 | 1 | ->end() |
|
383 | 1 | ->end(); |
|
384 | 1 | // End cookie plugin |
|
385 | 1 | ||
386 | 1 | $decoder = $children->arrayNode('decoder'); |
|
387 | 1 | $disableAll ? $decoder->canBeEnabled() : $decoder->canBeDisabled(); |
|
388 | $decoder->addDefaultsIfNotSet() |
||
389 | ->children() |
||
390 | ->scalarNode('use_content_encoding')->defaultTrue()->end() |
||
391 | ->end() |
||
392 | ->end(); |
||
393 | // End decoder plugin |
||
394 | |||
395 | $children->arrayNode('history') |
||
396 | ->canBeEnabled() |
||
397 | ->children() |
||
398 | ->scalarNode('journal') |
||
399 | ->info('This must be a service id to a service implementing Http\Client\Plugin\Journal') |
||
400 | ->isRequired() |
||
401 | ->cannotBeEmpty() |
||
402 | ->end() |
||
403 | ->end() |
||
404 | ->end(); |
||
405 | // End history plugin |
||
406 | |||
407 | $logger = $children->arrayNode('logger'); |
||
408 | $disableAll ? $logger->canBeEnabled() : $logger->canBeDisabled(); |
||
409 | $logger->addDefaultsIfNotSet() |
||
410 | ->children() |
||
411 | ->scalarNode('logger') |
||
412 | ->info('This must be a service id to a service implementing Psr\Log\LoggerInterface') |
||
413 | ->defaultValue('logger') |
||
414 | ->cannotBeEmpty() |
||
415 | ->end() |
||
416 | ->scalarNode('formatter') |
||
417 | ->info('This must be a service id to a service implementing Http\Message\Formatter') |
||
418 | ->defaultNull() |
||
419 | ->end() |
||
420 | ->end() |
||
421 | ->end(); |
||
422 | // End logger plugin |
||
423 | |||
424 | $redirect = $children->arrayNode('redirect'); |
||
425 | $disableAll ? $redirect->canBeEnabled() : $redirect->canBeDisabled(); |
||
426 | $redirect->addDefaultsIfNotSet() |
||
427 | ->children() |
||
428 | ->scalarNode('preserve_header')->defaultTrue()->end() |
||
429 | ->scalarNode('use_default_for_multiple')->defaultTrue()->end() |
||
430 | ->end() |
||
431 | ->end(); |
||
432 | // End redirect plugin |
||
433 | |||
434 | $retry = $children->arrayNode('retry'); |
||
435 | $disableAll ? $retry->canBeEnabled() : $retry->canBeDisabled(); |
||
436 | $retry->addDefaultsIfNotSet() |
||
437 | ->children() |
||
438 | ->scalarNode('retry')->defaultValue(1)->end() // TODO: should be called retries for consistency with the class |
||
439 | ->end() |
||
440 | ->end(); |
||
441 | // End retry plugin |
||
442 | |||
443 | $stopwatch = $children->arrayNode('stopwatch'); |
||
444 | $disableAll ? $stopwatch->canBeEnabled() : $stopwatch->canBeDisabled(); |
||
445 | $stopwatch->addDefaultsIfNotSet() |
||
446 | ->children() |
||
447 | ->scalarNode('stopwatch') |
||
448 | ->info('This must be a service id to a service extending Symfony\Component\Stopwatch\Stopwatch') |
||
449 | ->defaultValue('debug.stopwatch') |
||
450 | ->cannotBeEmpty() |
||
451 | ->end() |
||
452 | ->end() |
||
453 | ->end(); |
||
454 | // End stopwatch plugin |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Create configuration for authentication plugin. |
||
459 | * |
||
460 | * @return NodeDefinition Definition for the authentication node in the plugins list. |
||
461 | */ |
||
462 | private function createAuthenticationPluginNode() |
||
463 | { |
||
464 | $builder = new TreeBuilder(); |
||
465 | $node = $builder->root('authentication'); |
||
466 | $node |
||
467 | ->useAttributeAsKey('name') |
||
468 | ->prototype('array') |
||
469 | ->validate() |
||
470 | ->always() |
||
471 | ->then(function ($config) { |
||
472 | switch ($config['type']) { |
||
473 | case 'basic': |
||
474 | $this->validateAuthenticationType(['username', 'password'], $config, 'basic'); |
||
475 | break; |
||
476 | case 'bearer': |
||
477 | $this->validateAuthenticationType(['token'], $config, 'bearer'); |
||
478 | break; |
||
479 | case 'service': |
||
480 | $this->validateAuthenticationType(['service'], $config, 'service'); |
||
481 | break; |
||
482 | case 'wsse': |
||
483 | $this->validateAuthenticationType(['username', 'password'], $config, 'wsse'); |
||
484 | break; |
||
485 | } |
||
486 | |||
487 | return $config; |
||
488 | }) |
||
489 | ->end() |
||
490 | ->children() |
||
491 | ->enumNode('type') |
||
492 | ->values(['basic', 'bearer', 'wsse', 'service']) |
||
493 | ->isRequired() |
||
494 | ->cannotBeEmpty() |
||
495 | ->end() |
||
496 | ->scalarNode('username')->end() |
||
497 | ->scalarNode('password')->end() |
||
498 | ->scalarNode('token')->end() |
||
499 | ->scalarNode('service')->end() |
||
500 | ->end() |
||
501 | ->end() |
||
502 | ->end(); // End authentication plugin |
||
503 | |||
504 | return $node; |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * Validate that the configuration fragment has the specified keys and none other. |
||
509 | * |
||
510 | * @param array $expected Fields that must exist |
||
511 | * @param array $actual Actual configuration hashmap |
||
512 | * @param string $authName Name of authentication method for error messages |
||
513 | * |
||
514 | * @throws InvalidConfigurationException If $actual does not have exactly the keys specified in $expected (plus 'type') |
||
515 | */ |
||
516 | private function validateAuthenticationType(array $expected, array $actual, $authName) |
||
534 | } |
||
535 |
If you suppress an error, we recommend checking for the error condition explicitly: