forked from KnpLabs/php-github-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContents.php
More file actions
268 lines (234 loc) · 9.52 KB
/
Contents.php
File metadata and controls
268 lines (234 loc) · 9.52 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
<?php
namespace Github\Api\Repository;
use Github\Api\AbstractApi;
use Github\Exception\InvalidArgumentException;
use Github\Exception\ErrorException;
use Github\Exception\MissingArgumentException;
use Github\Exception\TwoFactorAuthenticationRequiredException;
/**
* @link http://developer.github.com/v3/repos/contents/
* @author Joseph Bielawski <[email protected]>
*/
class Contents extends AbstractApi
{
/**
* Get content of README file in a repository
* @link http://developer.github.com/v3/repos/contents/
*
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param null|string $reference reference to a branch or commit
*
* @return array information for README file
*/
public function readme($username, $repository, $reference = null)
{
return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/readme', array(
'ref' => $reference
));
}
/**
* Get contents of any file or directory in a repository
* @link http://developer.github.com/v3/repos/contents/
*
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param null|string $path path to file or directory
* @param null|string $reference reference to a branch or commit
*
* @return array information for file | information for each item in directory
*/
public function show($username, $repository, $path = null, $reference = null)
{
$url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents';
if (null !== $path) {
$url .= '/'.rawurlencode($path);
}
return $this->get($url, array(
'ref' => $reference
));
}
/**
* Creates a new file in a repository
* @link http://developer.github.com/v3/repos/contents/#create-a-file
*
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param string $path path to file
* @param string $content contents of the new file
* @param string $message the commit message
* @param null|string $branch name of a branch
* @param null|array $committer information about the committer
*
* @throws MissingArgumentException
*
* @return array information about the new file
*/
public function create($username, $repository, $path, $content, $message, $branch = null, array $committer = null)
{
$url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents/'.rawurlencode($path);
$parameters = array(
'content' => base64_encode($content),
'message' => $message,
);
if (null !== $branch) {
$parameters['branch'] = $branch;
}
if (null !== $committer) {
if (!isset($committer['name'], $committer['email'])) {
throw new MissingArgumentException(array('name', 'email'));
}
$parameters['committer'] = $committer;
}
return $this->put($url, $parameters);
}
/**
* Checks that a given path exists in a repository.
*
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param string $path path of file to check
* @param null|string $reference reference to a branch or commit
* @return boolean
*/
public function exists($username, $repository, $path, $reference = null)
{
$url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents';
if (null !== $path) {
$url .= '/'.rawurlencode($path);
}
try {
$response = $this->head($url, array(
'ref' => $reference
));
if ($response->getStatusCode() != 200) {
return false;
}
} catch (TwoFactorAuthenticationRequiredException $ex) {
throw $ex;
} catch (\Exception $ex) {
return false;
}
return true;
}
/**
* Updates the contents of a file in a repository
* @link http://developer.github.com/v3/repos/contents/#update-a-file
*
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param string $path path to file
* @param string $content contents of the new file
* @param string $message the commit message
* @param string $sha blob SHA of the file being replaced
* @param null|string $branch name of a branch
* @param null|array $committer information about the committer
*
* @throws MissingArgumentException
*
* @return array information about the updated file
*/
public function update($username, $repository, $path, $content, $message, $sha, $branch = null, array $committer = null)
{
$url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents/'.rawurlencode($path);
$parameters = array(
'content' => base64_encode($content),
'message' => $message,
'sha' => $sha,
);
if (null !== $branch) {
$parameters['branch'] = $branch;
}
if (null !== $committer) {
if (!isset($committer['name'], $committer['email'])) {
throw new MissingArgumentException(array('name', 'email'));
}
$parameters['committer'] = $committer;
}
return $this->put($url, $parameters);
}
/**
* Deletes a file from a repository
* @link http://developer.github.com/v3/repos/contents/#delete-a-file
*
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param string $path path to file
* @param string $message the commit message
* @param string $sha blob SHA of the file being deleted
* @param null|string $branch name of a branch
* @param null|array $committer information about the committer
*
* @throws MissingArgumentException
*
* @return array information about the updated file
*/
public function rm($username, $repository, $path, $message, $sha, $branch = null, array $committer = null)
{
$url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents/'.rawurlencode($path);
$parameters = array(
'message' => $message,
'sha' => $sha,
);
if (null !== $branch) {
$parameters['branch'] = $branch;
}
if (null !== $committer) {
if (!isset($committer['name'], $committer['email'])) {
throw new MissingArgumentException(array('name', 'email'));
}
$parameters['committer'] = $committer;
}
return $this->delete($url, $parameters);
}
/**
* Get content of archives in a repository
* @link http://developer.github.com/v3/repos/contents/
*
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param string $format format of archive: tarball or zipball
* @param null|string $reference reference to a branch or commit
*
* @return array information for archives
*/
public function archive($username, $repository, $format, $reference = null)
{
if (!in_array($format, array('tarball', 'zipball'))) {
$format = 'tarball';
}
return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/'.rawurlencode($format), array(
'ref' => $reference
));
}
/**
* Get the contents of a file in a repository
*
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param string $path path to file
* @param null|string $reference reference to a branch or commit
*
* @return null|string content of file, or null in case of base64_decode failure
*
* @throws InvalidArgumentException If $path is not a file or if its encoding is different from base64
* @throws ErrorException If $path doesn't include a 'content' index
*/
public function download($username, $repository, $path, $reference = null)
{
$file = $this->show($username, $repository, $path, $reference);
if (!isset($file['type']) || 'file' !== $file['type']) {
throw new InvalidArgumentException(sprintf('Path "%s" is not a file.', $path));
}
if (!isset($file['content'])) {
throw new ErrorException(sprintf('Unable to access "content" for file "%s" (possible keys: "%s").', $path, implode(', ', array_keys($file))));
}
if (!isset($file['encoding'])) {
throw new InvalidArgumentException(sprintf('Can\'t decode content of file "%s", as no encoding is defined.', $path));
}
if ('base64' !== $file['encoding']) {
throw new InvalidArgumentException(sprintf('Encoding "%s" of file "%s" is not supported.', $file['encoding'], $path));
}
return base64_decode($file['content']) ?: null;
}
}