1 | <?php |
||
31 | class NodeObject implements TreeishInterface |
||
32 | { |
||
33 | const TYPE_BLOB = 'blob'; |
||
34 | const TYPE_TREE = 'tree'; |
||
35 | const TYPE_LINK = 'commit'; |
||
36 | |||
37 | /** |
||
38 | * @var \GitElephant\Repository |
||
39 | */ |
||
40 | protected $repository; |
||
41 | |||
42 | /** |
||
43 | * permissions |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | private $permissions; |
||
48 | |||
49 | /** |
||
50 | * type |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | private $type; |
||
55 | |||
56 | /** |
||
57 | * sha |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | private $sha; |
||
62 | |||
63 | /** |
||
64 | * size |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | private $size; |
||
69 | |||
70 | /** |
||
71 | * name |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | private $name; |
||
76 | |||
77 | /** |
||
78 | * path |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | private $path; |
||
83 | |||
84 | /** |
||
85 | * create a Object from a single outputLine of the git ls-tree command |
||
86 | * |
||
87 | * @param \GitElephant\Repository $repository repository instance |
||
88 | * @param string $outputLine output from ls-tree command |
||
89 | * |
||
90 | * @see LsTreeCommand::tree |
||
91 | * @return NodeObject |
||
92 | */ |
||
93 | 8 | public static function createFromOutputLine(Repository $repository, string $outputLine) |
|
94 | { |
||
95 | 8 | $slices = static::getLineSlices($outputLine); |
|
96 | 8 | $fullPath = $slices['fullPath']; |
|
97 | 8 | if (false === $pos = mb_strrpos($fullPath, '/')) { |
|
98 | // repository root |
||
99 | 6 | $path = ''; |
|
100 | 6 | $name = $fullPath; |
|
101 | } else { |
||
102 | 4 | $path = substr($fullPath, 0, $pos); |
|
103 | 4 | $name = substr($fullPath, $pos + 1); |
|
104 | } |
||
105 | |||
106 | 8 | return new static( |
|
107 | 8 | $repository, |
|
108 | 8 | $slices['permissions'], |
|
109 | 8 | $slices['type'], |
|
110 | 8 | $slices['sha'], |
|
111 | 8 | $slices['size'], |
|
112 | $name, |
||
113 | $path |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Take a line and turn it in slices |
||
119 | * |
||
120 | * @param string $line a single line output from the git binary |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | 14 | public static function getLineSlices(string $line) |
|
125 | { |
||
126 | 14 | preg_match('/^(\d+) (\w+) ([a-z0-9]+) +(\d+|-)\t(.*)$/', $line, $matches); |
|
127 | 14 | $permissions = $matches[1]; |
|
128 | 14 | $type = null; |
|
129 | 14 | switch ($matches[2]) { |
|
130 | case NodeObject::TYPE_TREE: |
||
131 | 5 | $type = NodeObject::TYPE_TREE; |
|
132 | 5 | break; |
|
133 | case NodeObject::TYPE_BLOB: |
||
134 | 14 | $type = NodeObject::TYPE_BLOB; |
|
135 | 14 | break; |
|
136 | case NodeObject::TYPE_LINK: |
||
137 | 1 | $type = NodeObject::TYPE_LINK; |
|
138 | 1 | break; |
|
139 | } |
||
140 | 14 | $sha = $matches[3]; |
|
141 | 14 | $size = $matches[4]; |
|
142 | 14 | $fullPath = $matches[5]; |
|
143 | |||
144 | return [ |
||
145 | 14 | 'permissions' => $permissions, |
|
146 | 14 | 'type' => $type, |
|
147 | 14 | 'sha' => $sha, |
|
148 | 14 | 'size' => $size, |
|
149 | 14 | 'fullPath' => $fullPath, |
|
150 | ]; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Class constructor |
||
155 | * |
||
156 | * @param \GitElephant\Repository $repository repository instance |
||
157 | * @param string $permissions node permissions |
||
158 | * @param string $type node type |
||
159 | * @param string $sha node sha |
||
160 | * @param string $size node size in bytes |
||
161 | * @param string $name node name |
||
162 | * @param string $path node path |
||
163 | */ |
||
164 | 14 | public function __construct( |
|
182 | |||
183 | /** |
||
184 | * toString magic method |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | 1 | public function __toString() |
|
192 | |||
193 | /** |
||
194 | * Mime Type getter |
||
195 | * |
||
196 | * @param string $basePath the base path of the repository |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | public function getMimeType(string $basePath) |
||
204 | |||
205 | /** |
||
206 | * get extension if it's a blob |
||
207 | * |
||
208 | * @return string|null |
||
209 | */ |
||
210 | public function getExtension() |
||
219 | |||
220 | /** |
||
221 | * whether the node is a tree |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | 9 | public function isTree() |
|
229 | |||
230 | /** |
||
231 | * whether the node is a link |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | public function isLink() |
||
239 | |||
240 | /** |
||
241 | * whether the node is a blob |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | 1 | public function isBlob() |
|
249 | |||
250 | /** |
||
251 | * Full path getter |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | 12 | public function getFullPath() |
|
262 | |||
263 | /** |
||
264 | * permissions getter |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | public function getPermissions() |
||
272 | |||
273 | /** |
||
274 | * sha getter |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | 6 | public function getSha() |
|
282 | |||
283 | /** |
||
284 | * type getter |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | 13 | public function getType() |
|
292 | |||
293 | /** |
||
294 | * name getter |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | 5 | public function getName() |
|
302 | |||
303 | /** |
||
304 | * path getter |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | public function getPath() |
||
312 | |||
313 | /** |
||
314 | * size getter |
||
315 | * |
||
316 | * @return string |
||
317 | */ |
||
318 | public function getSize() |
||
322 | |||
323 | /** |
||
324 | * gets the last commit in this object |
||
325 | * |
||
326 | * @return Commit |
||
327 | */ |
||
328 | 3 | public function getLastCommit() |
|
334 | |||
335 | /** |
||
336 | * rev-parse command - often used to return a commit tag. |
||
337 | * |
||
338 | * @param array $options the options to apply to rev-parse |
||
339 | * |
||
340 | * @throws \RuntimeException |
||
341 | * @throws \InvalidArgumentException |
||
342 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
343 | * @return array |
||
344 | */ |
||
345 | 1 | public function revParse(array $options = []) |
|
353 | |||
354 | /* |
||
355 | * Repository setter |
||
356 | * |
||
357 | * @param \GitElephant\Repository $repository the repository variable |
||
358 | */ |
||
359 | 1 | public function setRepository(Repository $repository) |
|
363 | |||
364 | /** |
||
365 | * Repository getter |
||
366 | * |
||
367 | * @return \GitElephant\Repository |
||
368 | */ |
||
369 | 59 | public function getRepository() |
|
373 | } |
||
374 |