forked from Potherca/flysystem-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGithubAdapter.php
More file actions
279 lines (249 loc) · 5.97 KB
/
GithubAdapter.php
File metadata and controls
279 lines (249 loc) · 5.97 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
<?php
namespace Potherca\Flysystem\Github;
use League\Flysystem\Adapter\AbstractAdapter;
use League\Flysystem\Adapter\Polyfill\StreamedTrait;
use League\Flysystem\Config;
use League\Flysystem\Exception;
use League\Flysystem\Util;
/**
*
*/
class GithubAdapter extends AbstractAdapter
{
use StreamedTrait;
const COMMITTER_MAIL = 'email';
const COMMITTER_NAME = 'name';
const VISIBILITY_PRIVATE = 'private';
const VISIBILITY_PUBLIC = 'public';
/** @var ApiInterface */
private $api;
/**
* @return ApiInterface
*/
final public function getApi()
{
return $this->api;
}
/**
* @param ApiInterface $api
*/
public function __construct(ApiInterface $api)
{
$this->api = $api;
}
/**
* Write a new file.
*
* @param string $path
* @param string $contents
* @param Config $config Config object
*
* @return array|false false on failure file meta data on success
*/
public function write($path, $contents, Config $config)
{
throw new Exception('Write action are not (yet) supported');
//@TODO: return $this->getApi()->create($path, $contents);
}
/**
* Update a file.
*
* @param string $path
* @param string $contents
* @param Config $config Config object
*
* @return array|false false on failure file meta data on success
*/
public function update($path, $contents, Config $config)
{
throw new Exception('Write action are not (yet) supported');
// @TODO: return $this->getApi()->update($path, $contents);
}
/**
* Rename a file.
*
* @param string $path
* @param string $newpath
*
* @return bool
*/
public function rename($path, $newpath)
{
throw new Exception('Write action are not (yet) supported');
// @TODO: return $this->getApi()->rename($path, $newPath);
}
/**
* Copy a file.
*
* @param string $path
* @param string $newpath
*
* @return bool
*/
public function copy($path, $newpath)
{
throw new Exception('Write action are not (yet) supported');
// @TODO: return $this->getApi()->copy($path, $newPath);
}
/**
* Delete a file.
*
* @param string $path
*
* @return bool
*/
public function delete($path)
{
throw new Exception('Write action are not (yet) supported');
// @TODO: return $this->getApi()->delete($path);
}
/**
* Delete a directory.
*
* @param string $dirname
*
* @return bool
*/
public function deleteDir($dirname)
{
throw new Exception('Write action are not (yet) supported');
// @TODO: return $this->getApi()->deleteDir($dirname);
}
/**
* Create a directory.
*
* @param string $dirname directory name
* @param Config $config
*
* @return array|false
*/
public function createDir($dirname, Config $config)
{
throw new Exception('Write action are not (yet) supported');
// @TODO: return $this->getApi()->createDir($dirname);
}
/**
* Set the visibility for a file.
*
* @param string $path
* @param string $visibility
*
* @return array|false file meta data
*/
public function setVisibility($path, $visibility)
{
// TODO: Implement setVisibility() method.
}
/**
* Check that a file or directory exists in the repository
*
* @param string $path
*
* @return array|bool|null
*/
public function has($path)
{
return $this->getApi()->exists($path);
}
/**
* Read a file
*
* @param string $path
*
* @return array|false
*
* @throws \Github\Exception\ErrorException
*/
public function read($path)
{
return [ApiInterface::KEY_CONTENTS => $this->getApi()->getFileContents($path)];
}
/**
* List contents of a directory.
*
* @param string $path
* @param bool $recursive
*
* @return array
*/
public function listContents($path = '/', $recursive = false)
{
$contents = $this->getApi()->getTreeMetadata($path, $recursive);
if ($this->isDirectoryContents($contents) === false) {
$contents = [];
}
return $contents;
}
/**
* Get all the meta data of a file or directory.
*
* @param string $path
*
* @return array|false
*/
public function getMetadata($path)
{
return $this->getApi()->getMetaData($path);
}
/**
* Get all the meta data of a file or directory.
*
* @param string $path
*
* @return array|false
*/
public function getSize($path)
{
return $this->getApi()->getMetaData($path);
}
/**
* Get the mimetype of a file.
*
* @param string $path
*
* @return array|false
*/
public function getMimetype($path)
{
return ['mimetype' => $this->getApi()->guessMimeType($path)];
}
/**
* Get the timestamp of a file.
*
* @param string $path
*
* @return array|false
*/
public function getTimestamp($path)
{
return $this->getApi()->getLastUpdatedTimestamp($path);
}
/**
* Get the visibility of a file.
*
* @param string $path
*
* @return array|false
*/
public function getVisibility($path)
{
$recursive = false;
$metadata = $this->getApi()->getTreeMetadata($path, $recursive);
return $metadata[0];
}
/**
* @param $contents
* @return bool
*/
private function isDirectoryContents($contents)
{
$isDirectory = false;
if (is_array($contents)) {
$isDirectory = array_key_exists(Api::KEY_TYPE, $contents) === false
|| $contents[Api::KEY_TYPE] === Api::KEY_DIRECTORY
;
}
return $isDirectory;
}
}
/*EOF*/