From 52405ed75fb25ce601e961c5f4f09202594020bb Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 28 Jun 2020 15:29:29 +0100 Subject: [PATCH 01/10] Support Guzzle 7+ --- src/Strategy/CommonClassesStrategy.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Strategy/CommonClassesStrategy.php b/src/Strategy/CommonClassesStrategy.php index 383c7fc..dc07af3 100644 --- a/src/Strategy/CommonClassesStrategy.php +++ b/src/Strategy/CommonClassesStrategy.php @@ -26,6 +26,7 @@ use Http\Message\StreamFactory\SlimStreamFactory; use Http\Message\UriFactory\SlimUriFactory; use Slim\Http\Request as SlimRequest; +use GuzzleHttp\Client as GuzzleHttp; use Http\Adapter\Guzzle6\Client as Guzzle6; use Http\Adapter\Guzzle5\Client as Guzzle5; use Http\Client\Curl\Client as Curl; @@ -76,6 +77,7 @@ final class CommonClassesStrategy implements DiscoveryStrategy ], HttpClient::class => [ ['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, RequestFactory::class, [self::class, 'isPsr17FactoryInstalled']]], + ['class' => GuzzleHttp::class, 'condition' => GuzzleHttp::class], ['class' => Guzzle6::class, 'condition' => Guzzle6::class], ['class' => Guzzle5::class, 'condition' => Guzzle5::class], ['class' => Curl::class, 'condition' => Curl::class], From 9a088e591b0309d5a91b14cf515ce434888341fe Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 28 Jun 2020 15:38:50 +0100 Subject: [PATCH 02/10] Moved to the correct position in the file --- src/Strategy/CommonClassesStrategy.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Strategy/CommonClassesStrategy.php b/src/Strategy/CommonClassesStrategy.php index dc07af3..11630f2 100644 --- a/src/Strategy/CommonClassesStrategy.php +++ b/src/Strategy/CommonClassesStrategy.php @@ -77,7 +77,6 @@ final class CommonClassesStrategy implements DiscoveryStrategy ], HttpClient::class => [ ['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, RequestFactory::class, [self::class, 'isPsr17FactoryInstalled']]], - ['class' => GuzzleHttp::class, 'condition' => GuzzleHttp::class], ['class' => Guzzle6::class, 'condition' => Guzzle6::class], ['class' => Guzzle5::class, 'condition' => Guzzle5::class], ['class' => Curl::class, 'condition' => Curl::class], @@ -97,6 +96,10 @@ final class CommonClassesStrategy implements DiscoveryStrategy 'class' => [self::class, 'symfonyPsr18Instantiate'], 'condition' => [SymfonyPsr18::class, Psr17RequestFactory::class], ], + [ + 'class' => GuzzleHttp::class, + 'condition' => GuzzleHttp::class, + ], [ 'class' => [self::class, 'buzzInstantiate'], 'condition' => [\Buzz\Client\FileGetContents::class, \Buzz\Message\ResponseBuilder::class], From d9536c47aa55d4473a2b3c59f0ffa48388a38981 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 28 Jun 2020 15:47:42 +0100 Subject: [PATCH 03/10] Fixes to account for Guzzle 5 and 6 --- src/Strategy/CommonClassesStrategy.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Strategy/CommonClassesStrategy.php b/src/Strategy/CommonClassesStrategy.php index 11630f2..a2cfa30 100644 --- a/src/Strategy/CommonClassesStrategy.php +++ b/src/Strategy/CommonClassesStrategy.php @@ -112,9 +112,21 @@ final class CommonClassesStrategy implements DiscoveryStrategy */ public static function getCandidates($type) { - if (Psr18Client::class === $type) { - $candidates = self::$classes[PSR18Client::class]; + $candidates = []; + + // Not all versions of all clients implement the same interfaces. For example, + // Guzzle 6 does not implement the PSR-18 client interface, but Guzzle 7 does. + foreach (self::$classes[$type] ?? [] as $c) { + try { + if (is_subclass_of($c['class'], $type)) { + $candidates[] = $c; + } + } catch (\Throwable $e) { + trigger_error(sprintf('Got exception "%s (%s)" while checking if a PSR-18 Client is available', get_class($e), $e->getMessage()), E_USER_WARNING); + } + } + if (Psr18Client::class === $type) { // HTTPlug 2.0 clients implements PSR18Client too. foreach (self::$classes[HttpClient::class] as $c) { try { @@ -125,15 +137,9 @@ public static function getCandidates($type) trigger_error(sprintf('Got exception "%s (%s)" while checking if a PSR-18 Client is available', get_class($e), $e->getMessage()), E_USER_WARNING); } } - - return $candidates; - } - - if (isset(self::$classes[$type])) { - return self::$classes[$type]; } - return []; + return $candidates; } public static function buzzInstantiate() From d20317753ef925f0e59b99d8c8c4413fc93bbaf8 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 28 Jun 2020 15:48:09 +0100 Subject: [PATCH 04/10] Fixed comment --- src/Strategy/CommonClassesStrategy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Strategy/CommonClassesStrategy.php b/src/Strategy/CommonClassesStrategy.php index a2cfa30..5302c6f 100644 --- a/src/Strategy/CommonClassesStrategy.php +++ b/src/Strategy/CommonClassesStrategy.php @@ -114,7 +114,7 @@ public static function getCandidates($type) { $candidates = []; - // Not all versions of all clients implement the same interfaces. For example, + // Not all versions of all clients implement the same interfaces. For example, // Guzzle 6 does not implement the PSR-18 client interface, but Guzzle 7 does. foreach (self::$classes[$type] ?? [] as $c) { try { From 8b5eaa01c7c2c80da75f559837fb4129bffc9214 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 1 Jul 2020 12:17:36 +0100 Subject: [PATCH 05/10] Make work around purely for Guzzle --- src/Strategy/CommonClassesStrategy.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Strategy/CommonClassesStrategy.php b/src/Strategy/CommonClassesStrategy.php index 5302c6f..3f18649 100644 --- a/src/Strategy/CommonClassesStrategy.php +++ b/src/Strategy/CommonClassesStrategy.php @@ -114,15 +114,14 @@ public static function getCandidates($type) { $candidates = []; - // Not all versions of all clients implement the same interfaces. For example, - // Guzzle 6 does not implement the PSR-18 client interface, but Guzzle 7 does. foreach (self::$classes[$type] ?? [] as $c) { - try { - if (is_subclass_of($c['class'], $type)) { + // Guzzle 6 does not implement the PSR-18 client interface, but Guzzle 7 does. + if (Psr18Client::class === $type && $c['class'] === GuzzleHttp::class) { + if (defined('GuzzleHttp\ClientInterface::MAJOR_VERSION')) { $candidates[] = $c; } - } catch (\Throwable $e) { - trigger_error(sprintf('Got exception "%s (%s)" while checking if a PSR-18 Client is available', get_class($e), $e->getMessage()), E_USER_WARNING); + } else { + $candidates[] = $c; } } From 26217f757a4714b3fcf82b4893a657a1ed7ec11f Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 1 Jul 2020 12:18:31 +0100 Subject: [PATCH 06/10] Fixed cs --- src/Strategy/CommonClassesStrategy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Strategy/CommonClassesStrategy.php b/src/Strategy/CommonClassesStrategy.php index 3f18649..94ed752 100644 --- a/src/Strategy/CommonClassesStrategy.php +++ b/src/Strategy/CommonClassesStrategy.php @@ -116,7 +116,7 @@ public static function getCandidates($type) foreach (self::$classes[$type] ?? [] as $c) { // Guzzle 6 does not implement the PSR-18 client interface, but Guzzle 7 does. - if (Psr18Client::class === $type && $c['class'] === GuzzleHttp::class) { + if (Psr18Client::class === $type && GuzzleHttp::class === $c['class']) { if (defined('GuzzleHttp\ClientInterface::MAJOR_VERSION')) { $candidates[] = $c; } From 770838a3f4d0fd06a44d34c0b6d045ca6e1c8cce Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 1 Jul 2020 22:21:13 +0100 Subject: [PATCH 07/10] Handle PSR-18 totally separately --- src/Strategy/CommonClassesStrategy.php | 35 +++++++++++++++++--------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Strategy/CommonClassesStrategy.php b/src/Strategy/CommonClassesStrategy.php index 94ed752..ceff827 100644 --- a/src/Strategy/CommonClassesStrategy.php +++ b/src/Strategy/CommonClassesStrategy.php @@ -111,12 +111,25 @@ final class CommonClassesStrategy implements DiscoveryStrategy * {@inheritdoc} */ public static function getCandidates($type) + { + if (Psr18Client::class === $type) { + return self::getPsr18Candidates(); + } + + return self::$classes[$type] ?? []; + } + + /** + * @return array The return value is always an array with zero or more elements. Each + * element is an array with two keys ['class' => string, 'condition' => mixed]. + */ + private static function getPsr18Candidates() { $candidates = []; - foreach (self::$classes[$type] ?? [] as $c) { - // Guzzle 6 does not implement the PSR-18 client interface, but Guzzle 7 does. - if (Psr18Client::class === $type && GuzzleHttp::class === $c['class']) { + // Guzzle 6 does not implement the PSR-18 client interface, but Guzzle 7 does. + foreach (self::$classes[Psr18Client::class] ?? [] as $c) { + if (GuzzleHttp::class === $c['class']) { if (defined('GuzzleHttp\ClientInterface::MAJOR_VERSION')) { $candidates[] = $c; } @@ -125,16 +138,14 @@ public static function getCandidates($type) } } - if (Psr18Client::class === $type) { - // HTTPlug 2.0 clients implements PSR18Client too. - foreach (self::$classes[HttpClient::class] as $c) { - try { - if (is_subclass_of($c['class'], Psr18Client::class)) { - $candidates[] = $c; - } - } catch (\Throwable $e) { - trigger_error(sprintf('Got exception "%s (%s)" while checking if a PSR-18 Client is available', get_class($e), $e->getMessage()), E_USER_WARNING); + // HTTPlug 2.0 clients implements PSR18Client too. + foreach (self::$classes[HttpClient::class] as $c) { + try { + if (is_subclass_of($c['class'], Psr18Client::class)) { + $candidates[] = $c; } + } catch (\Throwable $e) { + trigger_error(sprintf('Got exception "%s (%s)" while checking if a PSR-18 Client is available', get_class($e), $e->getMessage()), E_USER_WARNING); } } From 7b928d95e5c192c778e66a29c04a4b551edc47a4 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 1 Jul 2020 22:36:07 +0100 Subject: [PATCH 08/10] Added more install test coverage --- .travis.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index fed7afe..d6647d3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -56,8 +56,10 @@ matrix: export -f install_test script: - # Test that we find Guzzle - - install_test will-find "Http\Discovery\HttpClientDiscovery::find();" "php-http/guzzle6-adapter" + # Test that we find Guzzle 6 v1 + - install_test will-find "Http\Discovery\HttpClientDiscovery::find();" "php-http/guzzle6-adapter:^1.1.1" + # Test that we find Guzzle 6 v2 + - install_test will-find "Http\Discovery\HttpClientDiscovery::find();" "php-http/guzzle6-adapter:^2.0.1" # Test that we find a client with Symfony and Guzzle PSR-7 - install_test will-find "Http\Discovery\HttpClientDiscovery::find();" "symfony/http-client:5.* php-http/httplug php-http/message-factory guzzlehttp/psr7:1.* http-interop/http-factory-guzzle" # We should fail if we dont have php-http/message-factory or PSR-17 @@ -69,6 +71,10 @@ matrix: - install_test will-find "Http\Discovery\HttpClientDiscovery::find();" "php-http/client-common:2.* php-http/message:1.8.* symfony/http-client:4.* php-http/guzzle6-adapter" # Test that we find an async client with Symfony and Guzzle - install_test will-find "Http\Discovery\HttpAsyncClientDiscovery::find();" "php-http/client-common:2.* php-http/message:1.8.* symfony/http-client:4.* php-http/guzzle6-adapter" + # Test that we find PSR-18 Guzzle 6 + - install_test will-find "Http\Discovery\Psr18ClientDiscovery::find();" "php-http/guzzle6-adapter:^2.0.1" + # Test that we find PSR-18 Guzzle 7 + - install_test will-find "Http\Discovery\Psr18ClientDiscovery::find();" "guzzlehttp/guzzle:^7.0.1" before_install: - if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi From b13db6f7bcf61438476d1b16d72732de6efa933a Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 1 Jul 2020 23:54:52 +0100 Subject: [PATCH 09/10] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7bc447..1eba516 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Added + +- Support discovering PSR-18 factories of `guzzlehttp/guzzle` 7+ + ## 1.8.0 - 2020-06-14 ### Added From 9f5fdb6975121b02172b43f30c7a30f0eadade3b Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 1 Jul 2020 23:55:18 +0100 Subject: [PATCH 10/10] Bumped branch alias --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7a8e424..bca83c8 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.7-dev" + "dev-master": "1.9-dev" } }, "conflict": {