3
3
namespace Gitlab \HttpClient ;
4
4
5
5
use Http \Client \Common \HttpMethodsClient ;
6
+ use Http \Client \Common \HttpMethodsClientInterface ;
6
7
use Http \Client \Common \Plugin ;
7
8
use Http \Client \Common \PluginClient ;
9
+ use Http \Client \Common \Plugin \Cache \Generator \HeaderCacheKeyGenerator ;
10
+ use Http \Client \Common \Plugin \CachePlugin ;
8
11
use Http \Client \Common \PluginClientFactory ;
9
12
use Http \Discovery \Psr17FactoryDiscovery ;
10
13
use Http \Discovery \Psr18ClientDiscovery ;
14
+ use Psr \Cache \CacheItemPoolInterface ;
11
15
use Psr \Http \Client \ClientInterface ;
12
16
use Psr \Http \Message \RequestFactoryInterface ;
13
17
use Psr \Http \Message \StreamFactoryInterface ;
14
18
15
19
/**
16
20
* A builder that builds the API client.
21
+ *
17
22
* This will allow you to fluently add and remove plugins.
18
23
*
19
24
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
25
+ * @author Graham Campbell <graham@alt-three.com>
20
26
*/
21
27
class Builder
22
28
{
@@ -30,16 +36,20 @@ class Builder
30
36
/**
31
37
* A HTTP client with all our plugins.
32
38
*
33
- * @var HttpMethodsClient
39
+ * @var HttpMethodsClientInterface
34
40
*/
35
41
private $ pluginClient ;
36
42
37
43
/**
44
+ * The HTTP request factory.
45
+ *
38
46
* @var RequestFactoryInterface
39
47
*/
40
48
private $ requestFactory ;
41
49
42
50
/**
51
+ * The HTTP stream factory.
52
+ *
43
53
* @var StreamFactoryInterface
44
54
*/
45
55
private $ streamFactory ;
@@ -52,11 +62,24 @@ class Builder
52
62
private $ httpClientModified = true ;
53
63
54
64
/**
65
+ * The currently registered plugins.
66
+ *
55
67
* @var Plugin[]
56
68
*/
57
69
private $ plugins = [];
58
70
59
71
/**
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
+ *
60
83
* @param ClientInterface|null $httpClient
61
84
* @param RequestFactoryInterface|null $requestFactory
62
85
* @param StreamFactoryInterface|null $streamFactory
@@ -66,21 +89,26 @@ public function __construct(
66
89
RequestFactoryInterface $ requestFactory = null ,
67
90
StreamFactoryInterface $ streamFactory = null
68
91
) {
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 ();
72
95
}
73
96
74
97
/**
75
- * @return HttpMethodsClient
98
+ * @return HttpMethodsClientInterface
76
99
*/
77
100
public function getHttpClient ()
78
101
{
79
102
if ($ this ->httpClientModified ) {
80
103
$ this ->httpClientModified = false ;
81
104
105
+ $ plugins = $ this ->plugins ;
106
+ if ($ this ->cachePlugin !== null ) {
107
+ $ plugins [] = $ this ->cachePlugin ;
108
+ }
109
+
82
110
$ this ->pluginClient = new HttpMethodsClient (
83
- (new PluginClientFactory ())->createClient ($ this ->httpClient , $ this -> plugins ),
111
+ (new PluginClientFactory ())->createClient ($ this ->httpClient , $ plugins ),
84
112
$ this ->requestFactory
85
113
);
86
114
}
@@ -92,6 +120,8 @@ public function getHttpClient()
92
120
* Add a new plugin to the end of the plugin chain.
93
121
*
94
122
* @param Plugin $plugin
123
+ *
124
+ * @return void
95
125
*/
96
126
public function addPlugin (Plugin $ plugin )
97
127
{
@@ -103,6 +133,8 @@ public function addPlugin(Plugin $plugin)
103
133
* Remove a plugin by its fully qualified class name (FQCN).
104
134
*
105
135
* @param string $fqcn
136
+ *
137
+ * @return void
106
138
*/
107
139
public function removePlugin ($ fqcn )
108
140
{
@@ -113,4 +145,33 @@ public function removePlugin($fqcn)
113
145
}
114
146
}
115
147
}
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
+ }
116
177
}
0 commit comments