Skip to content
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

feat: Guzzle 7 support #1868

Merged
merged 8 commits into from
Jul 9, 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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"license": "Apache-2.0",
"require": {
"php": ">=5.4",
"google/auth": "^1.9",
"google/auth": "^1.10",
"google/apiclient-services": "~0.13",
"firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0",
"monolog/monolog": "^1.17|^2.0",
"phpseclib/phpseclib": "~0.3.10||~2.0",
"guzzlehttp/guzzle": "~5.3.1||~6.0",
"guzzlehttp/guzzle": "~5.3.1||~6.0||~7.0",
"guzzlehttp/psr7": "^1.2"
},
"require-dev": {
Expand Down
15 changes: 11 additions & 4 deletions src/Google/AuthHandler/AuthHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ class Google_AuthHandler_AuthHandlerFactory
*/
public static function build($cache = null, array $cacheConfig = [])
{
$version = ClientInterface::VERSION;
$guzzleVersion = null;
if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
$guzzleVersion = ClientInterface::MAJOR_VERSION;
} elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) {
$guzzleVersion = (int) substr(ClientInterface::VERSION, 0, 1);
}

switch ($version[0]) {
case '5':
switch ($guzzleVersion) {
case 5:
return new Google_AuthHandler_Guzzle5AuthHandler($cache, $cacheConfig);
case '6':
case 6:
return new Google_AuthHandler_Guzzle6AuthHandler($cache, $cacheConfig);
case 7:
return new Google_AuthHandler_Guzzle7AuthHandler($cache, $cacheConfig);
default:
throw new Exception('Version not supported');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Google/AuthHandler/Guzzle6AuthHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Psr\Cache\CacheItemPoolInterface;

/**
*
* This supports Guzzle 6
*/
class Google_AuthHandler_Guzzle6AuthHandler
{
Expand Down
23 changes: 23 additions & 0 deletions src/Google/AuthHandler/Guzzle7AuthHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* This supports Guzzle 7
*/
class Google_AuthHandler_Guzzle7AuthHandler extends Google_AuthHandler_Guzzle6AuthHandler
{
}
19 changes: 13 additions & 6 deletions src/Google/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1124,22 +1124,29 @@ public function setApiFormatV2($value)

protected function createDefaultHttpClient()
{
$options = ['exceptions' => false];
$guzzleVersion = null;
if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
$guzzleVersion = ClientInterface::MAJOR_VERSION;
} elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) {
$guzzleVersion = (int)substr(ClientInterface::VERSION, 0, 1);
}

$version = ClientInterface::VERSION;
if ('5' === $version[0]) {
$options = ['exceptions' => false];
if (5 === $guzzleVersion) {
$options = [
'base_url' => $this->config['base_path'],
'defaults' => $options,
];
if ($this->isAppEngine()) {
// set StreamHandler on AppEngine by default
$options['handler'] = new StreamHandler();
$options['handler'] = new StreamHandler();
$options['defaults']['verify'] = '/etc/ca-certificates.crt';
}
} else {
// guzzle 6
} elseif (6 === $guzzleVersion || 7 === $guzzleVersion) {
// guzzle 6 or 7
$options['base_uri'] = $this->config['base_path'];
} else {
throw new LogicException('Could not find supported version of Guzzle.');
}

return new Client($options);
Expand Down
18 changes: 17 additions & 1 deletion tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private function createClient()
}

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

Expand Down Expand Up @@ -208,15 +208,31 @@ protected function loadExample($example)
return false;
}

protected function isGuzzle7()
{
if (!defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
return false;
}

return (7 === ClientInterface::MAJOR_VERSION);
}

protected function isGuzzle6()
{
if (!defined('\GuzzleHttp\ClientInterface::VERSION')) {
return false;
}
$version = ClientInterface::VERSION;

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

protected function isGuzzle5()
{
if (!defined('\GuzzleHttp\ClientInterface::VERSION')) {
return false;
}

$version = ClientInterface::VERSION;

return ('5' === $version[0]);
Expand Down
4 changes: 2 additions & 2 deletions tests/Google/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testSignAppKey()

private function checkAuthHandler($http, $className)
{
if ($this->isGuzzle6()) {
if ($this->isGuzzle6() || $this->isGuzzle7()) {
$stack = $http->getConfig('handler');
$class = new ReflectionClass(get_class($stack));
$property = $class->getProperty('stack');
Expand Down Expand Up @@ -75,7 +75,7 @@ private function checkAuthHandler($http, $className)

private function checkCredentials($http, $fetcherClass, $sub = null)
{
if ($this->isGuzzle6()) {
if ($this->isGuzzle6() || $this->isGuzzle7()) {
$stack = $http->getConfig('handler');
$class = new ReflectionClass(get_class($stack));
$property = $class->getProperty('stack');
Expand Down