Skip to content

Support Guzzle 7+ #175

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 10 commits into from
Jul 2, 2020
Merged
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
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Added

- Support discovering PSR-18 factories of `guzzlehttp/guzzle` 7+

## 1.8.0 - 2020-06-14

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.7-dev"
"dev-master": "1.9-dev"
}
},
"conflict": {
Expand Down
49 changes: 35 additions & 14 deletions src/Strategy/CommonClassesStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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],
Expand All @@ -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')) {
Copy link
Member

Choose a reason for hiding this comment

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

I dont think we need this.

This logic should be on the condition

$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()
Expand Down