1 | <?php |
||
28 | class Configuration implements ConfigurationInterface |
||
29 | { |
||
30 | /** |
||
31 | * Whether to use the debug mode. |
||
32 | * |
||
33 | * @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41 |
||
34 | * |
||
35 | * @var bool |
||
36 | */ |
||
37 | private $debug; |
||
38 | |||
39 | /** |
||
40 | * @param bool $debug |
||
41 | */ |
||
42 | 25 | public function __construct($debug) |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 25 | public function getConfigTreeBuilder() |
|
51 | { |
||
52 | 25 | $treeBuilder = new TreeBuilder(); |
|
53 | 25 | $rootNode = $treeBuilder->root('httplug'); |
|
54 | |||
55 | 25 | $this->configureClients($rootNode); |
|
56 | 25 | $this->configureSharedPlugins($rootNode); |
|
57 | |||
58 | $rootNode |
||
59 | 25 | ->validate() |
|
60 | 25 | ->ifTrue(function ($v) { |
|
61 | 19 | return !empty($v['classes']['client']) |
|
62 | 16 | || !empty($v['classes']['message_factory']) |
|
63 | 16 | || !empty($v['classes']['uri_factory']) |
|
64 | 19 | || !empty($v['classes']['stream_factory']); |
|
65 | 25 | }) |
|
66 | 25 | ->then(function ($v) { |
|
67 | 3 | foreach ($v['classes'] as $key => $class) { |
|
68 | 3 | if (null !== $class && !class_exists($class)) { |
|
69 | 1 | throw new InvalidConfigurationException(sprintf( |
|
70 | 1 | 'Class %s specified for httplug.classes.%s does not exist.', |
|
71 | 1 | $class, |
|
72 | 3 | $key |
|
73 | )); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | 2 | return $v; |
|
78 | 25 | }) |
|
79 | 25 | ->end() |
|
80 | 25 | ->beforeNormalization() |
|
81 | 25 | ->ifTrue(function ($v) { |
|
82 | 25 | return is_array($v) && array_key_exists('toolbar', $v) && is_array($v['toolbar']); |
|
83 | 25 | }) |
|
84 | 25 | ->then(function ($v) { |
|
85 | 3 | if (array_key_exists('profiling', $v)) { |
|
86 | 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".'); |
|
87 | } |
||
88 | |||
89 | 2 | @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); |
|
90 | |||
91 | 2 | if (array_key_exists('enabled', $v['toolbar']) && 'auto' === $v['toolbar']['enabled']) { |
|
92 | 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); |
|
93 | 1 | $v['toolbar']['enabled'] = $this->debug; |
|
94 | } |
||
95 | |||
96 | 2 | $v['profiling'] = $v['toolbar']; |
|
97 | |||
98 | 2 | unset($v['toolbar']); |
|
99 | |||
100 | 2 | return $v; |
|
101 | 25 | }) |
|
102 | 25 | ->end() |
|
103 | 25 | ->fixXmlConfig('client') |
|
104 | 25 | ->children() |
|
105 | 25 | ->arrayNode('main_alias') |
|
106 | 25 | ->addDefaultsIfNotSet() |
|
107 | 25 | ->info('Configure which service the main alias point to.') |
|
108 | 25 | ->children() |
|
109 | 25 | ->scalarNode('client')->defaultValue('httplug.client.default')->end() |
|
110 | 25 | ->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end() |
|
111 | 25 | ->scalarNode('uri_factory')->defaultValue('httplug.uri_factory.default')->end() |
|
112 | 25 | ->scalarNode('stream_factory')->defaultValue('httplug.stream_factory.default')->end() |
|
113 | 25 | ->end() |
|
114 | 25 | ->end() |
|
115 | 25 | ->arrayNode('classes') |
|
116 | 25 | ->addDefaultsIfNotSet() |
|
117 | 25 | ->info('Overwrite a service class instead of using the discovery mechanism.') |
|
118 | 25 | ->children() |
|
119 | 25 | ->scalarNode('client')->defaultNull()->end() |
|
120 | 25 | ->scalarNode('message_factory')->defaultNull()->end() |
|
121 | 25 | ->scalarNode('uri_factory')->defaultNull()->end() |
|
122 | 25 | ->scalarNode('stream_factory')->defaultNull()->end() |
|
123 | 25 | ->end() |
|
124 | 25 | ->end() |
|
125 | 25 | ->arrayNode('profiling') |
|
126 | 25 | ->addDefaultsIfNotSet() |
|
127 | 25 | ->treatFalseLike(['enabled' => false]) |
|
128 | 25 | ->treatTrueLike(['enabled' => true]) |
|
129 | 25 | ->treatNullLike(['enabled' => $this->debug]) |
|
130 | 25 | ->info('Extend the debug profiler with information about requests.') |
|
131 | 25 | ->children() |
|
132 | 25 | ->booleanNode('enabled') |
|
133 | 25 | ->info('Turn the toolbar on or off. Defaults to kernel debug mode.') |
|
134 | 25 | ->defaultValue($this->debug) |
|
135 | 25 | ->end() |
|
136 | 25 | ->scalarNode('formatter')->defaultNull()->end() |
|
137 | 25 | ->integerNode('captured_body_length') |
|
138 | 25 | ->defaultValue(0) |
|
139 | 25 | ->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).') |
|
140 | 25 | ->end() |
|
141 | 25 | ->end() |
|
142 | 25 | ->end() |
|
143 | 25 | ->arrayNode('discovery') |
|
144 | 25 | ->addDefaultsIfNotSet() |
|
145 | 25 | ->info('Control what clients should be found by the discovery.') |
|
146 | 25 | ->children() |
|
147 | 25 | ->scalarNode('client') |
|
148 | 25 | ->defaultValue('auto') |
|
149 | 25 | ->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.') |
|
150 | 25 | ->end() |
|
151 | 25 | ->scalarNode('async_client') |
|
152 | 25 | ->defaultNull() |
|
153 | 25 | ->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.') |
|
154 | 25 | ->end() |
|
155 | 25 | ->end() |
|
156 | 25 | ->end() |
|
157 | 25 | ->end(); |
|
158 | |||
159 | 25 | return $treeBuilder; |
|
160 | } |
||
161 | |||
162 | 25 | private function configureClients(ArrayNodeDefinition $root) |
|
163 | { |
||
164 | 25 | $root->children() |
|
165 | 25 | ->arrayNode('clients') |
|
166 | 25 | ->useAttributeAsKey('name') |
|
167 | 25 | ->prototype('array') |
|
168 | 25 | ->fixXmlConfig('plugin') |
|
169 | 25 | ->validate() |
|
170 | 25 | ->ifTrue(function ($config) { |
|
171 | // Make sure we only allow one of these to be true |
||
172 | 7 | return (bool) $config['flexible_client'] + (bool) $config['http_methods_client'] + (bool) $config['batch_client'] >= 2; |
|
173 | 25 | }) |
|
174 | 25 | ->thenInvalid('A http client can\'t be decorated with several of FlexibleHttpClient, HttpMethodsClient and BatchClient. Only one of the following options can be true. ("flexible_client", "http_methods_client", "batch_client")') |
|
175 | 25 | ->end() |
|
176 | 25 | ->validate() |
|
177 | 25 | ->ifTrue(function ($config) { |
|
178 | 7 | return 'httplug.factory.auto' === $config['factory'] && !empty($config['config']); |
|
179 | 25 | }) |
|
180 | 25 | ->thenInvalid('If you want to use the "config" key you must also specify a valid "factory".') |
|
181 | 25 | ->end() |
|
182 | 25 | ->children() |
|
183 | 25 | ->scalarNode('factory') |
|
184 | 25 | ->defaultValue('httplug.factory.auto') |
|
185 | 25 | ->cannotBeEmpty() |
|
186 | 25 | ->info('The service id of a factory to use when creating the adapter.') |
|
187 | 25 | ->end() |
|
188 | 25 | ->booleanNode('flexible_client') |
|
189 | 25 | ->defaultFalse() |
|
190 | 25 | ->info('Set to true to get the client wrapped in a FlexibleHttpClient which emulates async or sync behavior.') |
|
191 | 25 | ->end() |
|
192 | 25 | ->booleanNode('http_methods_client') |
|
193 | 25 | ->defaultFalse() |
|
194 | 25 | ->info('Set to true to get the client wrapped in a HttpMethodsClient which emulates provides functions for HTTP verbs.') |
|
195 | 25 | ->end() |
|
196 | 25 | ->booleanNode('batch_client') |
|
197 | 25 | ->defaultFalse() |
|
198 | 25 | ->info('Set to true to get the client wrapped in a BatchClient which allows you to send multiple request at the same time.') |
|
199 | 25 | ->end() |
|
200 | 25 | ->variableNode('config')->defaultValue([])->end() |
|
201 | 25 | ->append($this->createClientPluginNode()) |
|
202 | 25 | ->end() |
|
203 | 25 | ->end() |
|
204 | 25 | ->end(); |
|
205 | 25 | } |
|
206 | |||
207 | /** |
||
208 | * @param ArrayNodeDefinition $root |
||
209 | */ |
||
210 | 25 | private function configureSharedPlugins(ArrayNodeDefinition $root) |
|
221 | |||
222 | /** |
||
223 | * Createplugins node of a client. |
||
224 | * |
||
225 | * @return ArrayNodeDefinition The plugin node |
||
226 | */ |
||
227 | 25 | private function createClientPluginNode() |
|
372 | |||
373 | /** |
||
374 | * Add the definitions for shared plugin configurations. |
||
375 | * |
||
376 | * @param ArrayNodeDefinition $pluginNode The node to add to. |
||
377 | * @param bool $disableAll Some shared plugins are enabled by default. On the client, all are disabled by default. |
||
378 | */ |
||
379 | 25 | private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableAll = false) |
|
470 | |||
471 | /** |
||
472 | * Create configuration for authentication plugin. |
||
473 | * |
||
474 | * @return NodeDefinition Definition for the authentication node in the plugins list. |
||
475 | */ |
||
476 | 25 | private function createAuthenticationPluginNode() |
|
524 | |||
525 | /** |
||
526 | * Validate that the configuration fragment has the specified keys and none other. |
||
527 | * |
||
528 | * @param array $expected Fields that must exist |
||
529 | * @param array $actual Actual configuration hashmap |
||
530 | * @param string $authName Name of authentication method for error messages |
||
531 | * |
||
532 | * @throws InvalidConfigurationException If $actual does not have exactly the keys specified in $expected (plus 'type') |
||
533 | */ |
||
534 | 7 | private function validateAuthenticationType(array $expected, array $actual, $authName) |
|
552 | |||
553 | /** |
||
554 | * Create configuration for cache plugin. |
||
555 | * |
||
556 | * @return NodeDefinition Definition for the cache node in the plugins list. |
||
557 | */ |
||
558 | 25 | private function createCachePluginNode() |
|
652 | } |
||
653 |