Skip to content

Implement HTTPlug #538

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

Closed
wants to merge 2 commits into from
Closed
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 README.UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

## Upgrade Guide

### Upgrading to Version 1.0.0
### Upgrading to Version 1.5

TBD
You need to install `php-http/guzzle6-adapter` and ` php-http/message`, then use the libaray as normal.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,17 @@ $provider = new \League\OAuth2\Client\Provider\GenericProvider([
Via Composer

``` bash
$ composer require league/oauth2-client
$ composer require league/oauth2-client php-http/guzzle6-adapter php-http/message
```

The two extra packages from `php-http` is part of the [HTTPlug](http://httplug.io/) organisation. It helps us not to
be coupled to Guzzle. That is why you need to install the `php-http/guzzle6-adapter` or any other library providing the virtual
[php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation) package. The
`php-http/message` package contains factory classes to create Guzzle and Diactoros PSR-7 requests.

You can read more about HTTPlug and how to use it in
[their documentation](http://docs.php-http.org/en/latest/httplug/users.html).

## Contributing

Please see [CONTRIBUTING](https://github.com/thephpleague/oauth2-client/blob/master/CONTRIBUTING.md) for details.
Expand Down
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
"license": "MIT",
"require": {
"php": ">=5.5.0",
"psr/http-message": "^1.0",
"ext-curl": "*",
"ircmaxell/random-lib": "~1.1",
"guzzlehttp/guzzle": "~6.0"
"php-http/httplug": "^1.0",
"php-http/client-implementation": "^1.0",
"php-http/message-factory": "^1.0.2",
"php-http/discovery": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"mockery/mockery": "~0.9",
"squizlabs/php_codesniffer": "~2.0",
"satooshi/php-coveralls": "0.6.*",
"php-http/message": "^1.0",
"php-http/guzzle6-adapter": "^1.0",
"jakub-onderka/php-parallel-lint": "0.8.*"
},
"keywords": [
Expand Down
169 changes: 147 additions & 22 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

namespace League\OAuth2\Client\Provider;

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\ClientInterface as HttpClientInterface;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use GuzzleHttp\Client as GuzzleClient;
use Http\Adapter\Guzzle6\Client as HttplugGuzzle6;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use League\OAuth2\Client\Grant\AbstractGrant;
use League\OAuth2\Client\Grant\GrantFactory;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
Expand Down Expand Up @@ -85,7 +87,7 @@ abstract class AbstractProvider
protected $requestFactory;

/**
* @var HttpClientInterface
* @var \Http\Client\HttpClient
*/
protected $httpClient;

Expand All @@ -94,6 +96,20 @@ abstract class AbstractProvider
*/
protected $randomFactory;

/**
* The options given to the Guzzle6 client
* @var array
* @deprecated These are to be removed in the next major release.
* These options are only used in getHttpClient to keep BC.
*/
private $guzzle6Options = [];

/**
* @var GuzzleClientInterface
* @deprecated This client is only used in getHttpClient to keep BC.
*/
private $guzzleClient;

/**
* Constructs an OAuth 2.0 service provider.
*
Expand Down Expand Up @@ -123,14 +139,7 @@ public function __construct(array $options = [], array $collaborators = [])
}
$this->setRequestFactory($collaborators['requestFactory']);

if (empty($collaborators['httpClient'])) {
$client_options = $this->getAllowedClientOptions($options);

$collaborators['httpClient'] = new HttpClient(
array_intersect_key($options, array_flip($client_options))
);
}
$this->setHttpClient($collaborators['httpClient']);
$this->setHttpClient($this->getHttpClientFromOptions($options, $collaborators));

if (empty($collaborators['randomFactory'])) {
$collaborators['randomFactory'] = new RandomFactory();
Expand Down Expand Up @@ -207,11 +216,44 @@ public function getRequestFactory()
/**
* Sets the HTTP client instance.
*
* @param HttpClientInterface $client
* @param HttpClient $client
* @return self
*/
public function setHttpClient(HttpClientInterface $client)
public function setHttpClient($client)
{
if ($client instanceof GuzzleClientInterface) {
@trigger_error(
sprintf(
'Passing a "%s" to "%s::setHttpClient" is deprecated. Use a "Http\Client\HttpClient" instead.',
GuzzleClientInterface::class,
static::class
),
E_USER_DEPRECATED
);
if (!class_exists(HttplugGuzzle6::class)) {
throw new \RuntimeException(
sprintf(
'You must install "php-http/guzzle6-adapter" to be able to pass a "%s" to "%s::setHttpClient".',
GuzzleClientInterface::class,
static::class
)
);
}
$client = new HttplugGuzzle6($client);
}

if (!$client instanceof HttpClient) {
$type = is_object($client) ? get_class($client) : gettype($client);
throw new \RuntimeException(
sprintf(
'First parameter to "%s::setHttpClient" was expected to be a "%s", you provided a "%s"',
static::class,
HttpClient::class,
$type
)
);
}

$this->httpClient = $client;

return $this;
Expand All @@ -220,13 +262,43 @@ public function setHttpClient(HttpClientInterface $client)
/**
* Returns the HTTP client instance.
*
* @return HttpClientInterface
* @return HttpClient
*/
public function getHttpClient()
public function getHttplugClient()
{
return $this->httpClient;
}

/**
* Returns the HTTP client instance.
*
* @return GuzzleClientInterface
*/
public function getHttpClient()
{
@trigger_error(
sprintf(
'Using "%s::getHttpClient" is deprecated in favor for "%s::getHttplugClient".',
static::class,
static::class
),
E_USER_DEPRECATED
);

if ($this->guzzleClient !== null) {
return $this->guzzleClient;
}

if (!class_exists(GuzzleClient::class)) {
throw new \RuntimeException(
'You must install "php-http/guzzle6-adapter" to be able to use "%s::getHttplugClient".',
static::class
);
}

return new GuzzleClient($this->guzzle6Options);
}

/**
* Sets the instance of the CSPRNG random generator factory.
*
Expand Down Expand Up @@ -621,12 +693,7 @@ protected function createRequest($method, $url, $token, array $options)
*/
protected function sendRequest(RequestInterface $request)
{
try {
$response = $this->getHttpClient()->send($request);
} catch (BadResponseException $e) {
$response = $e->getResponse();
}
return $response;
return $this->getHttplugClient()->sendRequest($request);
}

/**
Expand Down Expand Up @@ -840,4 +907,62 @@ public function getHeaders($token = null)

return $this->getDefaultHeaders();
}

/**
* Get a HttpClient from constructor options
*
* @param array $options
* @param array $collaborators
*
* @return HttpClient
*/
private function getHttpClientFromOptions(array $options, array $collaborators)
{
if (!empty($collaborators['httplugClient'])) {
// Use provided client
return $collaborators['httplugClient'];
}

if (empty($collaborators['httpClient'])) {
$client_options = $this->getAllowedClientOptions($options);
$guzzle6Options = array_intersect_key($options, array_flip($client_options));

if (empty($guzzle6Options)) {
return HttpClientDiscovery::find();
} else {
@trigger_error(
'Passing options to Guzzle6 client is deprecated. Use "httplugClient" instead',
E_USER_DEPRECATED
);
if (!class_exists(HttplugGuzzle6::class)) {
throw new \RuntimeException(
'You must install "php-http/guzzle6-adapter" to be able to pass options to the Guzzle6 client.'
);
}
$this->guzzle6Options = $guzzle6Options;

return HttplugGuzzle6::createWithConfig($guzzle6Options);
}
}

@trigger_error('The "httpClient" option is deprecated. Use "httplugClient" instead', E_USER_DEPRECATED);
$client = $collaborators['httpClient'];
if (!$client instanceof GuzzleClientInterface) {
throw new \RuntimeException(
sprintf(
'The value provided with option "HttpClient" must be instance of "%s".',
GuzzleClientInterface::class
)
);
}

if (!class_exists(HttplugGuzzle6::class)) {
throw new \RuntimeException(
'You must install "php-http/guzzle6-adapter" to pass a Guzzle6 client with the "httpClient" option.'
);
}

$this->guzzleClient = $client;
return new HttplugGuzzle6($client);
}
}
19 changes: 15 additions & 4 deletions src/Tool/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

namespace League\OAuth2\Client\Tool;

use GuzzleHttp\Psr7\Request;
use Http\Discovery\MessageFactoryDiscovery;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;

/**
* Used to produce PSR-7 Request instances.
Expand All @@ -23,6 +25,11 @@
*/
class RequestFactory
{
/**
* @var \Http\Message\RequestFactory
*/
private $factory;

/**
* Creates a PSR-7 Request instance.
*
Expand All @@ -32,7 +39,7 @@ class RequestFactory
* @param string|resource|StreamInterface $body Message body.
* @param string $version HTTP protocol version.
*
* @return Request
* @return RequestInterface
*/
public function getRequest(
$method,
Expand All @@ -41,7 +48,11 @@ public function getRequest(
$body = null,
$version = '1.1'
) {
return new Request($method, $uri, $headers, $body, $version);
if (!$this->factory) {
$this->factory = MessageFactoryDiscovery::find();
}

return $this->factory->createRequest($method, $uri, $headers, $body, $version);
}

/**
Expand Down Expand Up @@ -70,7 +81,7 @@ protected function parseOptions(array $options)
* @param null|string $uri
* @param array $options
*
* @return Request
* @return RequestInterface
*/
public function getRequestWithOptions($method, $uri, array $options = [])
{
Expand Down
8 changes: 4 additions & 4 deletions test/src/Grant/GrantTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace League\OAuth2\Client\Test\Grant;

use GuzzleHttp\ClientInterface;
use Http\Client\HttpClient;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use League\OAuth2\Client\Token\AccessToken;
Expand Down Expand Up @@ -44,7 +44,7 @@ abstract public function providerGetAccessToken();
/**
* Callback to test access token request parameters.
*
* @return Closure
* @return \Closure
*/
abstract protected function getParamExpectation();

Expand All @@ -64,8 +64,8 @@ public function testGetAccessToken($grant, array $params = [])

$paramCheck = $this->getParamExpectation();

$client = m::mock(ClientInterface::class);
$client->shouldReceive('send')->with(
$client = m::mock(HttpClient::class);
$client->shouldReceive('sendRequest')->with(
$request = m::on(function ($request) use ($paramCheck) {
parse_str((string) $request->getBody(), $body);
return $paramCheck($body);
Expand Down
Loading