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 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 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": { diff --git a/src/Strategy/CommonClassesStrategy.php b/src/Strategy/CommonClassesStrategy.php index 383c7fc..ceff827 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; @@ -95,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], @@ -108,27 +113,43 @@ final class CommonClassesStrategy implements DiscoveryStrategy public static function getCandidates($type) { if (Psr18Client::class === $type) { - $candidates = self::$classes[PSR18Client::class]; + 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 = []; - // 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); + // 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; } + } else { + $candidates[] = $c; } - - return $candidates; } - if (isset(self::$classes[$type])) { - return self::$classes[$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); + } } - return []; + return $candidates; } public static function buzzInstantiate()