forked from KnpLabs/php-github-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClient.php
More file actions
450 lines (393 loc) · 12.1 KB
/
Client.php
File metadata and controls
450 lines (393 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php
namespace Github;
use Github\Api\ApiInterface;
use Github\Exception\InvalidArgumentException;
use Github\Exception\BadMethodCallException;
use Github\HttpClient\Cache\CacheInterface;
use Github\HttpClient\Plugin\Authentication;
use Github\HttpClient\Plugin\Cache;
use Github\HttpClient\Plugin\GithubExceptionThrower;
use Github\HttpClient\Plugin\History;
use Github\HttpClient\Plugin\PathPrepend;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\Plugin;
use Http\Client\Common\PluginClient;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\UriFactoryDiscovery;
use Http\Message\MessageFactory;
/**
* Simple yet very cool PHP GitHub client.
*
* @method Api\CurrentUser currentUser()
* @method Api\CurrentUser me()
* @method Api\Enterprise ent()
* @method Api\Enterprise enterprise()
* @method Api\GitData git()
* @method Api\GitData gitData()
* @method Api\Gists gist()
* @method Api\Gists gists()
* @method Api\Issue issue()
* @method Api\Issue issues()
* @method Api\Markdown markdown()
* @method Api\Notification notification()
* @method Api\Notification notifications()
* @method Api\Organization organization()
* @method Api\Organization organizations()
* @method Api\PullRequest pr()
* @method Api\PullRequest pullRequest()
* @method Api\PullRequest pullRequests()
* @method Api\RateLimit rateLimit()
* @method Api\Repo repo()
* @method Api\Repo repos()
* @method Api\Repo repository()
* @method Api\Repo repositories()
* @method Api\Search search()
* @method Api\Organization team()
* @method Api\Organization teams()
* @method Api\User user()
* @method Api\User users()
* @method Api\Authorizations authorization()
* @method Api\Authorizations authorizations()
* @method Api\Meta meta()
*
* @author Joseph Bielawski <[email protected]>
*
* Website: http://github.com/KnpLabs/php-github-api
*/
class Client
{
/**
* Constant for authentication method. Indicates the default, but deprecated
* login with username and token in URL.
*/
const AUTH_URL_TOKEN = 'url_token';
/**
* Constant for authentication method. Not indicates the new login, but allows
* usage of unauthenticated rate limited requests for given client_id + client_secret.
*/
const AUTH_URL_CLIENT_ID = 'url_client_id';
/**
* Constant for authentication method. Indicates the new favored login method
* with username and password via HTTP Authentication.
*/
const AUTH_HTTP_PASSWORD = 'http_password';
/**
* Constant for authentication method. Indicates the new login method with
* with username and token via HTTP Authentication.
*/
const AUTH_HTTP_TOKEN = 'http_token';
/**
* @var array
*/
private $options = array(
'api_version' => 'v3',
);
/**
* The object that sends HTTP messages
*
* @var HttpClient
*/
private $httpClient;
/**
* A HTTP client with all our plugins
*
* @var PluginClient
*/
private $pluginClient;
/**
* @var MessageFactory
*/
private $messageFactory;
/**
* @var Plugin[]
*/
private $plugins = [];
/**
* True if we should create a new Plugin client at next request.
* @var bool
*/
private $httpClientModified = true;
/**
* Http headers
* @var array
*/
private $headers = [];
/**
* @var History
*/
private $responseHistory;
/**
* Instantiate a new GitHub client.
*
* @param HttpClient|null $httpClient
*/
public function __construct(HttpClient $httpClient = null)
{
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
$this->messageFactory = MessageFactoryDiscovery::find();
$this->responseHistory = new History();
$this->addPlugin(new GithubExceptionThrower());
$this->addPlugin(new Plugin\HistoryPlugin($this->responseHistory));
$this->addPlugin(new Plugin\AddHostPlugin(UriFactoryDiscovery::find()->createUri('https://api.github.com/')));
$this->addPlugin(new Plugin\HeaderDefaultsPlugin(array(
'User-Agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)',
)));
}
/**
* @param string $name
*
* @throws InvalidArgumentException
*
* @return ApiInterface
*/
public function api($name)
{
switch ($name) {
case 'me':
case 'current_user':
case 'currentUser':
$api = new Api\CurrentUser($this);
break;
case 'deployment':
case 'deployments':
$api = new Api\Deployment($this);
break;
case 'ent':
case 'enterprise':
$api = new Api\Enterprise($this);
break;
case 'git':
case 'git_data':
case 'gitData':
$api = new Api\GitData($this);
break;
case 'gist':
case 'gists':
$api = new Api\Gists($this);
break;
case 'issue':
case 'issues':
$api = new Api\Issue($this);
break;
case 'markdown':
$api = new Api\Markdown($this);
break;
case 'notification':
case 'notifications':
$api = new Api\Notification($this);
break;
case 'organization':
case 'organizations':
$api = new Api\Organization($this);
break;
case 'pr':
case 'pullRequest':
case 'pull_request':
case 'pullRequests':
case 'pull_requests':
$api = new Api\PullRequest($this);
break;
case 'rateLimit':
case 'rate_limit':
$api = new Api\RateLimit($this);
break;
case 'repo':
case 'repos':
case 'repository':
case 'repositories':
$api = new Api\Repo($this);
break;
case 'search':
$api = new Api\Search($this);
break;
case 'team':
case 'teams':
$api = new Api\Organization\Teams($this);
break;
case 'user':
case 'users':
$api = new Api\User($this);
break;
case 'authorization':
case 'authorizations':
$api = new Api\Authorizations($this);
break;
case 'meta':
$api = new Api\Meta($this);
break;
default:
throw new InvalidArgumentException(sprintf('Undefined api instance called: "%s"', $name));
}
return $api;
}
/**
* Authenticate a user for all next requests.
*
* @param string $tokenOrLogin GitHub private token/username/client ID
* @param null|string $password GitHub password/secret (optionally can contain $authMethod)
* @param null|string $authMethod One of the AUTH_* class constants
*
* @throws InvalidArgumentException If no authentication method was given
*/
public function authenticate($tokenOrLogin, $password = null, $authMethod = null)
{
if (null === $password && null === $authMethod) {
throw new InvalidArgumentException('You need to specify authentication method!');
}
if (null === $authMethod && in_array($password, array(self::AUTH_URL_TOKEN, self::AUTH_URL_CLIENT_ID, self::AUTH_HTTP_PASSWORD, self::AUTH_HTTP_TOKEN))) {
$authMethod = $password;
$password = null;
}
if (null === $authMethod) {
$authMethod = self::AUTH_HTTP_PASSWORD;
}
$this->removePlugin(Authentication::class);
$this->addPlugin(new Authentication($tokenOrLogin, $password, $authMethod));
}
/**
* Sets the URL of your GitHub Enterprise instance.
*
* @param string $enterpriseUrl URL of the API in the form of http(s)://hostname
*/
public function setEnterpriseUrl($enterpriseUrl)
{
$this->removePlugin(Plugin\AddHostPlugin::class);
$this->removePlugin(PathPrepend::class);
$this->addPlugin(new Plugin\AddHostPlugin(UriFactoryDiscovery::find()->createUri($enterpriseUrl)));
$this->addPlugin(new PathPrepend(sprintf('/api/%s/', $this->getOption('api_version'))));
}
/**
* Add a new plugin to the chain
*
* @param Plugin $plugin
*/
protected function addPlugin(Plugin $plugin)
{
$this->plugins[] = $plugin;
$this->httpClientModified = true;
}
/**
* Remove a plugin by its fully qualified class name (FQCN).
*
* @param string $fqcn
*/
protected function removePlugin($fqcn)
{
foreach ($this->plugins as $idx => $plugin) {
if ($plugin instanceof $fqcn) {
unset($this->plugins[$idx]);
$this->httpClientModified = true;
}
}
}
/**
* @return HttpMethodsClient
*/
public function getHttpClient()
{
if ($this->httpClientModified) {
$this->httpClientModified = false;
$this->pluginClient = new HttpMethodsClient(
new PluginClient($this->httpClient, $this->plugins),
$this->messageFactory
);
}
return $this->pluginClient;
}
/**
* @param HttpClient $httpClient
*/
public function setHttpClient(HttpClient $httpClient)
{
$this->httpClientModified = true;
$this->httpClient = $httpClient;
}
/**
* Clears used headers.
*/
public function clearHeaders()
{
$this->headers = array(
'Accept' => sprintf('application/vnd.github.%s+json', $this->options['api_version']),
);
$this->removePlugin(Plugin\HeaderAppendPlugin::class);
$this->addPlugin(new Plugin\HeaderAppendPlugin($this->headers));
}
/**
* @param array $headers
*/
public function addHeaders(array $headers)
{
$this->headers = array_merge($this->headers, $headers);
$this->removePlugin(Plugin\HeaderAppendPlugin::class);
$this->addPlugin(new Plugin\HeaderAppendPlugin($this->headers));
}
/**
* @param bool|CacheInterface $cache
*/
public function useCache($cache = true)
{
$this->removePlugin(Cache::class);
if ($cache !== false) {
if ($cache instanceof CacheInterface) {
$plugin = new Cache($cache);
} else {
$plugin = new Cache();
}
$this->addPlugin($plugin);
}
}
/**
* @param string $name
*
* @throws InvalidArgumentException
*
* @return mixed
*/
public function getOption($name)
{
if (!array_key_exists($name, $this->options)) {
throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name));
}
return $this->options[$name];
}
/**
* @param string $name
* @param mixed $value
*
* @throws InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setOption($name, $value)
{
if (!array_key_exists($name, $this->options)) {
throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name));
}
$this->options[$name] = $value;
}
/**
* @param string $name
*
* @throws InvalidArgumentException
*
* @return ApiInterface
*/
public function __call($name, $args)
{
try {
return $this->api($name);
} catch (InvalidArgumentException $e) {
throw new BadMethodCallException(sprintf('Undefined method called: "%s"', $name));
}
}
/**
*
* @return null|\Psr\Http\Message\ResponseInterface
*/
public function getLastResponse()
{
return $this->responseHistory->getLastResponse();
}
}