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() |
|
43 | { |
||
44 | 14 | $treeBuilder = new TreeBuilder(); |
|
45 | 14 | $rootNode = $treeBuilder->root('httplug'); |
|
46 | |||
47 | 14 | $this->configureClients($rootNode); |
|
48 | 14 | $this->configureSharedPlugins($rootNode); |
|
49 | |||
50 | $rootNode |
||
51 | 14 | ->validate() |
|
52 | ->ifTrue(function ($v) { |
||
53 | 12 | return !empty($v['classes']['client']) |
|
54 | 12 | || !empty($v['classes']['message_factory']) |
|
55 | 9 | || !empty($v['classes']['uri_factory']) |
|
56 | 12 | || !empty($v['classes']['stream_factory']); |
|
57 | 14 | }) |
|
58 | ->then(function ($v) { |
||
59 | 3 | foreach ($v['classes'] as $key => $class) { |
|
60 | 3 | if (null !== $class && !class_exists($class)) { |
|
61 | 1 | throw new InvalidConfigurationException(sprintf( |
|
62 | 1 | 'Class %s specified for httplug.classes.%s does not exist.', |
|
63 | 1 | $class, |
|
64 | $key |
||
65 | 1 | )); |
|
66 | } |
||
67 | 2 | } |
|
68 | |||
69 | 2 | return $v; |
|
70 | 14 | }) |
|
71 | 14 | ->end() |
|
72 | 14 | ->beforeNormalization() |
|
73 | ->ifTrue(function ($v) { |
||
74 | 14 | return is_array($v) && array_key_exists('toolbar', $v) && is_array($v['toolbar']); |
|
75 | 14 | }) |
|
76 | ->then(function ($v) { |
||
77 | 4 | if (array_key_exists('profiling', $v)) { |
|
78 | 1 | throw new InvalidConfigurationException('Can\'t configure both "toolbar" and "profiling" section. The "toolbar" config is deprecated as of version 1.3.0, please only use "profiling".'); |
|
79 | } |
||
80 | |||
81 | 3 | @trigger_error('"httplug.toolbar" config is deprecated since version 1.3 and will be removed in 2.0. Use "httplug.profiling" instead.', E_USER_DEPRECATED); |
|
|
|||
82 | |||
83 | 3 | if (array_key_exists('enabled', $v['toolbar']) && 'auto' === $v['toolbar']['enabled']) { |
|
84 | 1 | @trigger_error('"auto" value in "httplug.toolbar" config is deprecated since version 1.3 and will be removed in 2.0. Use a boolean value instead.', E_USER_DEPRECATED); |
|
1 ignored issue
–
show
|
|||
85 | 1 | $v['toolbar']['enabled'] = $this->debug; |
|
86 | 1 | } |
|
87 | |||
88 | 3 | $v['profiling'] = $v['toolbar']; |
|
89 | |||
90 | 3 | unset($v['toolbar']); |
|
91 | |||
92 | 3 | return $v; |
|
93 | 14 | }) |
|
94 | 14 | ->end() |
|
95 | 14 | ->fixXmlConfig('client') |
|
96 | 14 | ->children() |
|
97 | 14 | ->arrayNode('main_alias') |
|
98 | 14 | ->addDefaultsIfNotSet() |
|
99 | 14 | ->info('Configure which service the main alias point to.') |
|
100 | 14 | ->children() |
|
101 | 14 | ->scalarNode('client')->defaultValue('httplug.client.default')->end() |
|
102 | 14 | ->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end() |
|
103 | 14 | ->scalarNode('uri_factory')->defaultValue('httplug.uri_factory.default')->end() |
|
104 | 14 | ->scalarNode('stream_factory')->defaultValue('httplug.stream_factory.default')->end() |
|
105 | 14 | ->end() |
|
106 | 14 | ->end() |
|
107 | 14 | ->arrayNode('classes') |
|
108 | 14 | ->addDefaultsIfNotSet() |
|
109 | 14 | ->info('Overwrite a service class instead of using the discovery mechanism.') |
|
110 | 14 | ->children() |
|
111 | 14 | ->scalarNode('client')->defaultNull()->end() |
|
112 | 14 | ->scalarNode('message_factory')->defaultNull()->end() |
|
113 | 14 | ->scalarNode('uri_factory')->defaultNull()->end() |
|
114 | 14 | ->scalarNode('stream_factory')->defaultNull()->end() |
|
115 | 14 | ->end() |
|
116 | 14 | ->end() |
|
117 | 14 | ->arrayNode('profiling') |
|
118 | 14 | ->addDefaultsIfNotSet() |
|
119 | 14 | ->treatFalseLike(['enabled' => false]) |
|
120 | 14 | ->treatTrueLike(['enabled' => true]) |
|
121 | 14 | ->treatNullLike(['enabled' => $this->debug]) |
|
122 | 14 | ->info('Extend the debug profiler with information about requests.') |
|
123 | 14 | ->children() |
|
124 | 14 | ->booleanNode('enabled') |
|
125 | 14 | ->info('Turn the toolbar on or off. Defaults to kernel debug mode.') |
|
126 | 14 | ->defaultValue($this->debug) |
|
127 | 14 | ->end() |
|
128 | 14 | ->scalarNode('formatter')->defaultNull()->end() |
|
129 | 14 | ->integerNode('captured_body_length') |
|
130 | 14 | ->defaultValue(0) |
|
131 | 14 | ->info('Limit long HTTP message bodies to x characters. If set to 0 we do not read the message body. Only available with the default formatter (FullHttpMessageFormatter).') |
|
132 | 14 | ->end() |
|
133 | 14 | ->end() |
|
134 | 14 | ->end() |
|
135 | 14 | ->arrayNode('discovery') |
|
136 | 14 | ->addDefaultsIfNotSet() |
|
137 | 14 | ->info('Control what clients should be found by the discovery.') |
|
138 | 14 | ->children() |
|
139 | 14 | ->scalarNode('client') |
|
140 | 14 | ->defaultValue('auto') |
|
141 | 14 | ->info('Set to "auto" to see auto discovered client in the web profiler. If provided a service id for a client then this client will be found by auto discovery.') |
|
142 | 14 | ->end() |
|
143 | 14 | ->scalarNode('async_client') |
|
144 | 14 | ->defaultNull() |
|
145 | 14 | ->info('Set to "auto" to see auto discovered client in the web profiler. If provided a service id for a client then this client will be found by auto discovery.') |
|
146 | 14 | ->end() |
|
147 | 14 | ->end() |
|
148 | 14 | ->end() |
|
149 | 14 | ->end(); |
|
150 | |||
151 | 14 | return $treeBuilder; |
|
152 | } |
||
153 | |||
154 | 14 | private function configureClients(ArrayNodeDefinition $root) |
|
155 | { |
||
156 | 14 | $pluginNode = $root->children() |
|
157 | 14 | ->arrayNode('clients') |
|
158 | 14 | ->fixXmlConfig('plugin') |
|
159 | ->validate() |
||
160 | 5 | ->ifTrue(function ($clients) { |
|
161 | foreach ($clients as $name => $config) { |
||
162 | 5 | // Make sure we only allow one of these to be true |
|
163 | return (bool) $config['flexible_client'] + (bool) $config['http_methods_client'] + (bool) $config['batch_client'] >= 2; |
||
164 | } |
||
165 | |||
166 | 14 | return false; |
|
167 | 14 | }) |
|
168 | 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() |
|
169 | 14 | ->useAttributeAsKey('name') |
|
170 | 14 | ->prototype('array') |
|
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 | ->addDefaultsIfNotSet() |
|
207 | ; |
||
208 | 14 | $this->addSharedPluginNodes($pluginsNode); |
|
209 | 14 | } |
|
210 | 14 | ||
211 | 14 | /** |
|
212 | 14 | * Configure plugins node of a client. |
|
213 | 14 | * |
|
214 | 14 | * @param ArrayNodeDefinition $pluginNode The node to add plugin definitions to. |
|
215 | 14 | */ |
|
216 | 14 | private function configureClientPlugins(ArrayNodeDefinition $pluginNode) |
|
217 | 14 | { |
|
218 | 14 | $pluginNode |
|
219 | 14 | // support having just a service id in the list |
|
220 | 14 | ->beforeNormalization() |
|
221 | 14 | ->always(function ($plugin) { |
|
222 | 14 | if (is_string($plugin)) { |
|
223 | 14 | return [ |
|
224 | 14 | 'reference' => [ |
|
225 | 14 | 'enabled' => true, |
|
226 | 14 | 'id' => $plugin, |
|
227 | 14 | ], |
|
228 | 14 | ]; |
|
229 | 14 | } |
|
230 | 14 | ||
231 | return $plugin; |
||
232 | 14 | }) |
|
233 | 14 | ->end() |
|
234 | 14 | ||
235 | 14 | ->validate() |
|
236 | 14 | ->always(function ($plugins) { |
|
237 | 14 | if (isset($plugins['authentication']) && !count($plugins['authentication'])) { |
|
238 | 14 | unset($plugins['authentication']); |
|
239 | 14 | } |
|
240 | 14 | foreach ($plugins as $name => $definition) { |
|
241 | 14 | if (!$definition['enabled']) { |
|
242 | unset($plugins[$name]); |
||
243 | 14 | } |
|
244 | 14 | } |
|
245 | 14 | ||
246 | 14 | return $plugins; |
|
247 | 14 | }) |
|
248 | 14 | ->end() |
|
249 | 14 | ; |
|
250 | $this->addSharedPluginNodes($pluginNode, true); |
||
251 | 14 | ||
252 | 14 | $pluginNode |
|
253 | 14 | ->children() |
|
254 | 14 | ->arrayNode('reference') |
|
255 | 14 | ->canBeEnabled() |
|
256 | 14 | ->info('Reference to a plugin service') |
|
257 | 14 | ->children() |
|
258 | 14 | ->scalarNode('id') |
|
259 | 14 | ->info('Service id of a plugin') |
|
260 | 14 | ->isRequired() |
|
261 | ->cannotBeEmpty() |
||
262 | 14 | ->end() |
|
263 | 14 | ->end() |
|
264 | 14 | ->end() |
|
265 | 14 | ->arrayNode('add_host') |
|
266 | 14 | ->canBeEnabled() |
|
267 | 14 | ->addDefaultsIfNotSet() |
|
268 | 14 | ->info('Configure the AddHostPlugin for this client.') |
|
269 | 14 | ->children() |
|
270 | 14 | ->scalarNode('host') |
|
271 | 14 | ->info('Host name including protocol and optionally the port number, e.g. https://api.local:8000') |
|
272 | 14 | ->isRequired() |
|
273 | 14 | ->cannotBeEmpty() |
|
274 | 14 | ->end() |
|
275 | 14 | ->scalarNode('replace') |
|
276 | 14 | ->info('Whether to replace the host if request already specifies it') |
|
277 | ->defaultValue(false) |
||
278 | 14 | ->end() |
|
279 | 14 | ->end() |
|
280 | 14 | ->end() |
|
281 | 14 | ||
282 | 14 | // TODO add aditional plugins that are only usable on a specific client |
|
283 | 14 | ->end() |
|
284 | 14 | ->end(); |
|
285 | 14 | } |
|
286 | |||
287 | 14 | /** |
|
288 | 14 | * Add the definitions for shared plugin configurations. |
|
289 | 14 | * |
|
290 | 14 | * @param ArrayNodeDefinition $pluginNode The node to add to. |
|
291 | 14 | * @param bool $disableAll Some shared plugins are enabled by default. On the client, all are disabled by default. |
|
292 | 14 | */ |
|
293 | 14 | private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableAll = false) |
|
294 | { |
||
295 | 14 | $children = $pluginNode->children(); |
|
296 | 14 | ||
297 | 14 | $children->append($this->createAuthenticationPluginNode()); |
|
298 | 14 | ||
299 | 14 | $children->arrayNode('cache') |
|
300 | 14 | ->canBeEnabled() |
|
301 | 14 | ->addDefaultsIfNotSet() |
|
302 | 14 | ->children() |
|
303 | 14 | ->scalarNode('cache_pool') |
|
304 | 14 | ->info('This must be a service id to a service implementing Psr\Cache\CacheItemPoolInterface') |
|
305 | 14 | ->isRequired() |
|
306 | ->cannotBeEmpty() |
||
307 | 14 | ->end() |
|
308 | 14 | ->scalarNode('stream_factory') |
|
309 | 14 | ->info('This must be a service id to a service implementing Http\Message\StreamFactory') |
|
310 | 14 | ->defaultValue('httplug.stream_factory') |
|
311 | ->cannotBeEmpty() |
||
312 | ->end() |
||
313 | ->arrayNode('config') |
||
314 | ->addDefaultsIfNotSet() |
||
315 | ->children() |
||
316 | ->scalarNode('default_ttl')->defaultNull()->end() |
||
317 | 14 | ->scalarNode('respect_cache_headers')->defaultTrue()->end() |
|
318 | ->end() |
||
319 | 14 | ->end() |
|
320 | 14 | ->end() |
|
321 | ->end(); |
||
322 | 14 | // End cache plugin |
|
323 | 14 | ||
324 | 14 | $children->arrayNode('cookie') |
|
325 | 14 | ->canBeEnabled() |
|
326 | 14 | ->children() |
|
327 | 2 | ->scalarNode('cookie_jar') |
|
328 | 2 | ->info('This must be a service id to a service implementing Http\Message\CookieJar') |
|
329 | 1 | ->isRequired() |
|
330 | 1 | ->cannotBeEmpty() |
|
331 | 2 | ->end() |
|
332 | 1 | ->end() |
|
333 | 1 | ->end(); |
|
334 | 2 | // End cookie plugin |
|
335 | 2 | ||
336 | 1 | $decoder = $children->arrayNode('decoder'); |
|
337 | 1 | $disableAll ? $decoder->canBeEnabled() : $decoder->canBeDisabled(); |
|
338 | 1 | $decoder->addDefaultsIfNotSet() |
|
339 | 1 | ->children() |
|
340 | 1 | ->scalarNode('use_content_encoding')->defaultTrue()->end() |
|
341 | ->end() |
||
342 | 1 | ->end(); |
|
343 | 14 | // End decoder plugin |
|
344 | 14 | ||
345 | 14 | $children->arrayNode('history') |
|
346 | 14 | ->canBeEnabled() |
|
347 | 14 | ->children() |
|
348 | 14 | ->scalarNode('journal') |
|
349 | 14 | ->info('This must be a service id to a service implementing Http\Client\Plugin\Journal') |
|
350 | 14 | ->isRequired() |
|
351 | 14 | ->cannotBeEmpty() |
|
352 | 14 | ->end() |
|
353 | 14 | ->end() |
|
354 | 14 | ->end(); |
|
355 | 14 | // End history plugin |
|
356 | 14 | ||
357 | 14 | $logger = $children->arrayNode('logger'); |
|
358 | $disableAll ? $logger->canBeEnabled() : $logger->canBeDisabled(); |
||
359 | 14 | $logger->addDefaultsIfNotSet() |
|
360 | ->children() |
||
361 | ->scalarNode('logger') |
||
362 | ->info('This must be a service id to a service implementing Psr\Log\LoggerInterface') |
||
363 | ->defaultValue('logger') |
||
364 | ->cannotBeEmpty() |
||
365 | ->end() |
||
366 | ->scalarNode('formatter') |
||
367 | ->info('This must be a service id to a service implementing Http\Message\Formatter') |
||
368 | ->defaultNull() |
||
369 | ->end() |
||
370 | ->end() |
||
371 | 2 | ->end(); |
|
372 | // End logger plugin |
||
373 | 2 | ||
374 | 2 | $redirect = $children->arrayNode('redirect'); |
|
375 | 2 | $disableAll ? $redirect->canBeEnabled() : $redirect->canBeDisabled(); |
|
376 | 2 | $redirect->addDefaultsIfNotSet() |
|
377 | ->children() |
||
378 | 2 | ->scalarNode('preserve_header')->defaultTrue()->end() |
|
379 | 1 | ->scalarNode('use_default_for_multiple')->defaultTrue()->end() |
|
380 | ->end() |
||
381 | ->end(); |
||
382 | 1 | // End redirect plugin |
|
383 | 1 | ||
384 | 1 | $retry = $children->arrayNode('retry'); |
|
385 | 1 | $disableAll ? $retry->canBeEnabled() : $retry->canBeDisabled(); |
|
386 | 1 | $retry->addDefaultsIfNotSet() |
|
387 | 1 | ->children() |
|
388 | ->scalarNode('retry')->defaultValue(1)->end() // TODO: should be called retries for consistency with the class |
||
389 | ->end() |
||
390 | ->end(); |
||
391 | // End retry plugin |
||
392 | |||
393 | $stopwatch = $children->arrayNode('stopwatch'); |
||
394 | $disableAll ? $stopwatch->canBeEnabled() : $stopwatch->canBeDisabled(); |
||
395 | $stopwatch->addDefaultsIfNotSet() |
||
396 | ->children() |
||
397 | ->scalarNode('stopwatch') |
||
398 | ->info('This must be a service id to a service extending Symfony\Component\Stopwatch\Stopwatch') |
||
399 | ->defaultValue('debug.stopwatch') |
||
400 | ->cannotBeEmpty() |
||
401 | ->end() |
||
402 | ->end() |
||
403 | ->end(); |
||
404 | // End stopwatch plugin |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * Create configuration for authentication plugin. |
||
409 | * |
||
410 | * @return NodeDefinition Definition for the authentication node in the plugins list. |
||
411 | */ |
||
412 | private function createAuthenticationPluginNode() |
||
456 | |||
457 | /** |
||
458 | * Validate that the configuration fragment has the specified keys and none other. |
||
459 | * |
||
460 | * @param array $expected Fields that must exist |
||
461 | * @param array $actual Actual configuration hashmap |
||
462 | * @param string $authName Name of authentication method for error messages |
||
463 | * |
||
464 | * @throws InvalidConfigurationException If $actual does not have exactly the keys specified in $expected (plus 'type') |
||
465 | */ |
||
466 | private function validateAuthenticationType(array $expected, array $actual, $authName) |
||
484 | } |
||
485 |
If you suppress an error, we recommend checking for the error condition explicitly: