1 | <?php |
||
31 | class Object 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 Object |
||
92 | */ |
||
93 | 8 | public static function createFromOutputLine(Repository $repository, $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 | 6 | } 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 | 8 | $name, |
|
113 | $path |
||
114 | 8 | ); |
|
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($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 | 14 | case Object::TYPE_TREE: |
|
131 | 5 | $type = Object::TYPE_TREE; |
|
132 | 5 | break; |
|
133 | 14 | case Object::TYPE_BLOB: |
|
134 | 14 | $type = Object::TYPE_BLOB; |
|
135 | 14 | break; |
|
136 | 1 | case Object::TYPE_LINK: |
|
137 | 1 | $type = Object::TYPE_LINK; |
|
138 | 1 | break; |
|
139 | 14 | } |
|
140 | 14 | $sha = $matches[3]; |
|
141 | 14 | $size = $matches[4]; |
|
142 | 14 | $fullPath = $matches[5]; |
|
143 | |||
144 | return array( |
||
145 | 14 | 'permissions' => $permissions, |
|
146 | 14 | 'type' => $type, |
|
147 | 14 | 'sha' => $sha, |
|
148 | 14 | 'size' => $size, |
|
149 | 'fullPath' => $fullPath |
||
150 | 14 | ); |
|
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(Repository $repository, $permissions, $type, $sha, $size, $name, $path) |
|
165 | { |
||
166 | 14 | $this->repository = $repository; |
|
167 | 14 | $this->permissions = $permissions; |
|
168 | 14 | $this->type = $type; |
|
169 | 14 | $this->sha = $sha; |
|
170 | 14 | $this->size = $size; |
|
171 | 14 | $this->name = $name; |
|
172 | 14 | $this->path = $path; |
|
173 | 14 | } |
|
174 | |||
175 | /** |
||
176 | * toString magic method |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | 1 | public function __toString() |
|
181 | { |
||
182 | 1 | return (string) $this->name; |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * Mime Type getter |
||
187 | * |
||
188 | * @param string $basePath the base path of the repository |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | public function getMimeType($basePath) |
||
193 | { |
||
194 | return mime_content_type($basePath . DIRECTORY_SEPARATOR . $this->path); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * get extension if it's a blob |
||
199 | * |
||
200 | * @return string|null |
||
201 | */ |
||
202 | public function getExtension() |
||
203 | { |
||
204 | $pos = strrpos($this->name, '.'); |
||
205 | if ($pos === false) { |
||
206 | return null; |
||
207 | } else { |
||
208 | return substr($this->name, $pos+1); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * whether the node is a tree |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | 9 | public function isTree() |
|
218 | { |
||
219 | 9 | return self::TYPE_TREE == $this->getType(); |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * whether the node is a link |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | public function isLink() |
||
228 | { |
||
229 | return self::TYPE_LINK == $this->getType(); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * whether the node is a blob |
||
234 | * |
||
235 | * @return bool |
||
236 | */ |
||
237 | 1 | public function isBlob() |
|
238 | { |
||
239 | 1 | return self::TYPE_BLOB == $this->getType(); |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * Full path getter |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | 12 | public function getFullPath() |
|
254 | |||
255 | /** |
||
256 | * permissions getter |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | public function getPermissions() |
||
261 | { |
||
262 | return $this->permissions; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * sha getter |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | 6 | public function getSha() |
|
274 | |||
275 | /** |
||
276 | * type getter |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | 13 | public function getType() |
|
284 | |||
285 | /** |
||
286 | * name getter |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | 5 | public function getName() |
|
294 | |||
295 | /** |
||
296 | * path getter |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | public function getPath() |
||
304 | |||
305 | /** |
||
306 | * size getter |
||
307 | * |
||
308 | * @return string |
||
309 | */ |
||
310 | public function getSize() |
||
314 | |||
315 | /** |
||
316 | * gets the last commit in this object |
||
317 | * |
||
318 | * @return Commit |
||
319 | */ |
||
320 | 3 | public function getLastCommit() |
|
325 | |||
326 | /** |
||
327 | * rev-parse command - often used to return a commit tag. |
||
328 | * |
||
329 | * @param array $options the options to apply to rev-parse |
||
330 | * |
||
331 | * @throws \RuntimeException |
||
332 | * @throws \InvalidArgumentException |
||
333 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
334 | * @return array |
||
335 | */ |
||
336 | 1 | public function revParse(Array $options = array()) |
|
344 | |||
345 | /* |
||
346 | * Repository setter |
||
347 | * |
||
348 | * @param \GitElephant\Repository $repository the repository variable |
||
349 | */ |
||
350 | 1 | public function setRepository(Repository $repository) |
|
354 | |||
355 | /** |
||
356 | * Repository getter |
||
357 | * |
||
358 | * @return \GitElephant\Repository |
||
359 | */ |
||
360 | 59 | public function getRepository() |
|
364 | } |
||
365 |