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