Skip to content

Commit b1f0964

Browse files
authored
feat: Guzzle 7 support (googleapis#1868)
1 parent c7f965f commit b1f0964

7 files changed

Lines changed: 69 additions & 16 deletions

File tree

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
"license": "Apache-2.0",
88
"require": {
99
"php": ">=5.4",
10-
"google/auth": "^1.9",
10+
"google/auth": "^1.10",
1111
"google/apiclient-services": "~0.13",
1212
"firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0",
1313
"monolog/monolog": "^1.17|^2.0",
1414
"phpseclib/phpseclib": "~0.3.10||~2.0",
15-
"guzzlehttp/guzzle": "~5.3.1||~6.0",
15+
"guzzlehttp/guzzle": "~5.3.1||~6.0||~7.0",
1616
"guzzlehttp/psr7": "^1.2"
1717
},
1818
"require-dev": {

src/Google/AuthHandler/AuthHandlerFactory.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,20 @@ class Google_AuthHandler_AuthHandlerFactory
2828
*/
2929
public static function build($cache = null, array $cacheConfig = [])
3030
{
31-
$version = ClientInterface::VERSION;
31+
$guzzleVersion = null;
32+
if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
33+
$guzzleVersion = ClientInterface::MAJOR_VERSION;
34+
} elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) {
35+
$guzzleVersion = (int) substr(ClientInterface::VERSION, 0, 1);
36+
}
3237

33-
switch ($version[0]) {
34-
case '5':
38+
switch ($guzzleVersion) {
39+
case 5:
3540
return new Google_AuthHandler_Guzzle5AuthHandler($cache, $cacheConfig);
36-
case '6':
41+
case 6:
3742
return new Google_AuthHandler_Guzzle6AuthHandler($cache, $cacheConfig);
43+
case 7:
44+
return new Google_AuthHandler_Guzzle7AuthHandler($cache, $cacheConfig);
3845
default:
3946
throw new Exception('Version not supported');
4047
}

src/Google/AuthHandler/Guzzle6AuthHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Psr\Cache\CacheItemPoolInterface;
1212

1313
/**
14-
*
14+
* This supports Guzzle 6
1515
*/
1616
class Google_AuthHandler_Guzzle6AuthHandler
1717
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* This supports Guzzle 7
20+
*/
21+
class Google_AuthHandler_Guzzle7AuthHandler extends Google_AuthHandler_Guzzle6AuthHandler
22+
{
23+
}

src/Google/Client.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,22 +1124,29 @@ public function setApiFormatV2($value)
11241124

11251125
protected function createDefaultHttpClient()
11261126
{
1127-
$options = ['exceptions' => false];
1127+
$guzzleVersion = null;
1128+
if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
1129+
$guzzleVersion = ClientInterface::MAJOR_VERSION;
1130+
} elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) {
1131+
$guzzleVersion = (int)substr(ClientInterface::VERSION, 0, 1);
1132+
}
11281133

1129-
$version = ClientInterface::VERSION;
1130-
if ('5' === $version[0]) {
1134+
$options = ['exceptions' => false];
1135+
if (5 === $guzzleVersion) {
11311136
$options = [
11321137
'base_url' => $this->config['base_path'],
11331138
'defaults' => $options,
11341139
];
11351140
if ($this->isAppEngine()) {
11361141
// set StreamHandler on AppEngine by default
1137-
$options['handler'] = new StreamHandler();
1142+
$options['handler'] = new StreamHandler();
11381143
$options['defaults']['verify'] = '/etc/ca-certificates.crt';
11391144
}
1140-
} else {
1141-
// guzzle 6
1145+
} elseif (6 === $guzzleVersion || 7 === $guzzleVersion) {
1146+
// guzzle 6 or 7
11421147
$options['base_uri'] = $this->config['base_path'];
1148+
} else {
1149+
throw new LogicException('Could not find supported version of Guzzle.');
11431150
}
11441151

11451152
return new Client($options);

tests/BaseTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private function createClient()
5959
}
6060

6161
// adjust constructor depending on guzzle version
62-
if (!$this->isGuzzle6()) {
62+
if ($this->isGuzzle5()) {
6363
$options = ['defaults' => $options];
6464
}
6565

@@ -208,15 +208,31 @@ protected function loadExample($example)
208208
return false;
209209
}
210210

211+
protected function isGuzzle7()
212+
{
213+
if (!defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
214+
return false;
215+
}
216+
217+
return (7 === ClientInterface::MAJOR_VERSION);
218+
}
219+
211220
protected function isGuzzle6()
212221
{
222+
if (!defined('\GuzzleHttp\ClientInterface::VERSION')) {
223+
return false;
224+
}
213225
$version = ClientInterface::VERSION;
214226

215227
return ('6' === $version[0]);
216228
}
217229

218230
protected function isGuzzle5()
219231
{
232+
if (!defined('\GuzzleHttp\ClientInterface::VERSION')) {
233+
return false;
234+
}
235+
220236
$version = ClientInterface::VERSION;
221237

222238
return ('5' === $version[0]);

tests/Google/ClientTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testSignAppKey()
4444

4545
private function checkAuthHandler($http, $className)
4646
{
47-
if ($this->isGuzzle6()) {
47+
if ($this->isGuzzle6() || $this->isGuzzle7()) {
4848
$stack = $http->getConfig('handler');
4949
$class = new ReflectionClass(get_class($stack));
5050
$property = $class->getProperty('stack');
@@ -75,7 +75,7 @@ private function checkAuthHandler($http, $className)
7575

7676
private function checkCredentials($http, $fetcherClass, $sub = null)
7777
{
78-
if ($this->isGuzzle6()) {
78+
if ($this->isGuzzle6() || $this->isGuzzle7()) {
7979
$stack = $http->getConfig('handler');
8080
$class = new ReflectionClass(get_class($stack));
8181
$property = $class->getProperty('stack');

0 commit comments

Comments
 (0)