1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cpliakas\ManifestPublisher; |
4
|
|
|
|
5
|
|
|
use GitWrapper\GitWrapper; |
6
|
|
|
|
7
|
|
|
class Repository |
8
|
|
|
{ |
9
|
|
|
use Traits\Directories; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $packageName; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var GitWrapper |
18
|
|
|
*/ |
19
|
|
|
protected $gitWrapper; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $pharFilenames = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $packageName |
28
|
|
|
* @param GitWrapper|null $gitWrapper |
29
|
|
|
*/ |
30
|
3 |
|
public function __construct($packageName, GitWrapper $gitWrapper = null) |
31
|
|
|
{ |
32
|
3 |
|
$this->packageName = $packageName; |
33
|
|
|
|
34
|
3 |
|
if (null === $gitWrapper) { |
35
|
3 |
|
$gitWrapper = new GitWrapper(); |
36
|
3 |
|
} |
37
|
|
|
|
38
|
3 |
|
$this->gitWrapper = $gitWrapper; |
39
|
3 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getPackageName() |
45
|
|
|
{ |
46
|
|
|
return $this->packageName; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return GitWrapper |
51
|
|
|
*/ |
52
|
|
|
public function getGitWrapper() |
53
|
|
|
{ |
54
|
|
|
return $this->gitWrapper; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string|null $tag |
59
|
|
|
* |
60
|
|
|
* @return \GitWrapper\GitWorkingCopy |
61
|
|
|
*/ |
62
|
3 |
|
public function checkout($tag = null) |
63
|
|
|
{ |
64
|
|
|
$git = $this->gitWrapper->workingCopy($this->getRepositoryDirectory($this)); |
65
|
|
|
|
66
|
|
|
if (!$git->isCloned()) { |
67
|
|
|
$git->clone('[email protected]:' . $this . '.git'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($tag !== null) { |
71
|
|
|
$git->checkout($tag, ['f' => true]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $git; |
75
|
3 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getPackageVersions() |
81
|
|
|
{ |
82
|
|
|
$git = $this->checkout() |
83
|
|
|
->fetchAll() |
84
|
|
|
->clearOutput() |
85
|
|
|
; |
86
|
|
|
|
87
|
|
|
$output = (string) $git->tag(); |
88
|
|
|
$tags = preg_split("/\r\n|\n|\r/", rtrim($output)); |
89
|
|
|
|
90
|
|
|
return array_reverse($tags); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $tag |
95
|
|
|
* |
96
|
|
|
* @return string|false |
97
|
|
|
*/ |
98
|
|
|
public function getPharFilename($tag) |
99
|
|
|
{ |
100
|
|
|
if (!isset($this->pharFilenames[$tag])) { |
101
|
|
|
|
102
|
|
|
$git = $this->checkout($tag); |
103
|
|
|
$filename = $git->getDirectory() . '/box.json'; |
104
|
|
|
|
105
|
|
|
if (!file_exists($filename)) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// @todo validation, but in dev I want to see the errors |
110
|
|
|
$filedata = file_get_contents($filename); |
111
|
|
|
$json = json_decode($filedata, true); |
112
|
|
|
|
113
|
|
|
$this->pharFilenames[$tag] = basename($json['output']); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->pharFilenames[$tag]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns the package name. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function __toString() |
125
|
|
|
{ |
126
|
|
|
return $this->packageName; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|