5
5
use Github \Api \ApiInterface ;
6
6
use Github \Exception \InvalidArgumentException ;
7
7
use Github \Exception \BadMethodCallException ;
8
+ use Github \HttpClient \Builder ;
8
9
use Github \HttpClient \Plugin \Authentication ;
9
10
use Github \HttpClient \Plugin \GithubExceptionThrower ;
10
11
use Github \HttpClient \Plugin \History ;
11
12
use Github \HttpClient \Plugin \PathPrepend ;
12
13
use Http \Client \Common \HttpMethodsClient ;
13
14
use Http \Client \Common \Plugin ;
14
- use Http \Client \Common \PluginClient ;
15
- use Http \Client \HttpClient ;
16
- use Http \Discovery \HttpClientDiscovery ;
17
- use Http \Discovery \MessageFactoryDiscovery ;
18
- use Http \Discovery \StreamFactoryDiscovery ;
19
15
use Http \Discovery \UriFactoryDiscovery ;
20
- use Http \Message \MessageFactory ;
21
- use Nyholm \Psr7 \Factory \StreamFactory ;
22
16
use Psr \Cache \CacheItemPoolInterface ;
23
17
24
18
/**
@@ -98,45 +92,9 @@ class Client
98
92
private $ apiVersion ;
99
93
100
94
/**
101
- * The object that sends HTTP messages
102
- *
103
- * @var HttpClient
104
- */
105
- private $ httpClient ;
106
-
107
- /**
108
- * A HTTP client with all our plugins
109
- *
110
- * @var PluginClient
111
- */
112
- private $ pluginClient ;
113
-
114
- /**
115
- * @var MessageFactory
116
- */
117
- private $ messageFactory ;
118
-
119
- /**
120
- * @var StreamFactory
121
- */
122
- private $ streamFactory ;
123
-
124
- /**
125
- * @var Plugin[]
126
- */
127
- private $ plugins = [];
128
-
129
- /**
130
- * True if we should create a new Plugin client at next request.
131
- * @var bool
95
+ * @var Builder
132
96
*/
133
- private $ httpClientModified = true ;
134
-
135
- /**
136
- * Http headers
137
- * @var array
138
- */
139
- private $ headers = [];
97
+ private $ httpClientBuilder ;
140
98
141
99
/**
142
100
* @var History
@@ -146,27 +104,28 @@ class Client
146
104
/**
147
105
* Instantiate a new GitHub client.
148
106
*
149
- * @param HttpClient |null $httpClient
150
- * @param string|null $apiVersion
151
- * @param string|null $enterpriseUrl
107
+ * @param Builder |null $httpClient
108
+ * @param string|null $apiVersion
109
+ * @param string|null $enterpriseUrl
152
110
*/
153
- public function __construct (HttpClient $ httpClient = null , $ apiVersion = null , $ enterpriseUrl = null )
111
+ public function __construct (Builder $ httpClientBuilder = null , $ apiVersion = null , $ enterpriseUrl = null )
154
112
{
155
- $ this ->httpClient = $ httpClient ?: HttpClientDiscovery::find ();
156
- $ this ->messageFactory = MessageFactoryDiscovery::find ();
157
- $ this ->streamFactory = StreamFactoryDiscovery::find ();
158
-
159
113
$ this ->responseHistory = new History ();
160
- $ this ->addPlugin (new GithubExceptionThrower ());
161
- $ this ->addPlugin (new Plugin \HistoryPlugin ($ this ->responseHistory ));
162
- $ this ->addPlugin (new Plugin \RedirectPlugin ());
163
- $ this ->addPlugin (new Plugin \AddHostPlugin (UriFactoryDiscovery::find ()->createUri ('https://api.github.com ' )));
164
- $ this ->addPlugin (new Plugin \HeaderDefaultsPlugin (array (
165
- 'User-Agent ' => 'php-github-api (http://github.com/KnpLabs/php-github-api) ' ,
114
+ $ this ->httpClientBuilder = new Builder ();
115
+ $ this ->httpClientBuilder ->addDefaultHeaders ([
116
+ 'Accept ' => sprintf ('application/vnd.github.%s+json ' , $ this ->getApiVersion ()),
117
+ ]);
118
+
119
+ $ this ->httpClientBuilder ->addPlugin (new GithubExceptionThrower ());
120
+ $ this ->httpClientBuilder ->addPlugin (new Plugin \HistoryPlugin ($ this ->responseHistory ));
121
+ $ this ->httpClientBuilder ->addPlugin (new Plugin \RedirectPlugin ());
122
+ $ this ->httpClientBuilder ->addPlugin (new Plugin \AddHostPlugin (UriFactoryDiscovery::find ()->createUri ('https://api.github.com ' )));
123
+ $ this ->httpClientBuilder ->addPlugin (new Plugin \HeaderDefaultsPlugin (array (
124
+ 'User-Agent ' => 'php-github-api (http://github.com/KnpLabs/php-github-api) ' ,
166
125
)));
167
126
168
127
$ this ->apiVersion = $ apiVersion ?: 'v3 ' ;
169
- $ this ->addHeaders (['Accept ' => sprintf ('application/vnd.github.%s+json ' , $ this ->apiVersion )]);
128
+ $ this ->httpClientBuilder -> addHeaders (['Accept ' => sprintf ('application/vnd.github.%s+json ' , $ this ->apiVersion )]);
170
129
171
130
if ($ enterpriseUrl ) {
172
131
$ this ->setEnterpriseUrl ($ enterpriseUrl );
@@ -313,15 +272,15 @@ public function authenticate($tokenOrLogin, $password = null, $authMethod = null
313
272
314
273
if (null === $ authMethod && in_array ($ password , array (self ::AUTH_URL_TOKEN , self ::AUTH_URL_CLIENT_ID , self ::AUTH_HTTP_PASSWORD , self ::AUTH_HTTP_TOKEN , self ::AUTH_JWT ))) {
315
274
$ authMethod = $ password ;
316
- $ password = null ;
275
+ $ password = null ;
317
276
}
318
277
319
278
if (null === $ authMethod ) {
320
279
$ authMethod = self ::AUTH_HTTP_PASSWORD ;
321
280
}
322
281
323
- $ this ->removePlugin (Authentication::class);
324
- $ this ->addPlugin (new Authentication ($ tokenOrLogin , $ password , $ authMethod ));
282
+ $ this ->httpClientBuilder -> removePlugin (Authentication::class);
283
+ $ this ->httpClientBuilder -> addPlugin (new Authentication ($ tokenOrLogin , $ password , $ authMethod ));
325
284
}
326
285
327
286
/**
@@ -331,64 +290,11 @@ public function authenticate($tokenOrLogin, $password = null, $authMethod = null
331
290
*/
332
291
private function setEnterpriseUrl ($ enterpriseUrl )
333
292
{
334
- $ this ->removePlugin (Plugin \AddHostPlugin::class);
335
- $ this ->removePlugin (PathPrepend::class);
336
-
337
- $ this ->addPlugin (new Plugin \AddHostPlugin (UriFactoryDiscovery::find ()->createUri ($ enterpriseUrl )));
338
- $ this ->addPlugin (new PathPrepend (sprintf ('/api/%s/ ' , $ this ->getApiVersion ())));
339
- }
340
-
341
- /**
342
- * Add a new plugin to the end of the plugin chain.
343
- *
344
- * @param Plugin $plugin
345
- */
346
- public function addPlugin (Plugin $ plugin )
347
- {
348
- $ this ->plugins [] = $ plugin ;
349
- $ this ->httpClientModified = true ;
350
- }
351
-
352
- /**
353
- * Remove a plugin by its fully qualified class name (FQCN).
354
- *
355
- * @param string $fqcn
356
- */
357
- public function removePlugin ($ fqcn )
358
- {
359
- foreach ($ this ->plugins as $ idx => $ plugin ) {
360
- if ($ plugin instanceof $ fqcn ) {
361
- unset($ this ->plugins [$ idx ]);
362
- $ this ->httpClientModified = true ;
363
- }
364
- }
365
- }
293
+ $ this ->httpClientBuilder ->removePlugin (Plugin \AddHostPlugin::class);
294
+ $ this ->httpClientBuilder ->removePlugin (PathPrepend::class);
366
295
367
- /**
368
- * @return HttpMethodsClient
369
- */
370
- public function getHttpClient ()
371
- {
372
- if ($ this ->httpClientModified ) {
373
- $ this ->httpClientModified = false ;
374
- $ this ->pushBackCachePlugin ();
375
-
376
- $ this ->pluginClient = new HttpMethodsClient (
377
- new PluginClient ($ this ->httpClient , $ this ->plugins ),
378
- $ this ->messageFactory
379
- );
380
- }
381
-
382
- return $ this ->pluginClient ;
383
- }
384
-
385
- /**
386
- * @param HttpClient $httpClient
387
- */
388
- public function setHttpClient (HttpClient $ httpClient )
389
- {
390
- $ this ->httpClientModified = true ;
391
- $ this ->httpClient = $ httpClient ;
296
+ $ this ->httpClientBuilder ->addPlugin (new Plugin \AddHostPlugin (UriFactoryDiscovery::find ()->createUri ($ enterpriseUrl )));
297
+ $ this ->httpClientBuilder ->addPlugin (new PathPrepend (sprintf ('/api/%s/ ' , $ this ->getApiVersion ())));
392
298
}
393
299
394
300
/**
@@ -399,30 +305,6 @@ public function getApiVersion()
399
305
return $ this ->apiVersion ;
400
306
}
401
307
402
- /**
403
- * Clears used headers.
404
- */
405
- public function clearHeaders ()
406
- {
407
- $ this ->headers = array (
408
- 'Accept ' => sprintf ('application/vnd.github.%s+json ' , $ this ->getApiVersion ()),
409
- );
410
-
411
- $ this ->removePlugin (Plugin \HeaderAppendPlugin::class);
412
- $ this ->addPlugin (new Plugin \HeaderAppendPlugin ($ this ->headers ));
413
- }
414
-
415
- /**
416
- * @param array $headers
417
- */
418
- public function addHeaders (array $ headers )
419
- {
420
- $ this ->headers = array_merge ($ this ->headers , $ headers );
421
-
422
- $ this ->removePlugin (Plugin \HeaderAppendPlugin::class);
423
- $ this ->addPlugin (new Plugin \HeaderAppendPlugin ($ this ->headers ));
424
- }
425
-
426
308
/**
427
309
* Add a cache plugin to cache responses locally.
428
310
*
@@ -431,16 +313,15 @@ public function addHeaders(array $headers)
431
313
*/
432
314
public function addCache (CacheItemPoolInterface $ cachePool , array $ config = [])
433
315
{
434
- $ this ->removeCache ();
435
- $ this ->addPlugin (new Plugin \CachePlugin ($ cachePool , $ this ->streamFactory , $ config ));
316
+ $ this ->httpClientBuilder ->addCache ($ cachePool , $ config );
436
317
}
437
318
438
319
/**
439
- * Remove the cache plugin
320
+ * Remove the cache plugin.
440
321
*/
441
322
public function removeCache ()
442
323
{
443
- $ this ->removePlugin ( Plugin \CachePlugin::class );
324
+ $ this ->httpClientBuilder -> removeCache ( );
444
325
}
445
326
446
327
/**
@@ -460,7 +341,6 @@ public function __call($name, $args)
460
341
}
461
342
462
343
/**
463
- *
464
344
* @return null|\Psr\Http\Message\ResponseInterface
465
345
*/
466
346
public function getLastResponse ()
@@ -469,20 +349,10 @@ public function getLastResponse()
469
349
}
470
350
471
351
/**
472
- * Make sure to move the cache plugin to the end of the chain
352
+ * @return HttpMethodsClient
473
353
*/
474
- private function pushBackCachePlugin ()
354
+ public function getHttpClient ()
475
355
{
476
- $ cachePlugin = null ;
477
- foreach ($ this ->plugins as $ i => $ plugin ) {
478
- if ($ plugin instanceof Plugin \CachePlugin) {
479
- $ cachePlugin = $ plugin ;
480
- unset($ this ->plugins [$ i ]);
481
-
482
- $ this ->plugins [] = $ cachePlugin ;
483
-
484
- return ;
485
- }
486
- }
356
+ return $ this ->httpClientBuilder ->getHttpClient ();
487
357
}
488
358
}
0 commit comments