1
|
|
|
<?php |
2
|
|
|
namespace FMUP; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class ProjectVersion |
6
|
|
|
* @package LogiCE |
7
|
|
|
*/ |
8
|
|
|
class ProjectVersion |
9
|
|
|
{ |
10
|
|
|
private static $instance; |
11
|
|
|
private $structure; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @return $this |
15
|
|
|
*/ |
16
|
|
|
final public static function getInstance() |
17
|
|
|
{ |
18
|
|
|
if (!self::$instance) { |
19
|
|
|
$class = get_called_class(); |
20
|
|
|
self::$instance = new $class; |
21
|
|
|
} |
22
|
|
|
return self::$instance; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* private construct - singleton |
27
|
|
|
*/ |
28
|
|
|
private function __construct() |
29
|
|
|
{ |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* private clone - singleton |
34
|
|
|
* @codeCoverageIgnore |
35
|
|
|
*/ |
36
|
|
|
private function __clone() |
37
|
|
|
{ |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get version name |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function get() |
45
|
|
|
{ |
46
|
|
|
$version = $this->getEnv('PROJECT_VERSION'); |
|
|
|
|
47
|
|
|
if ($version) { |
48
|
|
|
return $version; |
49
|
|
|
} |
50
|
|
|
try { |
51
|
|
|
if (isset($this->getStructure()->version)) { |
52
|
|
|
return $this->getStructure()->version; |
53
|
|
|
} |
54
|
|
|
} catch (\LogicException $e) { |
|
|
|
|
55
|
|
|
} |
56
|
|
|
$rootPath = $this->getGitHeadFilePath(); |
57
|
|
|
if (file_exists($rootPath)) { |
58
|
|
|
$stringFromFile = file($rootPath); |
59
|
|
|
$firstLine = $stringFromFile[0]; //get the string from the array |
60
|
|
|
$explodedString = explode("/", $firstLine, 3); //seperate out by the "/" in the string |
61
|
|
|
return trim($explodedString[2]); //get the one that is always the branch name |
62
|
|
|
} |
63
|
|
|
return 'v0.0.0'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
* @codeCoverageIgnore |
69
|
|
|
*/ |
70
|
|
|
protected function getGitHeadFilePath() |
71
|
|
|
{ |
72
|
|
|
return implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', '..', '..', '..', '.git', 'HEAD')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Retrieve environment variable |
77
|
|
|
* @param string $name |
78
|
|
|
* @return array|false|string |
79
|
|
|
* @codeCoverageIgnore |
80
|
|
|
*/ |
81
|
|
|
protected function getEnv($name) |
82
|
|
|
{ |
83
|
|
|
return getenv($name); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function name() |
87
|
|
|
{ |
88
|
|
|
return $this->getStructure()->name; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return composer.json path to project |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function getComposerPath() |
96
|
|
|
{ |
97
|
|
|
return implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', '..', '..', '..', 'composer.json')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get composer file structure |
102
|
|
|
* @return Object |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
protected function getStructure() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
if (!$this->structure) { |
107
|
|
|
if (!is_file($this->getComposerPath())) { |
108
|
|
|
throw new \LogicException('composer.json does not exist'); |
109
|
|
|
} |
110
|
|
|
$this->structure = json_decode(file_get_contents($this->getComposerPath())); |
111
|
|
|
if (!$this->structure) { |
112
|
|
|
throw new \LogicException('composer.json invalid structure'); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
return $this->structure; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|