Skip to content

Commit fef8475

Browse files
Support Guzzle 7+ (#175)
1 parent 623c911 commit fef8475

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

.travis.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ matrix:
5656
export -f install_test
5757
5858
script:
59-
# Test that we find Guzzle
60-
- install_test will-find "Http\Discovery\HttpClientDiscovery::find();" "php-http/guzzle6-adapter"
59+
# Test that we find Guzzle 6 v1
60+
- install_test will-find "Http\Discovery\HttpClientDiscovery::find();" "php-http/guzzle6-adapter:^1.1.1"
61+
# Test that we find Guzzle 6 v2
62+
- install_test will-find "Http\Discovery\HttpClientDiscovery::find();" "php-http/guzzle6-adapter:^2.0.1"
6163
# Test that we find a client with Symfony and Guzzle PSR-7
6264
- 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"
6365
# We should fail if we dont have php-http/message-factory or PSR-17
@@ -69,6 +71,10 @@ matrix:
6971
- 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"
7072
# Test that we find an async client with Symfony and Guzzle
7173
- 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"
74+
# Test that we find PSR-18 Guzzle 6
75+
- install_test will-find "Http\Discovery\Psr18ClientDiscovery::find();" "php-http/guzzle6-adapter:^2.0.1"
76+
# Test that we find PSR-18 Guzzle 7
77+
- install_test will-find "Http\Discovery\Psr18ClientDiscovery::find();" "guzzlehttp/guzzle:^7.0.1"
7278

7379
before_install:
7480
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Added
6+
7+
- Support discovering PSR-18 factories of `guzzlehttp/guzzle` 7+
8+
59
## 1.8.0 - 2020-06-14
610

711
### Added

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
},
4141
"extra": {
4242
"branch-alias": {
43-
"dev-master": "1.7-dev"
43+
"dev-master": "1.9-dev"
4444
}
4545
},
4646
"conflict": {

src/Strategy/CommonClassesStrategy.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Http\Message\StreamFactory\SlimStreamFactory;
2727
use Http\Message\UriFactory\SlimUriFactory;
2828
use Slim\Http\Request as SlimRequest;
29+
use GuzzleHttp\Client as GuzzleHttp;
2930
use Http\Adapter\Guzzle6\Client as Guzzle6;
3031
use Http\Adapter\Guzzle5\Client as Guzzle5;
3132
use Http\Client\Curl\Client as Curl;
@@ -95,6 +96,10 @@ final class CommonClassesStrategy implements DiscoveryStrategy
9596
'class' => [self::class, 'symfonyPsr18Instantiate'],
9697
'condition' => [SymfonyPsr18::class, Psr17RequestFactory::class],
9798
],
99+
[
100+
'class' => GuzzleHttp::class,
101+
'condition' => GuzzleHttp::class,
102+
],
98103
[
99104
'class' => [self::class, 'buzzInstantiate'],
100105
'condition' => [\Buzz\Client\FileGetContents::class, \Buzz\Message\ResponseBuilder::class],
@@ -108,27 +113,43 @@ final class CommonClassesStrategy implements DiscoveryStrategy
108113
public static function getCandidates($type)
109114
{
110115
if (Psr18Client::class === $type) {
111-
$candidates = self::$classes[PSR18Client::class];
116+
return self::getPsr18Candidates();
117+
}
118+
119+
return self::$classes[$type] ?? [];
120+
}
121+
122+
/**
123+
* @return array The return value is always an array with zero or more elements. Each
124+
* element is an array with two keys ['class' => string, 'condition' => mixed].
125+
*/
126+
private static function getPsr18Candidates()
127+
{
128+
$candidates = [];
112129

113-
// HTTPlug 2.0 clients implements PSR18Client too.
114-
foreach (self::$classes[HttpClient::class] as $c) {
115-
try {
116-
if (is_subclass_of($c['class'], Psr18Client::class)) {
117-
$candidates[] = $c;
118-
}
119-
} catch (\Throwable $e) {
120-
trigger_error(sprintf('Got exception "%s (%s)" while checking if a PSR-18 Client is available', get_class($e), $e->getMessage()), E_USER_WARNING);
130+
// Guzzle 6 does not implement the PSR-18 client interface, but Guzzle 7 does.
131+
foreach (self::$classes[Psr18Client::class] ?? [] as $c) {
132+
if (GuzzleHttp::class === $c['class']) {
133+
if (defined('GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
134+
$candidates[] = $c;
121135
}
136+
} else {
137+
$candidates[] = $c;
122138
}
123-
124-
return $candidates;
125139
}
126140

127-
if (isset(self::$classes[$type])) {
128-
return self::$classes[$type];
141+
// HTTPlug 2.0 clients implements PSR18Client too.
142+
foreach (self::$classes[HttpClient::class] as $c) {
143+
try {
144+
if (is_subclass_of($c['class'], Psr18Client::class)) {
145+
$candidates[] = $c;
146+
}
147+
} catch (\Throwable $e) {
148+
trigger_error(sprintf('Got exception "%s (%s)" while checking if a PSR-18 Client is available', get_class($e), $e->getMessage()), E_USER_WARNING);
149+
}
129150
}
130151

131-
return [];
152+
return $candidates;
132153
}
133154

134155
public static function buzzInstantiate()

0 commit comments

Comments
 (0)