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
-------
b2c885dfde
[FrameworkBundle] Allow env variables in scoped_client base_uri
This commit is contained in:
commit
0c78413b1e
@ -22,7 +22,6 @@ 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\HttpClient\HttpClientTrait;
|
||||
use Symfony\Component\HttpFoundation\Cookie;
|
||||
use Symfony\Component\Lock\Lock;
|
||||
use Symfony\Component\Lock\Store\SemaphoreStore;
|
||||
@ -1371,29 +1370,16 @@ class Configuration implements ConfigurationInterface
|
||||
->beforeNormalization()
|
||||
->always()
|
||||
->then(function ($config) {
|
||||
if (!trait_exists(HttpClientTrait::class)) {
|
||||
if (!class_exists(HttpClient::class)) {
|
||||
throw new LogicException('HttpClient support cannot be enabled as the component is not installed. Try running "composer require symfony/http-client".');
|
||||
}
|
||||
|
||||
$config = \is_array($config) ? $config : ['base_uri' => $config];
|
||||
|
||||
if (!isset($config['scope']) && isset($config['base_uri'])) {
|
||||
$urlResolver = new class() {
|
||||
use HttpClientTrait {
|
||||
resolveUrl as public;
|
||||
parseUrl as public;
|
||||
}
|
||||
};
|
||||
|
||||
$config['scope'] = preg_quote(implode('', $urlResolver->resolveUrl($urlResolver->parseUrl('.'), $urlResolver->parseUrl($config['base_uri']))));
|
||||
}
|
||||
|
||||
return $config;
|
||||
return \is_array($config) ? $config : ['base_uri' => $config];
|
||||
})
|
||||
->end()
|
||||
->validate()
|
||||
->ifTrue(function ($v) { return !isset($v['scope']); })
|
||||
->thenInvalid('either "scope" or "base_uri" should be defined.')
|
||||
->ifTrue(function ($v) { return !isset($v['scope']) && !isset($v['base_uri']); })
|
||||
->thenInvalid('Either "scope" or "base_uri" should be defined.')
|
||||
->end()
|
||||
->validate()
|
||||
->ifTrue(function ($v) { return isset($v['query']) && !isset($v['base_uri']); })
|
||||
|
@ -1854,11 +1854,17 @@ class FrameworkExtension extends Extension
|
||||
throw new InvalidArgumentException(sprintf('Invalid scope name: "%s" is reserved.', $name));
|
||||
}
|
||||
|
||||
$scope = $scopeConfig['scope'];
|
||||
$scope = $scopeConfig['scope'] ?? null;
|
||||
unset($scopeConfig['scope']);
|
||||
|
||||
$container->register($name, ScopingHttpClient::class)
|
||||
->setArguments([new Reference('http_client'), [$scope => $scopeConfig], $scope]);
|
||||
if (null === $scope) {
|
||||
$container->register($name, ScopingHttpClient::class)
|
||||
->setFactory([ScopingHttpClient::class, 'forBaseUri'])
|
||||
->setArguments([new Reference('http_client'), $scopeConfig['base_uri'], $scopeConfig]);
|
||||
} else {
|
||||
$container->register($name, ScopingHttpClient::class)
|
||||
->setArguments([new Reference('http_client'), [$scope => $scopeConfig], $scope]);
|
||||
}
|
||||
|
||||
$container->registerAliasForArgument($name, HttpClientInterface::class);
|
||||
|
||||
|
@ -1489,19 +1489,17 @@ abstract class FrameworkExtensionTest extends TestCase
|
||||
|
||||
$this->assertSame(['foo' => 'bar'], $container->getDefinition('http_client')->getArgument(0)['headers']);
|
||||
$this->assertSame(4, $container->getDefinition('http_client')->getArgument(1));
|
||||
$this->assertSame('http://example.com', $container->getDefinition('foo')->getArgument(1));
|
||||
|
||||
$expected = [
|
||||
'http\://example\.com/' => [
|
||||
'base_uri' => 'http://example.com',
|
||||
'headers' => [
|
||||
'bar' => 'baz',
|
||||
],
|
||||
'query' => [],
|
||||
'resolve' => [],
|
||||
'base_uri' => 'http://example.com',
|
||||
'headers' => [
|
||||
'bar' => 'baz',
|
||||
],
|
||||
'query' => [],
|
||||
'resolve' => [],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $container->getDefinition('foo')->getArgument(1));
|
||||
$this->assertSame($expected, $container->getDefinition('foo')->getArgument(2));
|
||||
}
|
||||
|
||||
public function testHttpClientFullDefaultOptions()
|
||||
|
Reference in New Issue
Block a user