1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Utility; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class Version |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $dir = __DIR__; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $placeholder; |
19
|
|
|
private $version; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* resolve version in case it is still a placeholder @.@.@, |
23
|
|
|
* e.g. when a source installation (composer package or git |
24
|
|
|
* clone) |
25
|
|
|
* |
26
|
|
|
* @param string $version |
27
|
|
|
* |
28
|
|
|
* @return string resolved version |
29
|
|
|
*/ |
30
|
1 |
|
public static function resolve($version) |
31
|
|
|
{ |
32
|
1 |
|
$subject = new self($version); |
33
|
1 |
|
return $subject->resolveSourceVersion(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Version constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $version input |
40
|
|
|
* @param string $placeholder |
41
|
|
|
* @param string $dir [optional] |
42
|
|
|
*/ |
43
|
11 |
|
public function __construct($version, $placeholder = '@.@.@', $dir = null) |
44
|
|
|
{ |
45
|
11 |
|
$this->version = $version; |
46
|
11 |
|
$this->placeholder = $placeholder; |
47
|
11 |
|
$this->dir = $dir === null ? __DIR__ : $dir; |
48
|
11 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* obtain utility version for the installation / source |
52
|
|
|
* in use. |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
5 |
|
public function resolveSourceVersion() |
57
|
|
|
{ |
58
|
|
|
// as build version |
59
|
5 |
|
if (null !== $buildVersion = $this->getBuildVersion()) { |
60
|
1 |
|
return $buildVersion; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// as composer package |
64
|
4 |
|
if (null !== $packageVersion = $this->getPackageVersion()) { |
65
|
1 |
|
return $packageVersion; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// as git repository |
69
|
3 |
|
if (null !== $gitVersion = $this->getGitVersion()) { |
70
|
2 |
|
return $gitVersion; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->version; // @codeCoverageIgnore |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* is version from build? |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
3 |
|
public function isBuildVersion() |
82
|
|
|
{ |
83
|
3 |
|
return $this->placeholder !== $this->version; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return null|string version or null if there is no buid version |
88
|
|
|
*/ |
89
|
3 |
|
public function getBuildVersion() |
90
|
|
|
{ |
91
|
3 |
|
if ($this->isBuildVersion()) { |
92
|
2 |
|
return $this->version; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* get package version from composer/installed.json. |
100
|
|
|
* |
101
|
|
|
* this is possible if pipelines is required as a composer package. |
102
|
|
|
* |
103
|
|
|
* @return null|string version or null if not required as composer package |
104
|
|
|
*/ |
105
|
3 |
|
public function getPackageVersion() |
106
|
|
|
{ |
107
|
3 |
|
foreach ($this->getInstalledPackages() as $package) { |
108
|
2 |
|
if (!isset($package->name) || $package->name !== 'ktomk/pipelines') { |
109
|
2 |
|
continue; |
110
|
|
|
} |
111
|
2 |
|
if (!isset($package->version)) { |
112
|
1 |
|
break; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
return $package->version . '-composer'; |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
return null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return array of installed packages, empty array if none |
123
|
|
|
*/ |
124
|
1 |
|
public function getInstalledPackages() |
125
|
|
|
{ |
126
|
1 |
|
$installedJsonFile = $this->dir . '/../../../../composer/installed.json'; |
127
|
1 |
|
if (false === $buffer = @file_get_contents($installedJsonFile)) { |
|
|
|
|
128
|
1 |
|
$buffer = 'null'; |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
$packages = (array)json_decode($buffer); |
132
|
|
|
|
133
|
1 |
|
return $packages; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* get git version from git repository |
138
|
|
|
*/ |
139
|
3 |
|
public function getGitVersion() |
140
|
|
|
{ |
141
|
3 |
|
$buffer = rtrim(exec(sprintf( |
142
|
3 |
|
'cd %s 2>/dev/null && echo $(git describe --tags --always --first-parent 2>/dev/null)$(git diff-index --quiet HEAD -- 2>/dev/null || echo +)', |
143
|
3 |
|
escapeshellarg($this->dir) |
144
|
|
|
))); |
145
|
|
|
|
146
|
3 |
|
if (false === in_array($buffer, array('+', ''), true)) { |
147
|
1 |
|
return $buffer; |
148
|
|
|
} |
149
|
|
|
|
150
|
2 |
|
return null; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|