Skip to content

Commit 729bb03

Browse files
authored
Merge pull request KnpLabs#389 from Nyholm/httplug
Decouple from Guzzle
2 parents 98d0bcd + fcfc29c commit 729bb03

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1001
-1882
lines changed

.styleci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
preset: psr2
22

33
enabled:
4-
- long_array_syntax
54
- return

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
language: php
22

33
php:
4-
- 5.3
5-
- 5.4
64
- 5.5
75
- 5.6
86
- 7.0

README.md

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ Uses [GitHub API v3](http://developer.github.com/v3/). The object API is very si
99

1010
## Features
1111

12-
* Follows PSR-0 conventions and coding standard: autoload friendly
12+
* Follows PSR-4 conventions and coding standard: autoload friendly
1313
* Light and fast thanks to lazy loading of API classes
1414
* Extensively tested and documented
1515

1616
## Requirements
1717

18-
* PHP >= 5.3.2 with [cURL](http://php.net/manual/en/book.curl.php) extension,
18+
* PHP >= 5.5
1919
* [Guzzle](https://github.com/guzzle/guzzle) library,
2020
* (optional) PHPUnit to run tests.
2121

@@ -28,21 +28,12 @@ The first step to use `php-github-api` is to download composer:
2828
$ curl -s http://getcomposer.org/installer | php
2929
```
3030

31-
Then we have to install our dependencies using:
31+
Then run the following command to require the library:
3232
```bash
33-
$ php composer.phar install
34-
```
35-
Now we can use autoloader from Composer by:
36-
37-
```json
38-
{
39-
"require": {
40-
"knplabs/github-api": "~1.4"
41-
}
42-
}
33+
$ php composer.phar require knplabs/github-api php-http/guzzle6-adapter
4334
```
4435

45-
> `php-github-api` follows the PSR-4 convention names for its classes, which means you can easily integrate `php-github-api` classes loading in your own autoloader.
36+
Why `php-http/guzzle6-adapter`? We are decoupled form any HTTP messaging client with help by [HTTPlug](http://httplug.io/). Read about clients in our [docs](doc/customize.md).
4637

4738
## Using Laravel?
4839

@@ -70,19 +61,15 @@ From `$client` object, you can access to all GitHub.
7061
// This file is generated by Composer
7162
require_once 'vendor/autoload.php';
7263

73-
$client = new \Github\Client(
74-
new \Github\HttpClient\CachedHttpClient(array('cache_dir' => '/tmp/github-api-cache'))
75-
);
64+
$client = new \Github\Client();
65+
$client->useCache();
7666

7767
// Or select directly which cache you want to use
78-
$client = new \Github\HttpClient\CachedHttpClient();
79-
$client->setCache(
68+
$client->useCache(
8069
// Built in one, or any cache implementing this interface:
8170
// Github\HttpClient\Cache\CacheInterface
8271
new \Github\HttpClient\Cache\FilesystemCache('/tmp/github-api-cache')
8372
);
84-
85-
$client = new \Github\Client($client);
8673
```
8774

8875
Using cache, the client will get cached responses if resources haven't changed since last time,

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
}
1818
],
1919
"require": {
20-
"php": ">=5.3.2",
21-
"ext-curl": "*",
22-
"guzzle/guzzle": "~3.7"
20+
"php": "^5.5|^7.0",
21+
"php-http/httplug": "^1.0",
22+
"php-http/discovery": "^1.0",
23+
"php-http/client-implementation": "^1.0",
24+
"php-http/client-common": "^1.1"
2325
},
2426
"require-dev": {
2527
"phpunit/phpunit": "~4.0",
28+
"php-http/guzzle6-adapter": "~1.0",
29+
"guzzlehttp/psr7": "^1.2",
2630
"sllh/php-cs-fixer-styleci-bridge": "~1.3"
2731
},
2832
"suggest": {

doc/customize.md

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,43 @@
11
## Customize `php-github-api` and testing
22
[Back to the navigation](README.md)
33

4-
### Configure the http client
54

6-
Wanna change, let's say, the http client User Agent?
5+
### Inject a new HTTP client instance
76

8-
```php
9-
$client->getHttpClient()->setOption('user_agent', 'My new User Agent');
10-
```
11-
12-
See all available options in `Github/HttpClient/HttpClient.php`
13-
14-
### Guzzle events
7+
`php-github-api` relies on `php-http/discovery` to find an installed HTTP client. You may specify a HTTP client
8+
yourself by calling `\Github\Client::setHttpClient`. A HTTP client must implement `Http\Client\HttpClient`. A list of
9+
community provided clients is found here: https://packagist.org/providers/php-http/client-implementation
1510

16-
If you need to perform any special action on request/response use guzzle events:
11+
You can inject a HTTP client through `Github\Client#setHttpClient()` method:
1712

1813
```php
19-
use Guzzle\Common\Event;
20-
use Github\HttpClient\Message\ResponseMediator;
21-
22-
$client->getHttpClient()->addListener('request.success', function(Event $event) {
23-
$remaining = ResponseMediator::getApiLimit($event['response']);
24-
25-
var_dump($remaining);
26-
});
27-
28-
$client->user()->show('cursedcoder');
14+
$client = new Github\Client();
15+
$client->setHttpClient(new Http\Adapter\Guzzle6\Client());
2916
```
3017

31-
see list of events http://guzzle3.readthedocs.org/http-client/request.html#plugins-and-events
18+
### Configure the HTTP client
3219

33-
### Inject a new http client instance
34-
35-
`php-github-api` provides a curl-based implementation of a http client.
36-
If you want to use your own http client implementation, inject it to the `Github\Client` instance:
20+
Wanna change, let's say, the HTTP client User Agent? You need to create a Plugin that modifies the
21+
request. Read more about [HTTPlug plugins here](http://docs.php-http.org/en/latest/plugins/introduction.html#how-it-works).
3722

3823
```php
39-
use Github\HttpClient\HttpClient;
24+
use Http\Client\Common\Plugin;
25+
use Psr\Http\Message\RequestInterface;
4026

41-
// create a custom http client
42-
class MyHttpClient extends HttpClient
27+
class CustomUserAgentPlugin implements Plugin
4328
{
44-
public function request($url, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
4533
{
46-
// send the request and return the raw response
34+
$request->withHeader('user-agent', 'Foobar');
35+
36+
return $next($request);
4737
}
4838
}
49-
```
50-
51-
> Your http client implementation may not extend `Github\HttpClient\HttpClient`, but only implement `Github\HttpClient\HttpClientInterface`.
5239

53-
You can now inject your http client through `Github\Client#setHttpClient()` method:
54-
55-
```php
56-
$client = new Github\Client();
57-
$client->setHttpClient(new MyHttpClient());
40+
$githubClient->addPlugin(new CustomUserAgentPlugin());
5841
```
5942

6043
### Run Test Suite

lib/Github/Api/AbstractApi.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function setPerPage($perPage)
6363
* @param array $parameters GET parameters.
6464
* @param array $requestHeaders Request Headers.
6565
*
66-
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
66+
* @return array|string
6767
*/
6868
protected function get($path, array $parameters = array(), $requestHeaders = array())
6969
{
@@ -73,7 +73,12 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
7373
if (array_key_exists('ref', $parameters) && is_null($parameters['ref'])) {
7474
unset($parameters['ref']);
7575
}
76-
$response = $this->client->getHttpClient()->get($path, $parameters, $requestHeaders);
76+
77+
if (count($parameters) > 0) {
78+
$path .= '?'.http_build_query($parameters);
79+
}
80+
81+
$response = $this->client->getHttpClient()->get($path, $requestHeaders);
7782

7883
return ResponseMediator::getContent($response);
7984
}
@@ -85,17 +90,15 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
8590
* @param array $parameters HEAD parameters.
8691
* @param array $requestHeaders Request headers.
8792
*
88-
* @return \Guzzle\Http\Message\Response
93+
* @return \Psr\Http\Message\ResponseInterface
8994
*/
9095
protected function head($path, array $parameters = array(), $requestHeaders = array())
9196
{
9297
if (array_key_exists('ref', $parameters) && is_null($parameters['ref'])) {
9398
unset($parameters['ref']);
9499
}
95100

96-
$response = $this->client->getHttpClient()->request($path, null, 'HEAD', $requestHeaders, array(
97-
'query' => $parameters
98-
));
101+
$response = $this->client->getHttpClient()->head($path.'?'.http_build_query($parameters), $requestHeaders);
99102

100103
return $response;
101104
}
@@ -120,17 +123,17 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar
120123
* Send a POST request with raw data.
121124
*
122125
* @param string $path Request path.
123-
* @param $body Request body.
126+
* @param string $body Request body.
124127
* @param array $requestHeaders Request headers.
125128
*
126-
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
129+
* @return array|string
127130
*/
128131
protected function postRaw($path, $body, $requestHeaders = array())
129132
{
130133
$response = $this->client->getHttpClient()->post(
131134
$path,
132-
$body,
133-
$requestHeaders
135+
$requestHeaders,
136+
$body
134137
);
135138

136139
return ResponseMediator::getContent($response);
@@ -147,8 +150,8 @@ protected function patch($path, array $parameters = array(), $requestHeaders = a
147150
{
148151
$response = $this->client->getHttpClient()->patch(
149152
$path,
150-
$this->createJsonBody($parameters),
151-
$requestHeaders
153+
$requestHeaders,
154+
$this->createJsonBody($parameters)
152155
);
153156

154157
return ResponseMediator::getContent($response);
@@ -165,8 +168,8 @@ protected function put($path, array $parameters = array(), $requestHeaders = arr
165168
{
166169
$response = $this->client->getHttpClient()->put(
167170
$path,
168-
$this->createJsonBody($parameters),
169-
$requestHeaders
171+
$requestHeaders,
172+
$this->createJsonBody($parameters)
170173
);
171174

172175
return ResponseMediator::getContent($response);
@@ -183,8 +186,8 @@ protected function delete($path, array $parameters = array(), $requestHeaders =
183186
{
184187
$response = $this->client->getHttpClient()->delete(
185188
$path,
186-
$this->createJsonBody($parameters),
187-
$requestHeaders
189+
$requestHeaders,
190+
$this->createJsonBody($parameters)
188191
);
189192

190193
return ResponseMediator::getContent($response);

lib/Github/Api/AcceptHeaderTrait.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Github\Api;
4+
5+
/**
6+
* A trait to make sure we add accept headers on all requests.
7+
*
8+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
9+
*/
10+
trait AcceptHeaderTrait
11+
{
12+
protected $acceptHeaderValue = null;
13+
14+
protected function get($path, array $parameters = array(), $requestHeaders = array())
15+
{
16+
return parent::get($path, $parameters, $this->mergeHeaders($requestHeaders));
17+
}
18+
19+
protected function head($path, array $parameters = array(), $requestHeaders = array())
20+
{
21+
return parent::head($path, $parameters, $this->mergeHeaders($requestHeaders));
22+
}
23+
24+
protected function post($path, array $parameters = array(), $requestHeaders = array())
25+
{
26+
return parent::post($path, $parameters, $this->mergeHeaders($requestHeaders));
27+
}
28+
29+
protected function postRaw($path, $body, $requestHeaders = array())
30+
{
31+
return parent::postRaw($path, $body, $this->mergeHeaders($requestHeaders));
32+
}
33+
34+
protected function patch($path, array $parameters = array(), $requestHeaders = array())
35+
{
36+
return parent::patch($path, $parameters, $this->mergeHeaders($requestHeaders));
37+
}
38+
39+
protected function put($path, array $parameters = array(), $requestHeaders = array())
40+
{
41+
return parent::put($path, $parameters, $this->mergeHeaders($requestHeaders));
42+
}
43+
44+
protected function delete($path, array $parameters = array(), $requestHeaders = array())
45+
{
46+
return parent::delete($path, $parameters, $this->mergeHeaders($requestHeaders));
47+
}
48+
49+
/**
50+
* Append a new accept header on all requests
51+
* @return array
52+
*/
53+
private function mergeHeaders(array $headers = array())
54+
{
55+
$default = array();
56+
if ($this->acceptHeaderValue) {
57+
$default = array('Accept' => $this->acceptHeaderValue);
58+
}
59+
60+
return array_merge($default, $headers);
61+
}
62+
}

lib/Github/Api/Enterprise/ManagementConsole.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function keys($hash)
6868
* @param string $uri the request URI
6969
* @param string $hash md5 hash of your license
7070
*
71-
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
71+
* @return array|string
7272
*/
7373
protected function getWithLicenseHash($uri, $hash)
7474
{

lib/Github/Api/GitData/Blobs.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@
33
namespace Github\Api\GitData;
44

55
use Github\Api\AbstractApi;
6+
use Github\Api\AcceptHeaderTrait;
67
use Github\Exception\MissingArgumentException;
78

89
/**
910
* @link http://developer.github.com/v3/git/blobs/
1011
* @author Joseph Bielawski <stloyd@gmail.com>
12+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
1113
*/
1214
class Blobs extends AbstractApi
1315
{
16+
use AcceptHeaderTrait;
17+
1418
/**
1519
* Configure the Accept header depending on the blob type.
1620
*
1721
* @param string|null $bodyType
1822
*/
1923
public function configure($bodyType = null)
2024
{
21-
if ('raw' == $bodyType) {
22-
$this->client->setHeaders(array(
23-
'Accept' => sprintf('application/vnd.github.%s.raw', $this->client->getOption('api_version'))
24-
));
25+
if ('raw' === $bodyType) {
26+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.raw', $this->client->getOption('api_version'));
2527
}
2628
}
2729

0 commit comments

Comments
 (0)