Skip to content
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

Make sure we support any psr-18 client #295

Merged
merged 5 commits into from Dec 30, 2018
Merged

Make sure we support any psr-18 client #295

merged 5 commits into from Dec 30, 2018

Conversation

Nyholm
Copy link
Member

@Nyholm Nyholm commented Dec 29, 2018

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets fixes #276
Documentation
License MIT

@Nyholm
Copy link
Member Author

Nyholm commented Dec 29, 2018

This is just minor improvements. It works even without this patch

Collector/ProfileClient.php Outdated Show resolved Hide resolved
@@ -6,6 +6,7 @@
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\HttplugBundle\ClientFactory\ClientFactory;
use Psr\Http\Client\ClientInterface;
Copy link
Contributor

Choose a reason for hiding this comment

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

I have checked out at your branch. I run composer update but still this namespace is not discovered by PHPStorm.. and the package is not installed.

Could it be that exists localy on you because you have not run composer update?

Copy link
Member Author

Choose a reason for hiding this comment

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

You need to make sure you install php-http/client-common:2.0-dev. If not you will get httplug: 1.1

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, you are correct.

@Nyholm
Copy link
Member Author

Nyholm commented Dec 30, 2018

Oh, you are correct. Since we have a bunch if httplug1.0 clients in require-dev we will never be able to install httplug2.0 by just running composer update.

I tested this by

composer create-project symfony/skeleton acme
cd acme
composer config minimum-stability dev
composer req php-http/httplug-bundle:dev-master nyholm/psr7

I created my client like:

namespace App\Service;

use Nyholm\Psr7\Response;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class MyHttpClient implements ClientInterface
{
    public function sendRequest(RequestInterface $request): ResponseInterface
    {
        return new Response(250);
    }
}

And I registered it like:

httplug:
    main_alias:
        client: httplug.client.my_client
    discovery:
        client: 'auto'

    clients:
        my_client:
            service: 'App\Service\MyHttpClient'

            http_methods_client: true
            plugins:
                - 'httplug.plugin.content_length'
                - 'httplug.plugin.redirect'

@gmponos gmponos mentioned this pull request Dec 30, 2018
1 task
@gmponos
Copy link
Contributor

gmponos commented Dec 30, 2018

Current PR brings up an issue... If you keep supporting both versions of HTTP-Client you can not depend on the PSR. Is this the direction you want this to go?

True. But we are not depending on the psr. Are we?

Well.. on the current a PR you want to do this if( $client instanceof ClientInterface) although this code works.. even if I don't have version 2 installed it kinda feels weird...

Secondly as a developer of this package... yes I do not depend on PSR client and I might not want to depend... As a consumer using this package.. is there a way that I feel confident that the client I am retrieving is a PSR one? Haven't discovered all the full aspects of the bundle so on this point I might be wrong... I think there is some autodiscovery mechanism.. that's what I am talking about.

Thirdly... here wont this miss the exceptions?

@gmponos
Copy link
Contributor

gmponos commented Dec 30, 2018

@Nyholm I think that your example works out of luck.. although I know that this was by design it just happens that PSR client and PHP-HTTP to have the same signature sendRequest..

for instance the ProfileClient I think it will work even a custom client of mine that does not implement any interface and it just has the sendRequest.. it feels kinda loose in this way...

@Nyholm
Copy link
Member Author

Nyholm commented Dec 30, 2018

Well.. on the current a PR you want to do this if( $client instanceof ClientInterface) although this code works.. even if I don't have version 2 installed it kinda feels weird...

Yes, I agree. But it works =)

Secondly as a developer of this package... yes I do not depend on PSR client and I might not want to depend... As a consumer using this package.. is there a way that I feel confident that the client I am retrieving is a PSR one? Haven't discovered all the full aspects of the bundle so on this point I might be wrong... I think there is some autodiscovery mechanism.. that's what I am talking about.

The auto discovery is here: https://github.com/php-http/discovery

And no, you cannot be sure unless you decide what you want. If you do composer req php-http/httplug:^2.0, then you will always get a PSR18 client. Same if you do composer req psr/http-client

Thirdly... here wont this miss the exceptions?

That should be updated. Thank you!

although I know that this was by design it just happens that PSR client and PHP-HTTP to have the same signature sendRequest..
for instance the ProfileClient I think it will work even a custom client of mine that does not implement any interface and it just has the sendRequest.. it feels kinda loose in this way...

Technically, yes. But we have code that make sure you submit a client. Check out the ProfileClient and the FlexibleClient

@gmponos
Copy link
Contributor

gmponos commented Dec 30, 2018

for instance the ProfileClient I think it will work even a custom client

But we have code that make sure you submit a client.

OK.. I missed the ! there...

I will have a deeper look because then I don't quite fully get it how your code example posted above worked with master :/

composer req php-http/httplug-bundle:dev-master nyholm/psr7

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.

not sure i understand the discussion on the PR. to me this looks correct. we allow either interface, and PHP will not explode if it does not know about a namespace, e.g. the PSR interfaces, in the case of running with httplug 1.x.

Collector/Formatter.php Outdated Show resolved Hide resolved
@dbu
Copy link
Collaborator

dbu commented Dec 30, 2018

Oh, you are correct. Since we have a bunch if httplug1.0 clients in require-dev we will never be able to install httplug2.0 by just running composer update.

is this issue fixed with #294? can you rebase and maybe add some tests that make sure both httplug 1 (when installed) and a psr-18 client (when installed) work with this code?

@Nyholm
Copy link
Member Author

Nyholm commented Dec 30, 2018

Correct.
Sure!

@gmponos
Copy link
Contributor

gmponos commented Dec 30, 2018

not sure i understand the discussion on the PR.

Yea.. I understand that I did not express my self correct on this.. also I was lost too because I thought that PHP will explode if it does not find a namespace which it doesn't... tests will be helpful to..

@dbu
Copy link
Collaborator

dbu commented Dec 30, 2018

Yea.. I understand that I did not express my self correct on this..

no worries, better speak up than silently be confused ;-) and i think it was very helpful here, because in the beginning we had no test covering the changes, which is dangerous.

and the fact that php ignores unknown namespaces in instanceof is ... "interesting".

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.

looks good to merge to me.

lets give @gmponos time to review as well.

Copy link
Contributor

@gmponos gmponos left a comment

Choose a reason for hiding this comment

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

According to the scope of the changes they are fine.. on an unrelated topic I believe that bundle must drop support for PHP 5 and HttpClient interface and bump to a new version.

@Nyholm
Copy link
Member Author

Nyholm commented Dec 30, 2018

Why do you believe that?

@Nyholm Nyholm merged commit 853b2c4 into master Dec 30, 2018
@Nyholm Nyholm mentioned this pull request Dec 30, 2018
@Nyholm
Copy link
Member Author

Nyholm commented Dec 30, 2018

Continue in #296

@Nyholm Nyholm deleted the patch-276 branch December 31, 2018 07:50
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.

Make sure we can use this bundle with ANY PSR-18 client
3 participants