Skip to content

Add default_client configuration option #479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee

# Version 2

# 2.2.0

- Added `default_client` configuration option to disable assigning the first client as default client and to remove the default client service.

# 2.1.0 - 2024-11-24

- Added [PluginConfigurator](https://docs.php-http.org/en/latest/integrations/symfony-bundle.html#configure-a-custom-plugin)
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultTrue()
->info('Set to false to not autowire ClientInterface and HttpAsyncClient.')
->end()
->booleanNode('default_client')
->defaultTrue()
->info('Set to false to disable assigning the first client as default client and to remove the default client service.')
->end()
->arrayNode('main_alias')
->addDefaultsIfNotSet()
->info('Configure which service the main alias point to.')
Expand Down
10 changes: 10 additions & 0 deletions src/DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ public function load(array $configs, ContainerBuilder $container): void
$container->removeAlias(ClientInterface::class);
}

if (!$config['default_client']) {
$container->removeAlias('httplug.client');
$container->removeAlias('httplug.client.default');
$container->removeDefinition('httplug.client.default');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think we should remove this definition. it has no semantic meaning for symfony, and if i define a client that i call default it would be very unexpected to just nuke my definition.

however, we should additionally remove the httplug.psr18_client.default alias (from the main_classes)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good catch indeed. It's naive for me to just remove this, because I assume it's the default one created by the bundle.

}

if ($this->useVcrPlugin) {
if (!\class_exists(RecordPlugin::class)) {
throw new InvalidConfigurationException('You need to require the VCR plugin to be able to use it: "composer require --dev php-http/vcr-plugin".');
Expand Down Expand Up @@ -139,6 +145,10 @@ private function configureClients(ContainerBuilder $container, array $config): v
$clients[] = $name;
}

if (!$config['default_client']) {
return;
}

// If we have clients configured
if (null !== $first) {
// If we do not have a client named 'default'
Expand Down
Loading