| 1 | <?php |
||
| 21 | class Configuration implements ConfigurationInterface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Whether to use the debug mode. |
||
| 25 | * |
||
| 26 | * @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41 |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | private $debug; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param bool $debug |
||
| 34 | 12 | */ |
|
| 35 | public function __construct($debug) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | 12 | */ |
|
| 43 | public function getConfigTreeBuilder() |
||
| 44 | 12 | { |
|
| 45 | 12 | $treeBuilder = new TreeBuilder(); |
|
| 46 | $rootNode = $treeBuilder->root('httplug'); |
||
| 47 | 12 | ||
| 48 | 12 | $this->configureClients($rootNode); |
|
| 49 | $this->configurePlugins($rootNode); |
||
| 50 | |||
| 51 | 12 | $rootNode |
|
| 52 | ->validate() |
||
| 53 | 11 | ->ifTrue(function ($v) { |
|
| 54 | 11 | return !empty($v['classes']['client']) |
|
| 55 | 8 | || !empty($v['classes']['message_factory']) |
|
| 56 | 11 | || !empty($v['classes']['uri_factory']) |
|
| 57 | 12 | || !empty($v['classes']['stream_factory']); |
|
| 58 | }) |
||
| 59 | 3 | ->then(function ($v) { |
|
| 60 | 3 | foreach ($v['classes'] as $key => $class) { |
|
| 61 | 1 | if (null !== $class && !class_exists($class)) { |
|
| 62 | 1 | throw new InvalidConfigurationException(sprintf( |
|
| 63 | 1 | 'Class %s specified for httplug.classes.%s does not exist.', |
|
| 64 | $class, |
||
| 65 | 1 | $key |
|
| 66 | )); |
||
| 67 | 2 | } |
|
| 68 | } |
||
| 69 | 2 | ||
| 70 | 12 | return $v; |
|
| 71 | 12 | }) |
|
| 72 | 12 | ->end() |
|
| 73 | 12 | ->beforeNormalization() |
|
| 74 | 12 | ->ifTrue(function ($v) { |
|
| 75 | 12 | return is_array($v) && array_key_exists('toolbar', $v) && is_array($v['toolbar']); |
|
| 76 | 12 | }) |
|
| 77 | 12 | ->then(function ($v) { |
|
| 78 | 12 | if (array_key_exists('profiling', $v)) { |
|
| 79 | 12 | 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".'); |
|
| 80 | 12 | } |
|
| 81 | 12 | ||
| 82 | 12 | @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); |
|
|
|
|||
| 83 | 12 | ||
| 84 | 12 | if (array_key_exists('enabled', $v['toolbar']) && 'auto' === $v['toolbar']['enabled']) { |
|
| 85 | 12 | @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
|
|||
| 86 | 12 | $v['toolbar']['enabled'] = $this->debug; |
|
| 87 | 12 | } |
|
| 88 | 12 | ||
| 89 | 12 | $v['profiling'] = $v['toolbar']; |
|
| 90 | 12 | ||
| 91 | 12 | unset($v['toolbar']); |
|
| 92 | 12 | ||
| 93 | 12 | return $v; |
|
| 94 | 12 | }) |
|
| 95 | 12 | ->end() |
|
| 96 | 12 | ->fixXmlConfig('client') |
|
| 97 | 12 | ->children() |
|
| 98 | 12 | ->arrayNode('main_alias') |
|
| 99 | 12 | ->addDefaultsIfNotSet() |
|
| 100 | ->info('Configure which service the main alias point to.') |
||
| 101 | 1 | ->children() |
|
| 102 | 12 | ->scalarNode('client')->defaultValue('httplug.client.default')->end() |
|
| 103 | 12 | ->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end() |
|
| 104 | 12 | ->scalarNode('uri_factory')->defaultValue('httplug.uri_factory.default')->end() |
|
| 105 | 12 | ->scalarNode('stream_factory')->defaultValue('httplug.stream_factory.default')->end() |
|
| 106 | 12 | ->end() |
|
| 107 | 12 | ->end() |
|
| 108 | 12 | ->arrayNode('classes') |
|
| 109 | 12 | ->addDefaultsIfNotSet() |
|
| 110 | 12 | ->info('Overwrite a service class instead of using the discovery mechanism.') |
|
| 111 | 12 | ->children() |
|
| 112 | 12 | ->scalarNode('client')->defaultNull()->end() |
|
| 113 | 12 | ->scalarNode('message_factory')->defaultNull()->end() |
|
| 114 | 12 | ->scalarNode('uri_factory')->defaultNull()->end() |
|
| 115 | 12 | ->scalarNode('stream_factory')->defaultNull()->end() |
|
| 116 | 12 | ->end() |
|
| 117 | 12 | ->end() |
|
| 118 | 12 | ->arrayNode('profiling') |
|
| 119 | 12 | ->addDefaultsIfNotSet() |
|
| 120 | 12 | ->treatFalseLike(['enabled' => false]) |
|
| 121 | 12 | ->treatTrueLike(['enabled' => true]) |
|
| 122 | 12 | ->treatNullLike(['enabled' => $this->debug]) |
|
| 123 | 12 | ->info('Extend the debug profiler with information about requests.') |
|
| 124 | 12 | ->children() |
|
| 125 | 12 | ->booleanNode('enabled') |
|
| 126 | 12 | ->info('Turn the toolbar on or off. Defaults to kernel debug mode.') |
|
| 127 | 12 | ->defaultValue($this->debug) |
|
| 128 | 12 | ->end() |
|
| 129 | ->scalarNode('formatter')->defaultNull()->end() |
||
| 130 | 12 | ->integerNode('captured_body_length') |
|
| 131 | ->defaultValue(0) |
||
| 132 | ->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).') |
||
| 133 | 12 | ->end() |
|
| 134 | ->end() |
||
| 135 | 12 | ->end() |
|
| 136 | 12 | ->arrayNode('discovery') |
|
| 137 | 12 | ->addDefaultsIfNotSet() |
|
| 138 | ->info('Control what clients should be found by the discovery.') |
||
| 139 | 3 | ->children() |
|
| 140 | ->scalarNode('client') |
||
| 141 | 3 | ->defaultValue('auto') |
|
| 142 | ->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.') |
||
| 143 | ->end() |
||
| 144 | ->scalarNode('async_client') |
||
| 145 | 12 | ->defaultNull() |
|
| 146 | 12 | ->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.') |
|
| 147 | 12 | ->end() |
|
| 148 | 12 | ->end() |
|
| 149 | 12 | ->end() |
|
| 150 | 12 | ->end(); |
|
| 151 | 12 | ||
| 152 | 12 | return $treeBuilder; |
|
| 153 | 12 | } |
|
| 154 | 12 | ||
| 155 | 12 | private function configureClients(ArrayNodeDefinition $root) |
|
| 156 | 12 | { |
|
| 157 | 12 | $root->children() |
|
| 158 | 12 | ->arrayNode('clients') |
|
| 159 | 12 | ->validate() |
|
| 160 | 12 | ->ifTrue(function ($clients) { |
|
| 161 | 12 | foreach ($clients as $name => $config) { |
|
| 162 | 12 | // Make sure we only allow one of these to be true |
|
| 163 | 12 | return (bool) $config['flexible_client'] + (bool) $config['http_methods_client'] + (bool) $config['batch_client'] >= 2; |
|
| 164 | 12 | } |
|
| 165 | 12 | ||
| 166 | 12 | return false; |
|
| 167 | 12 | }) |
|
| 168 | 12 | ->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 | 12 | ->useAttributeAsKey('name') |
|
| 170 | 12 | ->prototype('array') |
|
| 171 | 12 | ->children() |
|
| 172 | 12 | ->scalarNode('factory') |
|
| 173 | 12 | ->isRequired() |
|
| 174 | 12 | ->cannotBeEmpty() |
|
| 175 | ->info('The service id of a factory to use when creating the adapter.') |
||
| 176 | ->end() |
||
| 177 | ->booleanNode('flexible_client') |
||
| 178 | ->defaultFalse() |
||
| 179 | 12 | ->info('Set to true to get the client wrapped in a FlexibleHttpClient which emulates async or sync behavior.') |
|
| 180 | ->end() |
||
| 181 | 12 | ->booleanNode('http_methods_client') |
|
| 182 | 12 | ->defaultFalse() |
|
| 183 | 12 | ->info('Set to true to get the client wrapped in a HttpMethodsClient which emulates provides functions for HTTP verbs.') |
|
| 184 | 12 | ->end() |
|
| 185 | 12 | ->booleanNode('batch_client') |
|
| 186 | ->defaultFalse() |
||
| 187 | 12 | ->info('Set to true to get the client wrapped in a BatchClient which allows you to send multiple request at the same time.') |
|
| 188 | 12 | ->end() |
|
| 189 | 12 | ->arrayNode('plugins') |
|
| 190 | 12 | ->info('A list of service ids of plugins. The order is important.') |
|
| 191 | 12 | ->prototype('scalar')->end() |
|
| 192 | 12 | ->end() |
|
| 193 | 12 | ->variableNode('config')->defaultValue([])->end() |
|
| 194 | 12 | ->append($this->createExtraPluginsNode()) |
|
| 195 | 12 | ->end() |
|
| 196 | 12 | ->end(); |
|
| 197 | 12 | } |
|
| 198 | 12 | ||
| 199 | 12 | /** |
|
| 200 | 12 | * @param ArrayNodeDefinition $root |
|
| 201 | 12 | */ |
|
| 202 | 12 | private function configurePlugins(ArrayNodeDefinition $root) |
|
| 203 | 12 | { |
|
| 204 | 12 | $pluginsNode = $root |
|
| 205 | 12 | ->children() |
|
| 206 | 12 | ->arrayNode('plugins') |
|
| 207 | 12 | ->addDefaultsIfNotSet() |
|
| 208 | 12 | ; |
|
| 209 | 12 | $this->configureSharedPluginNodes($pluginsNode); |
|
| 210 | } |
||
| 211 | 12 | ||
| 212 | 12 | /** |
|
| 213 | 12 | * Create configuration for the extra_plugins node inside the client. |
|
| 214 | 12 | * |
|
| 215 | 12 | * @return NodeDefinition Definition of the extra_plugins node in the client. |
|
| 216 | 12 | */ |
|
| 217 | 12 | private function createExtraPluginsNode() |
|
| 218 | 12 | { |
|
| 219 | 12 | $builder = new TreeBuilder(); |
|
| 220 | 12 | $node = $builder->root('extra_plugins'); |
|
| 221 | $node->validate() |
||
| 222 | 12 | ->always(function ($plugins) { |
|
| 223 | 12 | if (!count($plugins['authentication'])) { |
|
| 224 | 12 | unset($plugins['authentication']); |
|
| 225 | 12 | } |
|
| 226 | 12 | foreach ($plugins as $name => $definition) { |
|
| 227 | 12 | if (!$definition['enabled']) { |
|
| 228 | 12 | unset($plugins[$name]); |
|
| 229 | } |
||
| 230 | 12 | } |
|
| 231 | 12 | ||
| 232 | 12 | return $plugins; |
|
| 233 | 12 | }) |
|
| 234 | 12 | ; |
|
| 235 | 12 | $this->configureSharedPluginNodes($node, true); |
|
| 236 | 12 | $node |
|
| 237 | 12 | ->children() |
|
| 238 | 12 | ->arrayNode('add_host') |
|
| 239 | 12 | ->canBeEnabled() |
|
| 240 | ->addDefaultsIfNotSet() |
||
| 241 | 12 | ->info('Configure the AddHostPlugin for this client.') |
|
| 242 | 12 | ->children() |
|
| 243 | 12 | ->scalarNode('host') |
|
| 244 | 12 | ->info('Host name including protocol and optionally the port number, e.g. https://api.local:8000') |
|
| 245 | 12 | ->isRequired() |
|
| 246 | 12 | ->cannotBeEmpty() |
|
| 247 | 12 | ->end() |
|
| 248 | 12 | ->scalarNode('replace') |
|
| 249 | 12 | ->info('Whether to replace the host if request already specifies it') |
|
| 250 | 12 | ->defaultValue(false) |
|
| 251 | 12 | ->end() |
|
| 252 | 12 | ->end() |
|
| 253 | 12 | ->end() |
|
| 254 | 12 | ->end() |
|
| 255 | 12 | ->end(); |
|
| 256 | |||
| 257 | 12 | return $node; |
|
| 258 | 12 | } |
|
| 259 | 12 | ||
| 260 | 12 | /** |
|
| 261 | 12 | * @param ArrayNodeDefinition $pluginNode |
|
| 262 | 12 | * @param bool $disableAll Some shared plugins are enabled by default. On the client, all are disabled by default. |
|
| 263 | 12 | */ |
|
| 264 | 12 | private function configureSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableAll = false) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Create configuration for authentication plugin. |
||
| 400 | * |
||
| 401 | * @return NodeDefinition Definition for the authentication node in the plugins list. |
||
| 402 | */ |
||
| 403 | private function createAuthenticationPluginNode() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Validate that the configuration fragment has the specified keys and none other. |
||
| 450 | * |
||
| 451 | * @param array $expected Fields that must exist |
||
| 452 | * @param array $actual Actual configuration hashmap |
||
| 453 | * @param string $authName Name of authentication method for error messages |
||
| 454 | * |
||
| 455 | * @throws InvalidConfigurationException If $actual does not have exactly the keys specified in $expected (plus 'type') |
||
| 456 | */ |
||
| 457 | private function validateAuthenticationType(array $expected, array $actual, $authName) |
||
| 475 | } |
||
| 476 |
If you suppress an error, we recommend checking for the error condition explicitly: