This repository was archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathClient.php
266 lines (236 loc) · 6.75 KB
/
Client.php
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
<?php
/**
* Simple yet very cool PHP Github client
*
* @tutorial http://github.com/ornicar/php-github-api/blob/master/README.markdown
* @version 3.2
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
* @license MIT License
*
* Website: http://github.com/ornicar/php-github-api
* Tickets: http://github.com/ornicar/php-github-api/issues
*/
class Github_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. 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';
/**
* The httpClient instance used to communicate with GitHub
*
* @var Github_HttpClient_Interface
*/
protected $httpClient = null;
/**
* The list of loaded API instances
*
* @var array
*/
protected $apis = array();
/**
* Instanciate a new GitHub client
*
* @param Github_HttpClient_Interface $httpClient custom http client
*/
public function __construct(Github_HttpClientInterface $httpClient = null)
{
if (null === $httpClient) {
$this->httpClient = new Github_HttpClient_Curl();
} else {
$this->httpClient = $httpClient;
}
}
/**
* Authenticate a user for all next requests
*
* @param string $login GitHub username
* @param string $secret GitHub private token or Github password if $method == AUTH_HTTP_PASSWORD
* @param string $method One of the AUTH_* class constants
*
* @return null
*/
public function authenticate($login, $secret, $method = NULL)
{
if (!$method) {
$method = self::AUTH_URL_TOKEN;
}
$this->getHttpClient()
->setOption('auth_method', $method)
->setOption('login', $login)
->setOption('secret', $secret);
}
/**
* Deauthenticate a user for all next requests
*
* @return null
*/
public function deAuthenticate()
{
$this->authenticate(null, null, null);
}
/**
* Call any path, GET method
* Ex: $api->get('repos/show/my-username/my-repo')
*
* @param string $path the GitHub path
* @param array $parameters GET parameters
* @param array $requestOptions reconfigure the request
* @return array data returned
*/
public function get($path, array $parameters = array(), $requestOptions = array())
{
return $this->getHttpClient()->get($path, $parameters, $requestOptions);
}
/**
* Call any path, POST method
* Ex: $api->post('repos/show/my-username', array('email' => '[email protected]'))
*
* @param string $path the GitHub path
* @param array $parameters POST parameters
* @param array $requestOptions reconfigure the request
* @return array data returned
*/
public function post($path, array $parameters = array(), $requestOptions = array())
{
return $this->getHttpClient()->post($path, $parameters, $requestOptions);
}
/**
* Get the http client.
*
* @return Github_HttpClient_Interface a request instance
*/
public function getHttpClient()
{
return $this->httpClient;
}
/**
* Inject another http client
*
* @param Github_HttpClient_Interface a httpClient instance
*
* @return null
*/
public function setHttpClient(Github_HttpClient_Interface $httpClient)
{
$this->httpClient = $httpClient;
}
/**
* Get the user API
*
* @return Github_Api_User the user API
*/
public function getUserApi()
{
if (!isset($this->apis['user'])) {
$this->apis['user'] = new Github_Api_User($this);
}
return $this->apis['user'];
}
/**
* Get the issue API
*
* @return Github_Api_Issue the issue API
*/
public function getIssueApi()
{
if (!isset($this->apis['issue'])) {
$this->apis['issue'] = new Github_Api_Issue($this);
}
return $this->apis['issue'];
}
/**
* Get the commit API
*
* @return Github_Api_Commit the commit API
*/
public function getCommitApi()
{
if (!isset($this->apis['commit'])) {
$this->apis['commit'] = new Github_Api_Commit($this);
}
return $this->apis['commit'];
}
/**
* Get the repo API
*
* @return Github_Api_Repo the repo API
*/
public function getRepoApi()
{
if (!isset($this->apis['repo'])) {
$this->apis['repo'] = new Github_Api_Repo($this);
}
return $this->apis['repo'];
}
/**
* Get the organization API
*
* @return Github_Api_Organization the object API
*/
public function getOrganizationApi()
{
if (!isset($this->apis['organization'])) {
$this->apis['organization'] = new Github_Api_Organization($this);
}
return $this->apis['organization'];
}
/**
* Get the object API
*
* @return Github_Api_Object the object API
*/
public function getObjectApi()
{
if (!isset($this->apis['object'])) {
$this->apis['object'] = new Github_Api_Object($this);
}
return $this->apis['object'];
}
/**
* Get the pull request API
*
* @return Github_Api_PullRequest the pull request API
*/
public function getPullRequestApi()
{
if (!isset($this->apis['pullrequest'])) {
$this->apis['pullrequest'] = new Github_Api_PullRequest($this);
}
return $this->apis['pullrequest'];
}
/**
* Inject an API instance
*
* @param string $name the API name
* @param Github_ApiInterface $api the API instance
*
* @return Github_ApiInterface the API instance
*/
public function setApi($name, Github_ApiInterface $instance)
{
$this->apis[$name] = $instance;
return $this;
}
/**
* Get any API
*
* @param string $name the API name
* @return Github_ApiInterface the API instance
*/
public function getApi($name)
{
return $this->apis[$name];
}
}