|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Acacha\Llum\Github; |
|
4
|
|
|
|
|
5
|
|
|
use Acacha\Llum\Filesystem\Filesystem; |
|
6
|
|
|
use Acacha\Llum\Github\Exceptions\CredentialsException; |
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class GithubAPI |
|
11
|
|
|
* @package Acacha\Llum\Github |
|
12
|
|
|
*/ |
|
13
|
|
|
class GithubAPI |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Guzzle http client. |
|
17
|
|
|
* |
|
18
|
|
|
* @var Client |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $client; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Github API URL. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $api_url = "https://api.github.com"; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Authorization URI in github API. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $authorizations_uri = "/authorizations"; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Authorization URI in github API. |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $repos_uri = "/user/repos"; |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Acacha Llum Filesystem. |
|
46
|
|
|
* |
|
47
|
|
|
* @var Filesystem |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $filesystem; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Token name; |
|
53
|
|
|
* |
|
54
|
|
|
* @var |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $tokenName; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* GithubAPI constructor. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct(Filesystem $filesystem) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->client = new Client(); |
|
64
|
|
|
$this->filesystem = $filesystem; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Path to repo.json stub |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function repo_json_stub() { |
|
73
|
|
|
return __DIR__ . '/stubs/repo_json.stub'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Authorization URL. |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function authorization_url() { |
|
82
|
|
|
return $this->api_url . $this->authorizations_uri; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Create repo URL. |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function create_repo_url() { |
|
91
|
|
|
return $this->api_url . $this->repos_uri; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Obtain personal token. |
|
96
|
|
|
* |
|
97
|
|
|
* @param $username |
|
98
|
|
|
* @param $password |
|
99
|
|
|
* @return mixed |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getPersonalToken($username, $password) |
|
102
|
|
|
{ |
|
103
|
|
|
$response = $this->client->request('POST', $this->authorization_url(), |
|
104
|
|
|
[ |
|
105
|
|
|
"auth" => [ $username, $password], |
|
106
|
|
|
"json" => $this->authorizationsRequestJson() |
|
107
|
|
|
] |
|
108
|
|
|
); |
|
109
|
|
|
$response = json_decode($response->getBody()); |
|
110
|
|
|
$this->tokenName = $response->app->name; |
|
111
|
|
|
return $response->token; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function authorizationsRequestJson(){ |
|
118
|
|
|
return [ |
|
119
|
|
|
'scopes' => [ 'public_repo' ], |
|
120
|
|
|
'note' => uniqid('llum_') |
|
121
|
|
|
]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Create repo in github. |
|
126
|
|
|
* |
|
127
|
|
|
* @param $repo_name |
|
128
|
|
|
* @param $repo_description |
|
129
|
|
|
* @return mixed |
|
130
|
|
|
*/ |
|
131
|
|
|
public function createRepo($repo_name, $repo_description) |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->client->request('POST', $this->create_repo_url(), |
|
134
|
|
|
[ |
|
135
|
|
|
"auth" => $this->credentials(), |
|
136
|
|
|
"json" => json_decode($this->compileStub($repo_name, $repo_description),true), |
|
137
|
|
|
] |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Set github credentials. |
|
143
|
|
|
* |
|
144
|
|
|
* @param array $credentials |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setCredentials(array $credentials) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->credentials = $credentials; |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get github credentials. |
|
153
|
|
|
* |
|
154
|
|
|
* @return mixed |
|
155
|
|
|
* @throws CredentialsException |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function credentials() |
|
158
|
|
|
{ |
|
159
|
|
|
if (isset($this->credentials)) |
|
160
|
|
|
return $this->credentials; |
|
161
|
|
|
throw new CredentialsException; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Compile stub. |
|
166
|
|
|
* |
|
167
|
|
|
* @param $repo_name |
|
168
|
|
|
* @param $repo_description |
|
169
|
|
|
* @return mixed |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function compileStub($repo_name, $repo_description) |
|
172
|
|
|
{ |
|
173
|
|
|
$data = [ |
|
174
|
|
|
"REPO_NAME" => $repo_name, |
|
175
|
|
|
"REPO_DESCRIPTION" => $repo_description |
|
176
|
|
|
]; |
|
177
|
|
|
return $this->compile( |
|
178
|
|
|
$this->filesystem->get($this->repo_json_stub()) , |
|
179
|
|
|
$data); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Compile the template using the given data. |
|
184
|
|
|
* |
|
185
|
|
|
* @param $template |
|
186
|
|
|
* @param $data |
|
187
|
|
|
* @return mixed |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function compile($template, $data) |
|
190
|
|
|
{ |
|
191
|
|
|
foreach($data as $key => $value) |
|
192
|
|
|
{ |
|
193
|
|
|
$template = preg_replace("/\\$$key\\$/i", $value, $template); |
|
194
|
|
|
} |
|
195
|
|
|
return $template; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @return mixed |
|
200
|
|
|
*/ |
|
201
|
|
|
public function tokenName() |
|
202
|
|
|
{ |
|
203
|
|
|
return $this->tokenName; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: