Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f2d2cf3

Browse files
nicowebnicolas-grekas
authored andcommitted
work with attributes for xml config
1 parent 0023a71 commit f2d2cf3

10 files changed

+141
-112
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Config\Definition\ConfigurationInterface;
2323
use Symfony\Component\DependencyInjection\Exception\LogicException;
2424
use Symfony\Component\Form\Form;
25+
use Symfony\Component\HttpClient\HttpClient;
2526
use Symfony\Component\HttpFoundation\Cookie;
2627
use Symfony\Component\Lock\Lock;
2728
use Symfony\Component\Lock\Store\SemaphoreStore;
@@ -1179,7 +1180,7 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode)
11791180
->children()
11801181
->arrayNode('http_client')
11811182
->info('HTTP Client configuration')
1182-
->canBeEnabled()
1183+
->{!class_exists(FullStack::class) && class_exists(HttpClient::class) ? 'canBeDisabled' : 'canBeEnabled'}()
11831184
->fixXmlConfig('client')
11841185
->children();
11851186

@@ -1213,12 +1214,27 @@ private function addHttpClientOptionsSection(NodeBuilder $rootNode)
12131214
->arrayNode('default_options')
12141215
->fixXmlConfig('header')
12151216
->children()
1216-
->scalarNode('auth')
1217+
->scalarNode('auth_basic')
12171218
->info('An HTTP Basic authentication "username:password".')
12181219
->end()
1220+
->scalarNode('auth_bearer')
1221+
->info('A token enabling HTTP Bearer authorization.')
1222+
->end()
12191223
->arrayNode('query')
12201224
->info('Associative array of query string values merged with URL parameters.')
12211225
->useAttributeAsKey('key')
1226+
->beforeNormalization()
1227+
->always(function ($config) {
1228+
if (!\is_array($config)) {
1229+
return [];
1230+
}
1231+
if (!isset($config['key'])) {
1232+
return $config;
1233+
}
1234+
1235+
return [$config['key'] => $config['value']];
1236+
})
1237+
->end()
12221238
->normalizeKeys(false)
12231239
->scalarPrototype()->end()
12241240
->end()
@@ -1237,12 +1253,21 @@ private function addHttpClientOptionsSection(NodeBuilder $rootNode)
12371253
->scalarNode('base_uri')
12381254
->info('The URI to resolve relative URLs, following rules in RFC 3986, section 2.')
12391255
->end()
1240-
->booleanNode('buffer')
1241-
->info('Indicates if the response should be buffered or not.')
1242-
->end()
12431256
->arrayNode('resolve')
12441257
->info('Associative array: domain => IP.')
12451258
->useAttributeAsKey('host')
1259+
->beforeNormalization()
1260+
->always(function ($config) {
1261+
if (!\is_array($config)) {
1262+
return [];
1263+
}
1264+
if (!isset($config['host'])) {
1265+
return $config;
1266+
}
1267+
1268+
return [$config['host'] => $config['value']];
1269+
})
1270+
->end()
12461271
->normalizeKeys(false)
12471272
->scalarPrototype()->end()
12481273
->end()
@@ -1284,9 +1309,12 @@ private function addHttpClientOptionsSection(NodeBuilder $rootNode)
12841309
->end()
12851310
->arrayNode('peer_fingerprint')
12861311
->info('Associative array: hashing algorithm => hash(es).')
1287-
->useAttributeAsKey('algo')
12881312
->normalizeKeys(false)
1289-
->variablePrototype()->end()
1313+
->children()
1314+
->variableNode('sha1')->end()
1315+
->variableNode('pin-sha256')->end()
1316+
->variableNode('md5')->end()
1317+
->end()
12901318
->end()
12911319
->end()
12921320
->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,9 +1771,16 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
17711771
public function merge(array $options, array $defaultOptions)
17721772
{
17731773
try {
1774-
[, $options] = $this->prepareRequest(null, null, $options, $defaultOptions);
1774+
[, $mergedOptions] = $this->prepareRequest(null, null, $options, $defaultOptions);
17751775

1776-
return $options;
1776+
foreach ($mergedOptions as $k => $v) {
1777+
if (!isset($options[$k]) && !isset($defaultOptions[$k])) {
1778+
// Remove options added by prepareRequest()
1779+
unset($mergedOptions[$k]);
1780+
}
1781+
}
1782+
1783+
return $mergedOptions;
17771784
} catch (TransportExceptionInterface $e) {
17781785
throw new InvalidArgumentException($e->getMessage(), 0, $e);
17791786
}
@@ -1783,19 +1790,27 @@ public function merge(array $options, array $defaultOptions)
17831790
$defaultOptions = $merger->merge($config['default_options'] ?? [], []);
17841791
$container->getDefinition('http_client')->setArguments([$defaultOptions, $config['max_host_connections'] ?? 6]);
17851792

1793+
if (!$hasPsr18 = interface_exists(ClientInterface::class)) {
1794+
$container->removeDefinition('psr18.http_client');
1795+
$container->removeAlias(ClientInterface::class);
1796+
}
1797+
17861798
foreach ($config['clients'] as $name => $clientConfig) {
17871799
$options = $merger->merge($clientConfig['default_options'] ?? [], $defaultOptions);
17881800

17891801
$container->register($name, HttpClientInterface::class)
17901802
->setFactory([HttpClient::class, 'create'])
17911803
->setArguments([$options, $clientConfig['max_host_connections'] ?? $config['max_host_connections'] ?? 6]);
17921804

1793-
$container->register('psr18.'.$name, Psr18Client::class)
1794-
->setAutowired(true)
1795-
->setArguments([new Reference($name)]);
1796-
17971805
$container->registerAliasForArgument($name, HttpClientInterface::class);
1798-
$container->registerAliasForArgument('psr18.'.$name, ClientInterface::class, $name);
1806+
1807+
if ($hasPsr18) {
1808+
$container->register('psr18.'.$name, Psr18Client::class)
1809+
->setAutowired(true)
1810+
->setArguments([new Reference($name)]);
1811+
1812+
$container->registerAliasForArgument('psr18.'.$name, ClientInterface::class, $name);
1813+
}
17991814
}
18001815
}
18011816

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<xsd:element name="php-errors" type="php-errors" minOccurs="0" maxOccurs="1" />
3333
<xsd:element name="lock" type="lock" minOccurs="0" maxOccurs="1" />
3434
<xsd:element name="messenger" type="messenger" minOccurs="0" maxOccurs="1" />
35-
<xsd:element name="http_client" type="http_client" minOccurs="0" maxOccurs="1" />
35+
<xsd:element name="http-client" type="http_client" minOccurs="0" maxOccurs="1" />
3636
</xsd:choice>
3737

3838
<xsd:attribute name="http-method-override" type="xsd:boolean" />
@@ -448,61 +448,63 @@
448448

449449
<xsd:complexType name="http_client">
450450
<xsd:sequence>
451-
<xsd:element name="max_host_connections" type="xsd:integer" minOccurs="0" />
452-
<xsd:element name="default_options" type="http_client_options" minOccurs="0" />
451+
<xsd:element name="default-options" type="http_client_options" minOccurs="0" />
453452
<xsd:element name="client" type="http_client_client" minOccurs="0" maxOccurs="unbounded" />
454453
</xsd:sequence>
455454
<xsd:attribute name="enabled" type="xsd:boolean" />
455+
<xsd:attribute name="max-host-connections" type="xsd:integer" />
456456
</xsd:complexType>
457457

458-
<xsd:complexType name="http_client_options">
459-
<xsd:sequence>
460-
<xsd:element name="auth" type="xsd:string" minOccurs="0" />
461-
<xsd:element name="query" type="http_query" minOccurs="0" />
462-
<xsd:element name="headers" type="http_headers" minOccurs="0" />
463-
<xsd:element name="max_redirects" type="xsd:integer" minOccurs="0" />
464-
<xsd:element name="http_version" type="xsd:string" minOccurs="0" />
465-
<xsd:element name="base_uri" type="xsd:string" minOccurs="0" />
466-
<xsd:element name="buffer" type="xsd:boolean" minOccurs="0" />
467-
<xsd:element name="resolve" type="metadata" minOccurs="0" maxOccurs="unbounded" />
468-
<xsd:element name="proxy" type="xsd:string" minOccurs="0" />
469-
<xsd:element name="no_proxy" type="xsd:string" minOccurs="0" />
470-
<xsd:element name="timeout" type="xsd:float" minOccurs="0" />
471-
<xsd:element name="bindto" type="xsd:string" minOccurs="0" />
472-
<xsd:element name="verify_peer" type="xsd:boolean" minOccurs="0" />
473-
<xsd:element name="verify_host" type="xsd:boolean" minOccurs="0" />
474-
<xsd:element name="cafile" type="xsd:string" minOccurs="0" />
475-
<xsd:element name="capath" type="xsd:string" minOccurs="0" />
476-
<xsd:element name="local_cert" type="xsd:string" minOccurs="0" />
477-
<xsd:element name="local_pk" type="xsd:string" minOccurs="0" />
478-
<xsd:element name="passphrase" type="xsd:string" minOccurs="0" />
479-
<xsd:element name="ciphers" type="xsd:string" minOccurs="0" />
480-
<xsd:element name="peer_fingerprint" type="fingerprint" minOccurs="0" maxOccurs="unbounded" />
481-
</xsd:sequence>
458+
<xsd:complexType name="http_client_options" mixed="true">
459+
<xsd:choice maxOccurs="unbounded">
460+
<xsd:element name="query" type="http_query" minOccurs="0" maxOccurs="unbounded" />
461+
<xsd:element name="resolve" type="http_resolve" minOccurs="0" maxOccurs="unbounded" />
462+
<xsd:element name="header" type="http_header" minOccurs="0" maxOccurs="unbounded" />
463+
<xsd:element name="peer-fingerprint" type="fingerprint" minOccurs="0" maxOccurs="unbounded" />
464+
</xsd:choice>
465+
<xsd:attribute name="proxy" type="xsd:string" />
466+
<xsd:attribute name="timeout" type="xsd:float" />
467+
<xsd:attribute name="bindto" type="xsd:string" />
468+
<xsd:attribute name="verify-peer" type="xsd:boolean" />
469+
<xsd:attribute name="auth-basic" type="xsd:string" />
470+
<xsd:attribute name="auth-bearer" type="xsd:string" />
471+
<xsd:attribute name="max-redirects" type="xsd:integer" />
472+
<xsd:attribute name="http-version" type="xsd:string" />
473+
<xsd:attribute name="base-uri" type="xsd:string" />
474+
<xsd:attribute name="no-proxy" type="xsd:string" />
475+
<xsd:attribute name="verify-host" type="xsd:boolean" />
476+
<xsd:attribute name="cafile" type="xsd:string" />
477+
<xsd:attribute name="capath" type="xsd:string" />
478+
<xsd:attribute name="local-cert" type="xsd:string" />
479+
<xsd:attribute name="local-pk" type="xsd:string" />
480+
<xsd:attribute name="passphrase" type="xsd:string" />
481+
<xsd:attribute name="ciphers" type="xsd:string" />
482482
</xsd:complexType>
483483

484484
<xsd:complexType name="http_client_client">
485485
<xsd:sequence>
486-
<xsd:element name="default_options" type="http_client_options" minOccurs="0" />
486+
<xsd:element name="default-options" type="http_client_options" minOccurs="0" />
487487
</xsd:sequence>
488488
<xsd:attribute name="name" type="xsd:string" />
489489
</xsd:complexType>
490490

491491
<xsd:complexType name="fingerprint">
492-
<xsd:sequence>
493-
<xsd:any minOccurs="0" processContents="lax" maxOccurs="unbounded" />
494-
</xsd:sequence>
492+
<xsd:choice maxOccurs="unbounded">
493+
<xsd:element name="pin-sha256" type="xsd:string" minOccurs="0" />
494+
<xsd:element name="sha1" type="xsd:string" minOccurs="0" />
495+
<xsd:element name="md5" type="xsd:string" minOccurs="0" />
496+
</xsd:choice>
495497
</xsd:complexType>
496498

497-
<xsd:complexType name="http_query">
498-
<xsd:sequence>
499-
<xsd:any minOccurs="0" processContents="lax" maxOccurs="unbounded" />
500-
</xsd:sequence>
499+
<xsd:complexType name="http_query" mixed="true">
500+
<xsd:attribute name="key" type="xsd:string" />
501501
</xsd:complexType>
502502

503-
<xsd:complexType name="http_headers">
504-
<xsd:sequence>
505-
<xsd:any minOccurs="0" processContents="lax" maxOccurs="unbounded" />
506-
</xsd:sequence>
503+
<xsd:complexType name="http_resolve" mixed="true">
504+
<xsd:attribute name="host" type="xsd:string" />
505+
</xsd:complexType>
506+
507+
<xsd:complexType name="http_header" mixed="true">
508+
<xsd:attribute name="name" type="xsd:string" />
507509
</xsd:complexType>
508510
</xsd:schema>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Bundle\FullStack;
1818
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1919
use Symfony\Component\Config\Definition\Processor;
20+
use Symfony\Component\HttpClient\HttpClient;
2021
use Symfony\Component\Lock\Store\SemaphoreStore;
2122
use Symfony\Component\Messenger\MessageBusInterface;
2223

@@ -332,8 +333,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
332333
],
333334
'disallow_search_engine_index' => true,
334335
'http_client' => [
335-
'enabled' => false,
336-
'max_host_connections' => 6,
336+
'enabled' => !class_exists(FullStack::class) && class_exists(HttpClient::class),
337337
'clients' => [],
338338
],
339339
];

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_full_default_options.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
$container->loadFromExtension('framework', [
44
'http_client' => [
55
'default_options' => [
6-
'auth' => 'foo:bar',
6+
'auth_basic' => 'foo:bar',
77
'query' => ['foo' => 'bar', 'bar' => 'baz'],
88
'headers' => ['X-powered' => 'PHP'],
99
'max_redirects' => 2,
1010
'http_version' => '2.0',
1111
'base_uri' => 'http://example.com',
12-
'buffer' => true,
1312
'resolve' => ['localhost' => '127.0.0.1'],
1413
'proxy' => 'proxy.org',
1514
'timeout' => 3.5,

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_default_options.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
77

88
<framework:config>
9-
<framework:http_client>
10-
<framework:max_host_connections>4</framework:max_host_connections>
11-
<framework:default_options />
9+
<framework:http-client max-host-connections="4">
10+
<framework:default-options />
1211
<framework:client name="foo">
13-
<framework:default_options />
12+
<framework:default-options />
1413
</framework:client>
15-
</framework:http_client>
14+
</framework:http-client>
1615
</framework:config>
1716
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_full_default_options.xml

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,34 @@
66
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
77

88
<framework:config>
9-
<framework:http_client>
10-
<framework:default_options>
11-
<framework:auth>foo:bar</framework:auth>
12-
<framework:query>
13-
<framework:foo>bar</framework:foo>
14-
<framework:bar>baz</framework:bar>
15-
</framework:query>
16-
<framework:headers>
17-
<framework:X-powered>PHP</framework:X-powered>
18-
</framework:headers>
19-
<framework:max_redirects>2</framework:max_redirects>
20-
<framework:http_version>2.0</framework:http_version>
21-
<framework:base_uri>http://example.com</framework:base_uri>
22-
<framework:buffer>true</framework:buffer>
23-
<framework:resolve>
24-
<framework:localhost>127.0.0.1</framework:localhost>
25-
</framework:resolve>
26-
<framework:proxy>proxy.org</framework:proxy>
27-
<framework:timeout>3.5</framework:timeout>
28-
<framework:bindto>127.0.0.1</framework:bindto>
29-
<framework:verify_peer>true</framework:verify_peer>
30-
<framework:verify_host>true</framework:verify_host>
31-
<framework:cafile>/etc/ssl/cafile</framework:cafile>
32-
<framework:capath>/etc/ssl</framework:capath>
33-
<framework:local_cert>/etc/ssl/cert.pem</framework:local_cert>
34-
<framework:local_pk>/etc/ssl/private_key.pem</framework:local_pk>
35-
<framework:passphrase>password123456</framework:passphrase>
36-
<framework:ciphers>RC4-SHA:TLS13-AES-128-GCM-SHA256</framework:ciphers>
37-
<framework:peer_fingerprint>
9+
<framework:http-client>
10+
<framework:default-options
11+
proxy="proxy.org"
12+
bindto="127.0.0.1"
13+
timeout="3.5"
14+
verify-peer="true"
15+
auth-basic="foo:bar"
16+
max-redirects="2"
17+
http-version="2.0"
18+
base-uri="http://example.com"
19+
verify-host="true"
20+
cafile="/etc/ssl/cafile"
21+
capath="/etc/ssl"
22+
local-cert="/etc/ssl/cert.pem"
23+
local-pk="/etc/ssl/private_key.pem"
24+
passphrase="password123456"
25+
ciphers="RC4-SHA:TLS13-AES-128-GCM-SHA256"
26+
>
27+
<framework:query key="foo">bar</framework:query>
28+
<framework:query key="bar">baz</framework:query>
29+
<framework:header name="X-powered">PHP</framework:header>
30+
<framework:resolve host="localhost">127.0.0.1</framework:resolve>
31+
<framework:peer-fingerprint>
3832
<framework:pin-sha256>14s5erg62v1v8471g2revg48r7==</framework:pin-sha256>
3933
<framework:pin-sha256>jsda84hjtyd4821bgfesd215bsfg5412=</framework:pin-sha256>
4034
<framework:md5>sdhtb481248721thbr=</framework:md5>
41-
</framework:peer_fingerprint>
42-
</framework:default_options>
43-
</framework:http_client>
35+
</framework:peer-fingerprint>
36+
</framework:default-options>
37+
</framework:http-client>
4438
</framework:config>
4539
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_override_default_options.xml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@
66
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
77

88
<framework:config>
9-
<framework:http_client>
10-
<framework:default_options>
11-
<framework:headers>
12-
<framework:foo>bar</framework:foo>
13-
</framework:headers>
14-
</framework:default_options>
9+
<framework:http-client>
10+
<framework:default-options>
11+
<framework:header name="foo">bar</framework:header>
12+
</framework:default-options>
1513
<framework:client name="foo">
16-
<framework:default_options>
17-
<framework:headers>
18-
<framework:bar>baz</framework:bar>
19-
</framework:headers>
20-
</framework:default_options>
14+
<framework:default-options>
15+
<framework:header name="bar">baz</framework:header>
16+
</framework:default-options>
2117
</framework:client>
22-
</framework:http_client>
18+
</framework:http-client>
2319
</framework:config>
2420
</container>

0 commit comments

Comments
 (0)