Skip to content

Commit 433f0d8

Browse files
Cache and methods client improvements
1 parent 367546b commit 433f0d8

File tree

6 files changed

+81
-24
lines changed

6 files changed

+81
-24
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"php-http/discovery": "^1.9",
2929
"php-http/httplug": "^2.1",
3030
"php-http/multipart-stream-builder": "^1.1",
31+
"psr/cache": "^1.0",
3132
"psr/http-client-implementation": "^1.0",
3233
"psr/http-factory-implementation": "^1.0",
3334
"psr/http-message": "^1.0",
@@ -37,10 +38,10 @@
3738
"guzzlehttp/psr7": "^1.5.2",
3839
"php-http/guzzle6-adapter": "^2.0.1",
3940
"http-interop/http-factory-guzzle": "^1.0",
40-
"phpunit/phpunit": "^7.5.15",
4141
"phpstan/phpstan": "^0.12.32",
4242
"phpstan/extension-installer": "^1.0.4",
43-
"phpstan/phpstan-deprecation-rules": "^0.12.4"
43+
"phpstan/phpstan-deprecation-rules": "^0.12.4",
44+
"phpunit/phpunit": "^7.5.15"
4445
},
4546
"autoload": {
4647
"psr-4": { "Gitlab\\": "lib/Gitlab/" }

lib/Gitlab/Api/AbstractApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ abstract class AbstractApi implements ApiInterface
3939
public function __construct(Client $client, StreamFactoryInterface $streamFactory = null)
4040
{
4141
$this->client = $client;
42-
$this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory();
42+
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory();
4343
}
4444

4545
/**

lib/Gitlab/Client.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Gitlab\HttpClient\Plugin\History;
88
use Gitlab\HttpClient\Plugin\Authentication;
99
use Gitlab\HttpClient\Plugin\GitlabExceptionThrower;
10-
use Http\Client\Common\HttpMethodsClient;
10+
use Http\Client\Common\HttpMethodsClientInterface;
1111
use Http\Client\Common\Plugin\AddHostPlugin;
1212
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
1313
use Http\Client\Common\Plugin\HistoryPlugin;
@@ -78,7 +78,7 @@ class Client
7878
public function __construct(Builder $httpClientBuilder = null)
7979
{
8080
$this->responseHistory = new History();
81-
$this->httpClientBuilder = $httpClientBuilder ?: new Builder();
81+
$this->httpClientBuilder = $httpClientBuilder ?? new Builder();
8282

8383
$this->httpClientBuilder->addPlugin(new GitlabExceptionThrower());
8484
$this->httpClientBuilder->addPlugin(new HistoryPlugin($this->responseHistory));
@@ -456,7 +456,7 @@ public function __get($api)
456456
}
457457

458458
/**
459-
* @return HttpMethodsClient
459+
* @return HttpMethodsClientInterface
460460
*/
461461
public function getHttpClient()
462462
{

lib/Gitlab/HttpClient/Builder.php

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@
33
namespace Gitlab\HttpClient;
44

55
use Http\Client\Common\HttpMethodsClient;
6+
use Http\Client\Common\HttpMethodsClientInterface;
67
use Http\Client\Common\Plugin;
78
use Http\Client\Common\PluginClient;
9+
use Http\Client\Common\Plugin\Cache\Generator\HeaderCacheKeyGenerator;
10+
use Http\Client\Common\Plugin\CachePlugin;
811
use Http\Client\Common\PluginClientFactory;
912
use Http\Discovery\Psr17FactoryDiscovery;
1013
use Http\Discovery\Psr18ClientDiscovery;
14+
use Psr\Cache\CacheItemPoolInterface;
1115
use Psr\Http\Client\ClientInterface;
1216
use Psr\Http\Message\RequestFactoryInterface;
1317
use Psr\Http\Message\StreamFactoryInterface;
1418

1519
/**
1620
* A builder that builds the API client.
21+
*
1722
* This will allow you to fluently add and remove plugins.
1823
*
1924
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
25+
* @author Graham Campbell <graham@alt-three.com>
2026
*/
2127
class Builder
2228
{
@@ -30,16 +36,20 @@ class Builder
3036
/**
3137
* A HTTP client with all our plugins.
3238
*
33-
* @var HttpMethodsClient
39+
* @var HttpMethodsClientInterface
3440
*/
3541
private $pluginClient;
3642

3743
/**
44+
* The HTTP request factory.
45+
*
3846
* @var RequestFactoryInterface
3947
*/
4048
private $requestFactory;
4149

4250
/**
51+
* The HTTP stream factory.
52+
*
4353
* @var StreamFactoryInterface
4454
*/
4555
private $streamFactory;
@@ -52,11 +62,24 @@ class Builder
5262
private $httpClientModified = true;
5363

5464
/**
65+
* The currently registered plugins.
66+
*
5567
* @var Plugin[]
5668
*/
5769
private $plugins = [];
5870

5971
/**
72+
* The cache plugin to use.
73+
*
74+
* This plugin is specially treated because it has to be the very last plugin.
75+
*
76+
* @var CachePlugin|null
77+
*/
78+
private $cachePlugin;
79+
80+
/**
81+
* Create a new http client builder instance.
82+
*
6083
* @param ClientInterface|null $httpClient
6184
* @param RequestFactoryInterface|null $requestFactory
6285
* @param StreamFactoryInterface|null $streamFactory
@@ -66,21 +89,26 @@ public function __construct(
6689
RequestFactoryInterface $requestFactory = null,
6790
StreamFactoryInterface $streamFactory = null
6891
) {
69-
$this->httpClient = $httpClient ?: Psr18ClientDiscovery::find();
70-
$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
71-
$this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory();
92+
$this->httpClient = $httpClient ?? Psr18ClientDiscovery::find();
93+
$this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory();
94+
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory();
7295
}
7396

7497
/**
75-
* @return HttpMethodsClient
98+
* @return HttpMethodsClientInterface
7699
*/
77100
public function getHttpClient()
78101
{
79102
if ($this->httpClientModified) {
80103
$this->httpClientModified = false;
81104

105+
$plugins = $this->plugins;
106+
if ($this->cachePlugin !== null) {
107+
$plugins[] = $this->cachePlugin;
108+
}
109+
82110
$this->pluginClient = new HttpMethodsClient(
83-
(new PluginClientFactory())->createClient($this->httpClient, $this->plugins),
111+
(new PluginClientFactory())->createClient($this->httpClient, $plugins),
84112
$this->requestFactory
85113
);
86114
}
@@ -92,6 +120,8 @@ public function getHttpClient()
92120
* Add a new plugin to the end of the plugin chain.
93121
*
94122
* @param Plugin $plugin
123+
*
124+
* @return void
95125
*/
96126
public function addPlugin(Plugin $plugin)
97127
{
@@ -103,6 +133,8 @@ public function addPlugin(Plugin $plugin)
103133
* Remove a plugin by its fully qualified class name (FQCN).
104134
*
105135
* @param string $fqcn
136+
*
137+
* @return void
106138
*/
107139
public function removePlugin($fqcn)
108140
{
@@ -113,4 +145,33 @@ public function removePlugin($fqcn)
113145
}
114146
}
115147
}
148+
149+
/**
150+
* Add a cache plugin to cache responses locally.
151+
*
152+
* @param CacheItemPoolInterface $cachePool
153+
* @param array $config
154+
*
155+
* @return void
156+
*/
157+
public function addCache(CacheItemPoolInterface $cachePool, array $config = [])
158+
{
159+
if (!isset($config['cache_key_generator'])) {
160+
$config['cache_key_generator'] = new HeaderCacheKeyGenerator(['Authorization', 'Cookie', 'Accept', 'Content-type']);
161+
}
162+
163+
$this->cachePlugin = CachePlugin::clientCache($cachePool, $this->streamFactory, $config);
164+
$this->httpClientModified = true;
165+
}
166+
167+
/**
168+
* Remove the cache plugin.
169+
*
170+
* @return void
171+
*/
172+
public function removeCache()
173+
{
174+
$this->cachePlugin = null;
175+
$this->httpClientModified = true;
176+
}
116177
}

test/Gitlab/Tests/HttpClient/BuilderTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Gitlab\Tests\HttpClient;
44

55
use Gitlab\HttpClient\Builder;
6-
use Http\Client\Common\HttpMethodsClient;
6+
use Http\Client\Common\HttpMethodsClientInterface;
77
use Http\Client\Common\Plugin;
88
use PHPUnit\Framework\TestCase;
99
use Psr\Http\Client\ClientInterface;
@@ -20,7 +20,10 @@ class BuilderTest extends TestCase
2020
*/
2121
private $subject;
2222

23-
public function setUp()
23+
/**
24+
* @before
25+
*/
26+
public function initBuilder()
2427
{
2528
$this->subject = new Builder(
2629
$this->getMockBuilder(ClientInterface::class)->getMock(),
@@ -51,6 +54,6 @@ public function testRemovePluginShouldInvalidateHttpClient()
5154

5255
public function testHttpClientShouldBeAnHttpMethodsClient()
5356
{
54-
$this->assertInstanceOf(HttpMethodsClient::class, $this->subject->getHttpClient());
57+
$this->assertInstanceOf(HttpMethodsClientInterface::class, $this->subject->getHttpClient());
5558
}
5659
}

test/Gitlab/Tests/ResultPagerTest.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Gitlab\ResultPager;
99
use GuzzleHttp\Psr7\Response;
1010
use function GuzzleHttp\Psr7\stream_for;
11-
use Http\Client\Common\HttpMethodsClient;
1211
use Http\Client\Common\HttpMethodsClientInterface;
1312
use PHPUnit\Framework\TestCase;
1413

@@ -72,14 +71,7 @@ public function testFetchAll()
7271
))
7372
;
7473

75-
if (interface_exists(HttpMethodsClientInterface::class)) {
76-
$httpClient = $this->createMock(HttpMethodsClientInterface::class);
77-
} else {
78-
$httpClient = $this->getMockBuilder(HttpMethodsClient::class)
79-
->disableOriginalConstructor()
80-
->getMock()
81-
;
82-
}
74+
$httpClient = $this->createMock(HttpMethodsClientInterface::class);
8375

8476
$httpClient->expects($this->exactly(2))
8577
->method('get')

0 commit comments

Comments
 (0)