Skip to content

Add configuration option to disable registration of default client #314

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

Merged
merged 1 commit into from
Mar 21, 2019

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented Feb 14, 2019

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets fixes #312
Documentation if this is a new feature, link to pull request in https://github.com/php-http/documentation that adds relevant documentation
License MIT

What's in this PR?

This allows you to disable the registration of the default HTTPlug client.

Why?

We are heavily using Symfony's auto wire functionality. Sometimes, developer inject HttpClient in the constructor without actually specifying the exact httplug.client.name. No error is thrown, and HTTPlug just picks the httplug.client.default service (or the first client it can find)

Checklist

  • Updated CHANGELOG.md to describe BC breaks / deprecations | new feature | bugfix
  • Documentation pull request created

@ruudk ruudk force-pushed the disable_default_clients branch from 196d9b0 to 5d05ccf Compare February 14, 2019 13:00
Copy link
Collaborator

@dbu dbu left a comment

Choose a reason for hiding this comment

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

thanks! i wonder if we should touch the default client or rather the alias with the client interface / class name (i think there are several, for psr-18 and http async and sync client). or we could remove both when the option is set.

@@ -87,6 +87,10 @@ public function load(array $configs, ContainerBuilder $container)
$this->configureClients($container, $config);
$this->configurePlugins($container, $config['plugins']); // must be after clients, as clients.X.plugins might use plugins as templates that will be removed
$this->configureAutoDiscoveryClients($container, $config);

if (!$config['register_default_client']) {
$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.

your goal is to prevent autowiring from happening, right? would it not be better to have an autowire_client flag and remove the alias with the client class name instead of the default service? with this, you would get an error that the alias is invalid, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the difference here is Symfony 3.4 and 4+.

With Symfony 3.4: when you have a default service, it will just auto wire when you use its class name.
I believe this changed in 4+ where you have to explicitly set the name of the service to the class name.

I faced this issue in a SF 3.4 application.

@dbu
Copy link
Collaborator

dbu commented Mar 2, 2019

@ruudk wdyt about the questions i asked?

@ruudk
Copy link
Contributor Author

ruudk commented Mar 2, 2019

Sorry for not responding. I will pick this up next week.

@dbu
Copy link
Collaborator

dbu commented Mar 5, 2019

please also rebase on master when you work on this. we moved the php files into src/ and tests/

i had no conflicts for other open merge requests, but github seems to not want to automatically solve this.

@ruudk ruudk force-pushed the disable_default_clients branch from 5d05ccf to 8266434 Compare March 5, 2019 11:56
@ruudk
Copy link
Contributor Author

ruudk commented Mar 5, 2019

Rebased the PR and didn't apply much changes. See comment above.

@dbu
Copy link
Collaborator

dbu commented Mar 5, 2019

looking at https://github.com/php-http/HttplugBundle/blob/master/src/Resources/config/services.xml#L44-L45 i am a bit confused. we set the default client to be loaded from discovery if there are no clients defined at all. and then we overwrite that with

if (null !== $first) {
// If we do not have a client named 'default'
if (!array_key_exists('default', $config['clients'])) {
$serviceId = 'httplug.client.'.$first;
// Alias the first client to httplug.client.default
$container->setAlias('httplug.client.default', $serviceId);
$default = $first;
} else {
$default = 'default';
}
if there are any clients configured. this seems odd.

so the configuration option would prevent both the factory service and aliasing the first client as the default client.

@Nyholm do you remember why we have the factory based client service as well? so that things work if you configure no client at all?

and the alias in https://github.com/php-http/HttplugBundle/blob/master/src/Resources/config/services.xml#L47 is actually on a client with default in the name. did not find where we define that service.

@ruudk ruudk force-pushed the disable_default_clients branch from 8266434 to 2c85746 Compare March 5, 2019 13:45
@Nyholm
Copy link
Member

Nyholm commented Mar 19, 2019

I like the feature but not the implementation (I think).

David: At runtime we configure profiling to the auto discovered client if we don't got any. It is a bit messy to say the least. Me and @fbourigault spend some long hours trying to figure stuff out.

@ruudk What about only removing the aliases and not the actual service?

@ruudk ruudk force-pushed the disable_default_clients branch from 2c85746 to 96121b4 Compare March 20, 2019 08:17
@ruudk
Copy link
Contributor Author

ruudk commented Mar 20, 2019

@Nyholm I updated the PR. It now only removes the aliases. Therefore, I think we might want to rename the configuration property to something like register_default_client_for_autowiring?

@dbu
Copy link
Collaborator

dbu commented Mar 20, 2019

agreed, and i think register_default_client_for_autowiring is a good name. it would be canBeDisabled() so that users can set enabled: false if they don't want autowiring.

can you please also rebase your pull request on master? i think we fixed the lowest build there.

@Nyholm
Copy link
Member

Nyholm commented Mar 20, 2019

Awesome. This is a much simpler implementation 👍

@joelwurtz
Copy link
Member

I prefer small value :), what about: default_client_autowiring ? (Or even default_autowiring)

@Nyholm
Copy link
Member

Nyholm commented Mar 20, 2019

client_interface_autowring?

@dbu
Copy link
Collaborator

dbu commented Mar 20, 2019

i would not call it default_autowiring. we could do autowire maybe? i guess there is nothing else that is autowired from this bundle?

@joelwurtz
Copy link
Member

If we want to use this value for disabling potential futur autowiring of other class, then i'm 👍 for autowiring only

@ruudk
Copy link
Contributor Author

ruudk commented Mar 20, 2019

i would not call it default_autowiring. we could do autowire maybe? i guess there is nothing else that is autowired from this bundle?

but what if we add something else in the future that autowires? Then this configuration property becomes too generic and we're screwed.

@ruudk
Copy link
Contributor Author

ruudk commented Mar 20, 2019

I really like default_client_autowiring because it's clear what it does.

@joelwurtz
Copy link
Member

And even if we want to split we can do somehting like that in the futur

autowiring:
    defaut_client: false
    other: true

autowiring is the best choice IMO

@ruudk
Copy link
Contributor Author

ruudk commented Mar 20, 2019

Oke, I will change it.

@ruudk
Copy link
Contributor Author

ruudk commented Mar 20, 2019

Shouldn't we also remove these aliases then?

        <service id="Http\Message\MessageFactory" alias="httplug.message_factory" public="false" />
        <service id="Http\Message\RequestFactory" alias="httplug.message_factory" public="false" />
        <service id="Http\Message\ResponseFactory" alias="httplug.message_factory" public="false" />
        <service id="Http\Message\StreamFactory" alias="httplug.stream_factory" public="false" />
        <service id="Http\Message\UriFactory" alias="httplug.uri_factory" public="false" />

@ruudk ruudk force-pushed the disable_default_clients branch from 96121b4 to 3cfe74b Compare March 20, 2019 09:02
@ruudk
Copy link
Contributor Author

ruudk commented Mar 20, 2019

Rebased PR and applied changes.

@dbu
Copy link
Collaborator

dbu commented Mar 20, 2019

oh, so we do autowire other things. i feel that it makes no sense to disable autowiring the factories. and therefore i would want to call it default_client_autowiring. if we have more autowiring that we want to allow to disable, we can still move to the nested structure. @joelwurtz can you live with that? i think clean name is more important than short name. and a nested structure seems too much, given that we probably don't want to un-autowire other things in the future.

@joelwurtz
Copy link
Member

I'm fine with that :) didn't know either there was autowiring for other classes sorry for the noise.

Thanks @ruudk for checking those things

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

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

Thank you

@ruudk ruudk force-pushed the disable_default_clients branch from 3cfe74b to b54efdb Compare March 20, 2019 14:12
@ruudk
Copy link
Contributor Author

ruudk commented Mar 20, 2019

Updated the PR one more time ;)

Copy link
Collaborator

@dbu dbu left a comment

Choose a reason for hiding this comment

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

cool! minor nitpick for the changelog, and the lowest version tests break. i suggest skipping your test on php 5 to keep this sane.

@ruudk ruudk force-pushed the disable_default_clients branch 2 times, most recently from b35fcfc to 083a825 Compare March 20, 2019 16:39
@dbu
Copy link
Collaborator

dbu commented Mar 20, 2019

one last thing i noticed: you add a git submodule documentation in this pull request. can you please remove that change? otherwise this looks ready to merge to me, thanks!

@ruudk ruudk force-pushed the disable_default_clients branch from 083a825 to 7fa5109 Compare March 20, 2019 17:29
@ruudk
Copy link
Contributor Author

ruudk commented Mar 20, 2019

Good catch. I accidentally staged that directory after all changing this PR 10 times ;) It's fixed now.

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

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

Thank you.

@dbu dbu merged commit bbe830d into php-http:master Mar 21, 2019
@dbu
Copy link
Collaborator

dbu commented Mar 21, 2019

thanks a lot and sorry for the back and forth!

@dbu
Copy link
Collaborator

dbu commented Mar 21, 2019

i documented this in php-http/documentation#260

@ruudk ruudk deleted the disable_default_clients branch March 21, 2019 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do not register default HttpClient to prevent auto wire issues
4 participants