|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spinen\Version; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Version |
|
7
|
|
|
* |
|
8
|
|
|
* Parse the version file into the parts |
|
9
|
|
|
* |
|
10
|
|
|
* @package App\Support |
|
11
|
|
|
*/ |
|
12
|
|
|
class Version |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Major version |
|
16
|
|
|
* |
|
17
|
|
|
* @var integer|null |
|
18
|
|
|
*/ |
|
19
|
|
|
public $major = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Meta |
|
23
|
|
|
* |
|
24
|
|
|
* Extra data about the version with dots separating key data and underscores for spaces |
|
25
|
|
|
* |
|
26
|
|
|
* @var string|null |
|
27
|
|
|
*/ |
|
28
|
|
|
public $meta = null; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Minor version |
|
32
|
|
|
* |
|
33
|
|
|
* @var integer|null |
|
34
|
|
|
*/ |
|
35
|
|
|
public $minor = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Patch version |
|
39
|
|
|
* |
|
40
|
|
|
* @var integer|null |
|
41
|
|
|
*/ |
|
42
|
|
|
public $patch = null; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Pre release |
|
46
|
|
|
* |
|
47
|
|
|
* This will most likely be the branch version, unless we are on the master branch |
|
48
|
|
|
* |
|
49
|
|
|
* @var string|null |
|
50
|
|
|
*/ |
|
51
|
|
|
public $pre_release = null; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Version in SemVer format |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
public $semver; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The version of the application |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
public $version = "UNDEFINED"; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* The file that holds the version info |
|
69
|
|
|
* |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $version_file; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Version constructor. |
|
76
|
|
|
* |
|
77
|
|
|
* @param null $file |
|
|
|
|
|
|
78
|
|
|
*/ |
|
79
|
9 |
|
public function __construct($file = null) |
|
80
|
|
|
{ |
|
81
|
9 |
|
$this->version_file = $file |
|
|
|
|
|
|
82
|
1 |
|
? : base_path('VERSION'); |
|
83
|
|
|
|
|
84
|
9 |
|
$this->parseVersionFile(); |
|
85
|
|
|
|
|
86
|
9 |
|
$this->generateSemVer(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* If the object is used as a string, then return the full version |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
9 |
|
public function __toString() |
|
95
|
|
|
{ |
|
96
|
9 |
|
return $this->semver; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Put together version parts into the SemVer format |
|
101
|
|
|
*/ |
|
102
|
9 |
|
protected function generateSemVer() |
|
103
|
|
|
{ |
|
104
|
9 |
|
$this->semver = $this->version; |
|
105
|
|
|
|
|
106
|
9 |
|
if ($this->pre_release) { |
|
107
|
3 |
|
$this->semver .= '-' . $this->pre_release; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
9 |
|
if ($this->meta) { |
|
111
|
3 |
|
$this->semver .= '+' . $this->meta; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* If the read in line matches a version, then parse it |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $version |
|
119
|
|
|
* |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
7 |
|
protected function parseVersion($version) |
|
123
|
|
|
{ |
|
124
|
7 |
|
if (!preg_match('/\\d+\\.\\d+\\.\\d+/u', $version)) { |
|
125
|
1 |
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
6 |
|
$this->version = $version; |
|
129
|
|
|
|
|
130
|
6 |
|
list($this->major, $this->minor, $this->patch) = array_map('intval', explode(".", $this->version)); |
|
131
|
|
|
|
|
132
|
6 |
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Read the version file into the various parts |
|
137
|
|
|
*/ |
|
138
|
9 |
|
protected function parseVersionFile() |
|
139
|
|
|
{ |
|
140
|
9 |
|
if (!file_exists($this->version_file)) { |
|
141
|
1 |
|
return; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
// Read file in as an array & remove any empty lines |
|
145
|
8 |
|
$version_parts = array_filter(explode("\n", @file_get_contents($this->version_file))); |
|
146
|
|
|
|
|
147
|
|
|
// First line is the version |
|
148
|
8 |
|
if (empty($version_parts) or !$this->parseVersion(array_shift($version_parts))) { |
|
149
|
2 |
|
return; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
// Next line is branch/pre release |
|
153
|
6 |
|
$pre_release = array_shift($version_parts); |
|
154
|
|
|
|
|
155
|
6 |
|
$this->pre_release = ($pre_release !== 'master') |
|
156
|
4 |
|
? $pre_release |
|
157
|
2 |
|
: null; |
|
158
|
|
|
|
|
159
|
|
|
// Is there anything left in the file for meta? |
|
160
|
6 |
|
if (empty($version_parts)) { |
|
161
|
3 |
|
return; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
// Everything left is the meta, so concatenate it with .'s & replace the spaces with _'s |
|
165
|
3 |
|
$this->meta = preg_replace("/\\s+/u", "_", implode('.', $version_parts)); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|