1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Biurad opensource projects. |
5
|
|
|
* |
6
|
|
|
* @copyright 2022 Biurad Group (https://biurad.com/) |
7
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Biurad\Git\Commit; |
14
|
|
|
|
15
|
|
|
use Biurad\Git\{GitObject, Repository}; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Represents a Blob commit. |
19
|
|
|
* |
20
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Blob extends GitObject |
23
|
|
|
{ |
24
|
|
|
protected ?string $content = null, $mimetype = null; |
25
|
|
|
|
26
|
|
|
public function __construct(Repository $repository, string $hash = null, protected ?int $mode = null) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($repository, $hash); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getMode(): ?int |
32
|
|
|
{ |
33
|
|
|
return $this->mode; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns content of the blob. |
38
|
|
|
* |
39
|
|
|
* @throws \RuntimeException Error occurred while getting content of blob |
40
|
|
|
*/ |
41
|
|
|
public function getContent(): string |
42
|
|
|
{ |
43
|
|
|
if (null === $this->content) { |
44
|
|
|
$this->content = $this->repository->run('cat-file', ['-p', $this->__toString()]); |
45
|
|
|
|
46
|
|
|
if (empty($this->content) || 0 !== $this->repository->getExitCode()) { |
47
|
|
|
throw new \RuntimeException(\sprintf('Blob "%s" content failed to read', $this->__toString())); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->content; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Determine the mimetype of the blob. |
56
|
|
|
*/ |
57
|
|
|
public function getMimetype(): string |
58
|
|
|
{ |
59
|
|
|
if (null === $this->mimetype) { |
60
|
|
|
$finfo = new \finfo(\FILEINFO_MIME); |
61
|
|
|
$this->mimetype = $finfo->buffer($this->getContent()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->mimetype; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Determines if file is binary. |
69
|
|
|
*/ |
70
|
|
|
public function isBinary(): bool |
71
|
|
|
{ |
72
|
|
|
return 1 !== \preg_match('#^(?|text/|application/xml)#', $this->getMimetype()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Determines if file is text. |
77
|
|
|
*/ |
78
|
|
|
public function isText(): bool |
79
|
|
|
{ |
80
|
|
|
return !$this->isBinary(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|