From f2d2cf3021f9a86b8603d3c6696fd428feea606f Mon Sep 17 00:00:00 2001 From: nicoweb Date: Wed, 13 Mar 2019 18:23:40 +0100 Subject: [PATCH] work with attributes for xml config --- .../DependencyInjection/Configuration.php | 42 ++++++++-- .../FrameworkExtension.php | 29 +++++-- .../Resources/config/schema/symfony-1.0.xsd | 80 ++++++++++--------- .../DependencyInjection/ConfigurationTest.php | 4 +- .../php/http_client_full_default_options.php | 3 +- .../xml/http_client_default_options.xml | 9 +-- .../xml/http_client_full_default_options.xml | 58 ++++++-------- .../http_client_override_default_options.xml | 20 ++--- .../yml/http_client_full_default_options.yml | 3 +- .../FrameworkExtensionTest.php | 5 +- 10 files changed, 141 insertions(+), 112 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 1d882101d5..1e52ce9322 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -22,6 +22,7 @@ use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\DependencyInjection\Exception\LogicException; use Symfony\Component\Form\Form; +use Symfony\Component\HttpClient\HttpClient; use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\Lock\Lock; use Symfony\Component\Lock\Store\SemaphoreStore; @@ -1179,7 +1180,7 @@ class Configuration implements ConfigurationInterface ->children() ->arrayNode('http_client') ->info('HTTP Client configuration') - ->canBeEnabled() + ->{!class_exists(FullStack::class) && class_exists(HttpClient::class) ? 'canBeDisabled' : 'canBeEnabled'}() ->fixXmlConfig('client') ->children(); @@ -1213,12 +1214,27 @@ class Configuration implements ConfigurationInterface ->arrayNode('default_options') ->fixXmlConfig('header') ->children() - ->scalarNode('auth') + ->scalarNode('auth_basic') ->info('An HTTP Basic authentication "username:password".') ->end() + ->scalarNode('auth_bearer') + ->info('A token enabling HTTP Bearer authorization.') + ->end() ->arrayNode('query') ->info('Associative array of query string values merged with URL parameters.') ->useAttributeAsKey('key') + ->beforeNormalization() + ->always(function ($config) { + if (!\is_array($config)) { + return []; + } + if (!isset($config['key'])) { + return $config; + } + + return [$config['key'] => $config['value']]; + }) + ->end() ->normalizeKeys(false) ->scalarPrototype()->end() ->end() @@ -1237,12 +1253,21 @@ class Configuration implements ConfigurationInterface ->scalarNode('base_uri') ->info('The URI to resolve relative URLs, following rules in RFC 3986, section 2.') ->end() - ->booleanNode('buffer') - ->info('Indicates if the response should be buffered or not.') - ->end() ->arrayNode('resolve') ->info('Associative array: domain => IP.') ->useAttributeAsKey('host') + ->beforeNormalization() + ->always(function ($config) { + if (!\is_array($config)) { + return []; + } + if (!isset($config['host'])) { + return $config; + } + + return [$config['host'] => $config['value']]; + }) + ->end() ->normalizeKeys(false) ->scalarPrototype()->end() ->end() @@ -1284,9 +1309,12 @@ class Configuration implements ConfigurationInterface ->end() ->arrayNode('peer_fingerprint') ->info('Associative array: hashing algorithm => hash(es).') - ->useAttributeAsKey('algo') ->normalizeKeys(false) - ->variablePrototype()->end() + ->children() + ->variableNode('sha1')->end() + ->variableNode('pin-sha256')->end() + ->variableNode('md5')->end() + ->end() ->end() ->end() ->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 99970c3d54..673c89f9fe 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -1771,9 +1771,16 @@ class FrameworkExtension extends Extension public function merge(array $options, array $defaultOptions) { try { - [, $options] = $this->prepareRequest(null, null, $options, $defaultOptions); + [, $mergedOptions] = $this->prepareRequest(null, null, $options, $defaultOptions); - return $options; + foreach ($mergedOptions as $k => $v) { + if (!isset($options[$k]) && !isset($defaultOptions[$k])) { + // Remove options added by prepareRequest() + unset($mergedOptions[$k]); + } + } + + return $mergedOptions; } catch (TransportExceptionInterface $e) { throw new InvalidArgumentException($e->getMessage(), 0, $e); } @@ -1783,6 +1790,11 @@ class FrameworkExtension extends Extension $defaultOptions = $merger->merge($config['default_options'] ?? [], []); $container->getDefinition('http_client')->setArguments([$defaultOptions, $config['max_host_connections'] ?? 6]); + if (!$hasPsr18 = interface_exists(ClientInterface::class)) { + $container->removeDefinition('psr18.http_client'); + $container->removeAlias(ClientInterface::class); + } + foreach ($config['clients'] as $name => $clientConfig) { $options = $merger->merge($clientConfig['default_options'] ?? [], $defaultOptions); @@ -1790,12 +1802,15 @@ class FrameworkExtension extends Extension ->setFactory([HttpClient::class, 'create']) ->setArguments([$options, $clientConfig['max_host_connections'] ?? $config['max_host_connections'] ?? 6]); - $container->register('psr18.'.$name, Psr18Client::class) - ->setAutowired(true) - ->setArguments([new Reference($name)]); - $container->registerAliasForArgument($name, HttpClientInterface::class); - $container->registerAliasForArgument('psr18.'.$name, ClientInterface::class, $name); + + if ($hasPsr18) { + $container->register('psr18.'.$name, Psr18Client::class) + ->setAutowired(true) + ->setArguments([new Reference($name)]); + + $container->registerAliasForArgument('psr18.'.$name, ClientInterface::class, $name); + } } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd index add9b19771..c1242a1e08 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd @@ -32,7 +32,7 @@ - + @@ -448,61 +448,63 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + - + - - - + + + + + - - - - + + - - - - + + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index 3aee994484..56be70050c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -17,6 +17,7 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Configuration; use Symfony\Bundle\FullStack; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; use Symfony\Component\Config\Definition\Processor; +use Symfony\Component\HttpClient\HttpClient; use Symfony\Component\Lock\Store\SemaphoreStore; use Symfony\Component\Messenger\MessageBusInterface; @@ -332,8 +333,7 @@ class ConfigurationTest extends TestCase ], 'disallow_search_engine_index' => true, 'http_client' => [ - 'enabled' => false, - 'max_host_connections' => 6, + 'enabled' => !class_exists(FullStack::class) && class_exists(HttpClient::class), 'clients' => [], ], ]; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_full_default_options.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_full_default_options.php index 2fd7822c4e..59e7f85d03 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_full_default_options.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_full_default_options.php @@ -3,13 +3,12 @@ $container->loadFromExtension('framework', [ 'http_client' => [ 'default_options' => [ - 'auth' => 'foo:bar', + 'auth_basic' => 'foo:bar', 'query' => ['foo' => 'bar', 'bar' => 'baz'], 'headers' => ['X-powered' => 'PHP'], 'max_redirects' => 2, 'http_version' => '2.0', 'base_uri' => 'http://example.com', - 'buffer' => true, 'resolve' => ['localhost' => '127.0.0.1'], 'proxy' => 'proxy.org', 'timeout' => 3.5, diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_default_options.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_default_options.xml index 92d35bf9d0..5a16c54914 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_default_options.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_default_options.xml @@ -6,12 +6,11 @@ http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> - - 4 - + + - + - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_full_default_options.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_full_default_options.xml index e1353fb6c0..6f889ba6e8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_full_default_options.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_full_default_options.xml @@ -6,40 +6,34 @@ http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> - - - foo:bar - - bar - baz - - - PHP - - 2 - 2.0 - http://example.com - true - - 127.0.0.1 - - proxy.org - 3.5 - 127.0.0.1 - true - true - /etc/ssl/cafile - /etc/ssl - /etc/ssl/cert.pem - /etc/ssl/private_key.pem - password123456 - RC4-SHA:TLS13-AES-128-GCM-SHA256 - + + + bar + baz + PHP + 127.0.0.1 + 14s5erg62v1v8471g2revg48r7== jsda84hjtyd4821bgfesd215bsfg5412= sdhtb481248721thbr= - - - + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_override_default_options.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_override_default_options.xml index af8760a7fd..33c201ef9f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_override_default_options.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_override_default_options.xml @@ -6,19 +6,15 @@ http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> - - - - bar - - + + + bar + - - - baz - - + + baz + - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_full_default_options.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_full_default_options.yml index 946472b894..3d18286820 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_full_default_options.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_full_default_options.yml @@ -1,14 +1,13 @@ framework: http_client: default_options: - auth: foo:bar + auth_basic: foo:bar query: {'foo': 'bar', 'bar': 'baz'} headers: X-powered: PHP max_redirects: 2 http_version: 2.0 base_uri: 'http://example.com' - buffer: true resolve: {'localhost': '127.0.0.1'} proxy: proxy.org timeout: 3.5 diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index a851d89304..6a98f7c184 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -1363,8 +1363,6 @@ abstract class FrameworkExtensionTest extends TestCase 'query' => [], 'headers' => [], 'resolve' => [], - 'peer_fingerprint' => [], - 'http_version' => '', ]; $this->assertSame([$defaultOptions, 4], $container->getDefinition('http_client')->getArguments()); @@ -1387,13 +1385,12 @@ abstract class FrameworkExtensionTest extends TestCase $defaultOptions = $container->getDefinition('http_client')->getArguments()[0]; - $this->assertSame('foo:bar', $defaultOptions['auth']); + $this->assertSame('foo:bar', $defaultOptions['auth_basic']); $this->assertSame(['foo' => 'bar', 'bar' => 'baz'], $defaultOptions['query']); $this->assertSame(['x-powered' => ['PHP']], $defaultOptions['headers']); $this->assertSame(2, $defaultOptions['max_redirects']); $this->assertSame(2.0, (float) $defaultOptions['http_version']); $this->assertSame('http://example.com', $defaultOptions['base_uri']); - $this->assertTrue($defaultOptions['buffer']); $this->assertSame(['localhost' => '127.0.0.1'], $defaultOptions['resolve']); $this->assertSame('proxy.org', $defaultOptions['proxy']); $this->assertSame(3.5, $defaultOptions['timeout']);