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
15
use Http \Client \HttpClient ;
16
- use Http \Discovery \HttpClientDiscovery ;
17
- use Http \Discovery \MessageFactoryDiscovery ;
18
- use Http \Discovery \StreamFactoryDiscovery ;
19
16
use Http \Discovery \UriFactoryDiscovery ;
20
- use Http \Message \MessageFactory ;
21
- use Nyholm \Psr7 \Factory \StreamFactory ;
22
17
use Psr \Cache \CacheItemPoolInterface ;
23
18
24
19
/**
@@ -98,45 +93,9 @@ class Client
98
93
private $ apiVersion ;
99
94
100
95
/**
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
96
+ * @var Builder
121
97
*/
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
132
- */
133
- private $ httpClientModified = true ;
134
-
135
- /**
136
- * Http headers
137
- * @var array
138
- */
139
- private $ headers = [];
98
+ private $ httpClientBuilder ;
140
99
141
100
/**
142
101
* @var History
@@ -146,33 +105,46 @@ class Client
146
105
/**
147
106
* Instantiate a new GitHub client.
148
107
*
149
- * @param HttpClient |null $httpClient
150
- * @param string|null $apiVersion
151
- * @param string|null $enterpriseUrl
108
+ * @param Builder |null $httpClientBuilder
109
+ * @param string|null $apiVersion
110
+ * @param string|null $enterpriseUrl
152
111
*/
153
- public function __construct (HttpClient $ httpClient = null , $ apiVersion = null , $ enterpriseUrl = null )
112
+ public function __construct (Builder $ httpClientBuilder = null , $ apiVersion = null , $ enterpriseUrl = null )
154
113
{
155
- $ this ->httpClient = $ httpClient ?: HttpClientDiscovery::find ();
156
- $ this ->messageFactory = MessageFactoryDiscovery::find ();
157
- $ this ->streamFactory = StreamFactoryDiscovery::find ();
158
-
159
114
$ 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) ' ,
115
+ $ this ->httpClientBuilder = $ builder = $ httpClientBuilder ?: new Builder ();
116
+
117
+ $ builder ->addPlugin (new GithubExceptionThrower ());
118
+ $ builder ->addPlugin (new Plugin \HistoryPlugin ($ this ->responseHistory ));
119
+ $ builder ->addPlugin (new Plugin \RedirectPlugin ());
120
+ $ builder ->addPlugin (new Plugin \AddHostPlugin (UriFactoryDiscovery::find ()->createUri ('https://api.github.com ' )));
121
+ $ builder ->addPlugin (new Plugin \HeaderDefaultsPlugin (array (
122
+ 'User-Agent ' => 'php-github-api (http://github.com/KnpLabs/php-github-api) ' ,
123
+ 'Accept ' => sprintf ('application/vnd.github.%s+json ' , $ this ->getApiVersion ()),
166
124
)));
167
125
168
126
$ this ->apiVersion = $ apiVersion ?: 'v3 ' ;
169
- $ this ->addHeaders (['Accept ' => sprintf ('application/vnd.github.%s+json ' , $ this ->apiVersion )]);
127
+ $ builder ->addHeaders (['Accept ' => sprintf ('application/vnd.github.%s+json ' , $ this ->apiVersion )]);
170
128
171
129
if ($ enterpriseUrl ) {
172
130
$ this ->setEnterpriseUrl ($ enterpriseUrl );
173
131
}
174
132
}
175
133
134
+ /**
135
+ * Create a Github\Client using a HttpClient.
136
+ *
137
+ * @param HttpClient $httpClient
138
+ *
139
+ * @return Client
140
+ */
141
+ public static function createWithHttpClient (HttpClient $ httpClient )
142
+ {
143
+ $ builder = new Builder ($ httpClient );
144
+
145
+ return new self ($ builder );
146
+ }
147
+
176
148
/**
177
149
* @param string $name
178
150
*
@@ -313,15 +285,15 @@ public function authenticate($tokenOrLogin, $password = null, $authMethod = null
313
285
314
286
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
287
$ authMethod = $ password ;
316
- $ password = null ;
288
+ $ password = null ;
317
289
}
318
290
319
291
if (null === $ authMethod ) {
320
292
$ authMethod = self ::AUTH_HTTP_PASSWORD ;
321
293
}
322
294
323
- $ this ->removePlugin (Authentication::class);
324
- $ this ->addPlugin (new Authentication ($ tokenOrLogin , $ password , $ authMethod ));
295
+ $ this ->getHttpClientBuilder ()-> removePlugin (Authentication::class);
296
+ $ this ->getHttpClientBuilder ()-> addPlugin (new Authentication ($ tokenOrLogin , $ password , $ authMethod ));
325
297
}
326
298
327
299
/**
@@ -331,64 +303,12 @@ public function authenticate($tokenOrLogin, $password = null, $authMethod = null
331
303
*/
332
304
private function setEnterpriseUrl ($ enterpriseUrl )
333
305
{
334
- $ this ->removePlugin (Plugin \AddHostPlugin::class);
335
- $ this ->removePlugin (PathPrepend::class);
306
+ $ builder = $ this ->getHttpClientBuilder ();
307
+ $ builder ->removePlugin (Plugin \AddHostPlugin::class);
308
+ $ builder ->removePlugin (PathPrepend::class);
336
309
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
- }
366
-
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 ;
310
+ $ builder ->addPlugin (new Plugin \AddHostPlugin (UriFactoryDiscovery::find ()->createUri ($ enterpriseUrl )));
311
+ $ builder ->addPlugin (new PathPrepend (sprintf ('/api/%s/ ' , $ this ->getApiVersion ())));
392
312
}
393
313
394
314
/**
@@ -399,30 +319,6 @@ public function getApiVersion()
399
319
return $ this ->apiVersion ;
400
320
}
401
321
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
322
/**
427
323
* Add a cache plugin to cache responses locally.
428
324
*
@@ -431,16 +327,15 @@ public function addHeaders(array $headers)
431
327
*/
432
328
public function addCache (CacheItemPoolInterface $ cachePool , array $ config = [])
433
329
{
434
- $ this ->removeCache ();
435
- $ this ->addPlugin (new Plugin \CachePlugin ($ cachePool , $ this ->streamFactory , $ config ));
330
+ $ this ->getHttpClientBuilder ()->addCache ($ cachePool , $ config );
436
331
}
437
332
438
333
/**
439
- * Remove the cache plugin
334
+ * Remove the cache plugin.
440
335
*/
441
336
public function removeCache ()
442
337
{
443
- $ this ->removePlugin ( Plugin \CachePlugin::class );
338
+ $ this ->getHttpClientBuilder ()-> removeCache ( );
444
339
}
445
340
446
341
/**
@@ -460,7 +355,6 @@ public function __call($name, $args)
460
355
}
461
356
462
357
/**
463
- *
464
358
* @return null|\Psr\Http\Message\ResponseInterface
465
359
*/
466
360
public function getLastResponse ()
@@ -469,20 +363,18 @@ public function getLastResponse()
469
363
}
470
364
471
365
/**
472
- * Make sure to move the cache plugin to the end of the chain
366
+ * @return HttpMethodsClient
473
367
*/
474
- private function pushBackCachePlugin ()
368
+ public function getHttpClient ()
475
369
{
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 ;
370
+ return $ this ->getHttpClientBuilder ()->getHttpClient ();
371
+ }
483
372
484
- return ;
485
- }
486
- }
373
+ /**
374
+ * @return Builder
375
+ */
376
+ protected function getHttpClientBuilder ()
377
+ {
378
+ return $ this ->httpClientBuilder ;
487
379
}
488
380
}
0 commit comments