Skip to content

Commit d33c412

Browse files
committed
Refactor Github_Client
1 parent e5dba3f commit d33c412

1 file changed

Lines changed: 48 additions & 64 deletions

File tree

‎lib/Github/Client.php‎

Lines changed: 48 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?php
22

33
/**
4-
* Simple PHP GitHub API
4+
* Simple PHP Github client
55
*
66
* @tutorial http://github.com/ornicar/php-github-api/blob/master/README.markdown
7-
* @version 2.12 2010-10-17
7+
* @version 3.0.0
88
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
99
* @license MIT License
1010
*
1111
* Website: http://github.com/ornicar/php-github-api
1212
* Tickets: http://github.com/ornicar/php-github-api/issues
1313
*/
14-
class phpGitHubApi
14+
class Github_Client
1515
{
1616
/**
1717
* Constant for authentication method. Indicates the default, but deprecated
@@ -32,29 +32,27 @@ class phpGitHubApi
3232
const AUTH_HTTP_TOKEN = 'http_token';
3333

3434
/**
35-
* The request instance used to communicate with GitHub
36-
* @var phpGitHubApiRequest
35+
* The httpClient instance used to communicate with GitHub
36+
*
37+
* @var Github_HttpClient_Interface
3738
*/
38-
protected $request = null;
39+
protected $httpClient = null;
40+
3941
/**
4042
* The list of loaded API instances
43+
*
4144
* @var array
4245
*/
4346
protected $apis = array();
44-
/**
45-
* Use debug mode (prints debug messages)
46-
* @var bool
47-
*/
48-
protected $debug;
4947

5048
/**
51-
* Instanciate a new GitHub API
49+
* Instanciate a new GitHub client
5250
*
53-
* @param bool $debug print debug messages
51+
* @param Github_HttpClient_Interface $httpClient custom http client
5452
*/
55-
public function __construct($debug = false)
53+
public function __construct($httpClient = null)
5654
{
57-
$this->debug = $debug;
55+
$this->httpClient = $httpClient ?: new Github_HttpClient_Curl();
5856
}
5957

6058
/**
@@ -64,29 +62,28 @@ public function __construct($debug = false)
6462
* @param string $secret GitHub private token or Github password if $method == AUTH_HTTP_PASSWORD
6563
* @param string $method One of the AUTH_* class constants
6664
*
67-
* @return phpGitHubApi fluent interface
65+
* @return null
6866
*/
6967
public function authenticate($login, $secret, $method = NULL)
7068
{
7169
if (!$method) {
7270
$method = self::AUTH_URL_TOKEN;
7371
}
74-
$this->getRequest()
72+
73+
$this->getHttpClient()
7574
->setOption('auth_method', $method)
7675
->setOption('login', $login)
7776
->setOption('secret', $secret);
78-
79-
return $this;
8077
}
8178

8279
/**
8380
* Deauthenticate a user for all next requests
8481
*
85-
* @return phpGitHubApi fluent interface
82+
* @return null
8683
*/
8784
public function deAuthenticate()
8885
{
89-
return $this->authenticate(null, null, null);
86+
$this->authenticate(null, null, null);
9087
}
9188

9289
/**
@@ -100,7 +97,7 @@ public function deAuthenticate()
10097
*/
10198
public function get($route, array $parameters = array(), $requestOptions = array())
10299
{
103-
return $this->getRequest()->get($route, $parameters, $requestOptions);
100+
return $this->getHttpClient()->get($route, $parameters, $requestOptions);
104101
}
105102

106103
/**
@@ -114,49 +111,40 @@ public function get($route, array $parameters = array(), $requestOptions = array
114111
*/
115112
public function post($route, array $parameters = array(), $requestOptions = array())
116113
{
117-
return $this->getRequest()->post($route, $parameters, $requestOptions);
114+
return $this->getHttpClient()->post($route, $parameters, $requestOptions);
118115
}
119116

120117
/**
121-
* Get the request
118+
* Get the http client. If none is available, instanciate a curl client.
122119
*
123-
* @return phpGitHubApiRequest a request instance
120+
* @return Github_HttpClient_Interface a request instance
124121
*/
125-
public function getRequest()
122+
public function getHttpClient()
126123
{
127-
if (!isset($this->request)) {
128-
require_once(dirname(__FILE__).'/request/phpGitHubApiRequest.php');
129-
$this->request = new phpGitHubApiRequest(array(
130-
'debug' => $this->debug
131-
));
132-
}
133-
134-
return $this->request;
124+
return $this->httpClient;
135125
}
136126

137127
/**
138-
* Inject another request
128+
* Inject another http client
129+
*
130+
* @param Github_HttpClient_Interface a httpClient instance
139131
*
140-
* @param phpGitHubApiRequest a request instance
141-
* @return phpGitHubApi fluent interface
132+
* @return null
142133
*/
143-
public function setRequest(phpGitHubApiRequest $request)
134+
public function setHttpClient(Github_HttpClient_Interface $httpClient)
144135
{
145-
$this->request = $request;
146-
147-
return $this;
136+
$this->httpClient = $httpClient;
148137
}
149138

150139
/**
151140
* Get the user API
152141
*
153-
* @return phpGitHubApiUser the user API
142+
* @return Github_Api_User the user API
154143
*/
155144
public function getUserApi()
156145
{
157146
if (!isset($this->apis['user'])) {
158-
require_once(dirname(__FILE__).'/api/phpGitHubApiUser.php');
159-
$this->apis['user'] = new phpGitHubApiUser($this);
147+
$this->apis['user'] = new Github_Api_User($this);
160148
}
161149

162150
return $this->apis['user'];
@@ -165,13 +153,12 @@ public function getUserApi()
165153
/**
166154
* Get the issue API
167155
*
168-
* @return phpGitHubApiIssue the issue API
156+
* @return Github_Api_Issue the issue API
169157
*/
170158
public function getIssueApi()
171159
{
172160
if (!isset($this->apis['issue'])) {
173-
require_once(dirname(__FILE__).'/api/phpGitHubApiIssue.php');
174-
$this->apis['issue'] = new phpGitHubApiIssue($this);
161+
$this->apis['issue'] = new Github_Api_Issue($this);
175162
}
176163

177164
return $this->apis['issue'];
@@ -180,13 +167,12 @@ public function getIssueApi()
180167
/**
181168
* Get the commit API
182169
*
183-
* @return phpGitHubApiCommit the commit API
170+
* @return Github_Api_Commit the commit API
184171
*/
185172
public function getCommitApi()
186173
{
187174
if (!isset($this->apis['commit'])) {
188-
require_once(dirname(__FILE__).'/api/phpGitHubApiCommit.php');
189-
$this->apis['commit'] = new phpGitHubApiCommit($this);
175+
$this->apis['commit'] = new Github_Api_Commit($this);
190176
}
191177

192178
return $this->apis['commit'];
@@ -195,13 +181,12 @@ public function getCommitApi()
195181
/**
196182
* Get the repo API
197183
*
198-
* @return phpGitHubRepoCommit the repo API
184+
* @return Github_Api_Repo the repo API
199185
*/
200186
public function getRepoApi()
201187
{
202188
if (!isset($this->apis['repo'])) {
203-
require_once(dirname(__FILE__).'/api/phpGitHubApiRepo.php');
204-
$this->apis['repo'] = new phpGitHubApiRepo($this);
189+
$this->apis['repo'] = new Github_Api_Repo($this);
205190
}
206191

207192
return $this->apis['repo'];
@@ -210,13 +195,12 @@ public function getRepoApi()
210195
/**
211196
* Get the organization API
212197
*
213-
* @return phpGitHubApiOrganization the object API
198+
* @return Github_Api_Organization the object API
214199
*/
215200
public function getOrganizationApi()
216201
{
217202
if (!isset($this->apis['organization'])) {
218-
require_once(dirname(__FILE__).'/api/phpGitHubApiOrganization.php');
219-
$this->apis['organization'] = new phpGitHubApiOrganization($this);
203+
$this->apis['organization'] = new Github_Api_Organization($this);
220204
}
221205

222206
return $this->apis['organization'];
@@ -225,26 +209,26 @@ public function getOrganizationApi()
225209
/**
226210
* Get the object API
227211
*
228-
* @return phpGitHubApiObject the object API
212+
* @return Github_Api_Object the object API
229213
*/
230214
public function getObjectApi()
231215
{
232216
if (!isset($this->apis['object'])) {
233-
require_once(dirname(__FILE__).'/api/phpGitHubApiObject.php');
234-
$this->apis['object'] = new phpGitHubApiObject($this);
217+
$this->apis['object'] = new Github_Api_Object($this);
235218
}
236219

237220
return $this->apis['object'];
238221
}
239222

240223
/**
241-
* Inject another API instance
224+
* Inject an API instance
242225
*
243226
* @param string $name the API name
244-
* @param phpGitHubApiAbstract $api the API instance
245-
* @return phpGitHubApi fluent interface
227+
* @param Github_Api_Interface $api the API instance
228+
*
229+
* @return null
246230
*/
247-
public function setApi($name, phpGitHubApiAbstract $instance)
231+
public function setApi($name, Github_Api_Interface $instance)
248232
{
249233
$this->apis[$name] = $instance;
250234

@@ -255,7 +239,7 @@ public function setApi($name, phpGitHubApiAbstract $instance)
255239
* Get any API
256240
*
257241
* @param string $name the API name
258-
* @return phpGitHubApiAbstract the API instance
242+
* @return Github_Api_Interface the API instance
259243
*/
260244
public function getApi($name)
261245
{

0 commit comments

Comments
 (0)