Skip to content

Commit 0c78413

Browse files
committed
bug #31307 [FrameworkBundle] Allow env variables in scoped_client base_uri (nicolas-grekas)
This PR was merged into the 4.3-dev branch. Discussion ---------- [FrameworkBundle] Allow env variables in scoped_client base_uri | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #31223 | License | MIT | Doc PR | - Commits ------- b2c885d [FrameworkBundle] Allow env variables in scoped_client base_uri
2 parents fd755b4 + b2c885d commit 0c78413

File tree

3 files changed

+20
-30
lines changed

3 files changed

+20
-30
lines changed

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

+4-18
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Symfony\Component\DependencyInjection\Exception\LogicException;
2323
use Symfony\Component\Form\Form;
2424
use Symfony\Component\HttpClient\HttpClient;
25-
use Symfony\Component\HttpClient\HttpClientTrait;
2625
use Symfony\Component\HttpFoundation\Cookie;
2726
use Symfony\Component\Lock\Lock;
2827
use Symfony\Component\Lock\Store\SemaphoreStore;
@@ -1371,29 +1370,16 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode)
13711370
->beforeNormalization()
13721371
->always()
13731372
->then(function ($config) {
1374-
if (!trait_exists(HttpClientTrait::class)) {
1373+
if (!class_exists(HttpClient::class)) {
13751374
throw new LogicException('HttpClient support cannot be enabled as the component is not installed. Try running "composer require symfony/http-client".');
13761375
}
13771376

1378-
$config = \is_array($config) ? $config : ['base_uri' => $config];
1379-
1380-
if (!isset($config['scope']) && isset($config['base_uri'])) {
1381-
$urlResolver = new class() {
1382-
use HttpClientTrait {
1383-
resolveUrl as public;
1384-
parseUrl as public;
1385-
}
1386-
};
1387-
1388-
$config['scope'] = preg_quote(implode('', $urlResolver->resolveUrl($urlResolver->parseUrl('.'), $urlResolver->parseUrl($config['base_uri']))));
1389-
}
1390-
1391-
return $config;
1377+
return \is_array($config) ? $config : ['base_uri' => $config];
13921378
})
13931379
->end()
13941380
->validate()
1395-
->ifTrue(function ($v) { return !isset($v['scope']); })
1396-
->thenInvalid('either "scope" or "base_uri" should be defined.')
1381+
->ifTrue(function ($v) { return !isset($v['scope']) && !isset($v['base_uri']); })
1382+
->thenInvalid('Either "scope" or "base_uri" should be defined.')
13971383
->end()
13981384
->validate()
13991385
->ifTrue(function ($v) { return isset($v['query']) && !isset($v['base_uri']); })

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -1854,11 +1854,17 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
18541854
throw new InvalidArgumentException(sprintf('Invalid scope name: "%s" is reserved.', $name));
18551855
}
18561856

1857-
$scope = $scopeConfig['scope'];
1857+
$scope = $scopeConfig['scope'] ?? null;
18581858
unset($scopeConfig['scope']);
18591859

1860-
$container->register($name, ScopingHttpClient::class)
1861-
->setArguments([new Reference('http_client'), [$scope => $scopeConfig], $scope]);
1860+
if (null === $scope) {
1861+
$container->register($name, ScopingHttpClient::class)
1862+
->setFactory([ScopingHttpClient::class, 'forBaseUri'])
1863+
->setArguments([new Reference('http_client'), $scopeConfig['base_uri'], $scopeConfig]);
1864+
} else {
1865+
$container->register($name, ScopingHttpClient::class)
1866+
->setArguments([new Reference('http_client'), [$scope => $scopeConfig], $scope]);
1867+
}
18621868

18631869
$container->registerAliasForArgument($name, HttpClientInterface::class);
18641870

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -1489,19 +1489,17 @@ public function testHttpClientOverrideDefaultOptions()
14891489

14901490
$this->assertSame(['foo' => 'bar'], $container->getDefinition('http_client')->getArgument(0)['headers']);
14911491
$this->assertSame(4, $container->getDefinition('http_client')->getArgument(1));
1492+
$this->assertSame('http://example.com', $container->getDefinition('foo')->getArgument(1));
14921493

14931494
$expected = [
1494-
'http\://example\.com/' => [
1495-
'base_uri' => 'http://example.com',
1496-
'headers' => [
1497-
'bar' => 'baz',
1498-
],
1499-
'query' => [],
1500-
'resolve' => [],
1495+
'base_uri' => 'http://example.com',
1496+
'headers' => [
1497+
'bar' => 'baz',
15011498
],
1499+
'query' => [],
1500+
'resolve' => [],
15021501
];
1503-
1504-
$this->assertSame($expected, $container->getDefinition('foo')->getArgument(1));
1502+
$this->assertSame($expected, $container->getDefinition('foo')->getArgument(2));
15051503
}
15061504

15071505
public function testHttpClientFullDefaultOptions()

0 commit comments

Comments
 (0)