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
|
2 |
|
final public static function getInstance() |
17
|
|
|
{ |
18
|
2 |
|
if (!self::$instance) { |
19
|
1 |
|
$class = get_called_class(); |
20
|
1 |
|
self::$instance = new $class; |
21
|
|
|
} |
22
|
2 |
|
return self::$instance; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* private construct - singleton |
27
|
|
|
*/ |
28
|
1 |
|
private function __construct() |
29
|
|
|
{ |
30
|
1 |
|
} |
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
|
7 |
|
public function get() |
45
|
|
|
{ |
46
|
7 |
|
$version = (string)$this->getEnv('PROJECT_VERSION'); |
47
|
7 |
|
if ($version) { |
48
|
1 |
|
return $version; |
49
|
|
|
} |
50
|
|
|
try { |
51
|
6 |
|
if (isset($this->getStructure()->version)) { |
52
|
1 |
|
return $this->getStructure()->version; |
53
|
|
|
} |
54
|
5 |
|
} catch (\LogicException $e) { |
55
|
|
|
//Do nothing since we have a fallback |
56
|
|
|
} |
57
|
5 |
|
return trim($this->getFromGit()) ?: 'v0.0.0'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
* @codeCoverageIgnore |
63
|
|
|
*/ |
64
|
|
|
protected function getFromGit() |
65
|
|
|
{ |
66
|
|
|
$rootPath = implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', '..', '..', '..')); |
67
|
|
|
exec("cd $rootPath && git describe", $gitVersion, $errorCode); |
68
|
|
|
return !$errorCode ? $gitVersion : ""; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Retrieve environment variable |
73
|
|
|
* @param string $name |
74
|
|
|
* @return array|false|string |
75
|
|
|
* @codeCoverageIgnore |
76
|
|
|
*/ |
77
|
|
|
protected function getEnv($name) |
78
|
|
|
{ |
79
|
|
|
return getenv($name); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function name() |
83
|
|
|
{ |
84
|
1 |
|
return $this->getStructure()->name; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return composer.json path to project |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
1 |
|
protected function getComposerPath() |
92
|
|
|
{ |
93
|
1 |
|
return implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', '..', '..', '..', 'composer.json')); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get composer file structure |
98
|
|
|
* @return Object |
99
|
|
|
*/ |
100
|
4 |
View Code Duplication |
protected function getStructure() |
|
|
|
|
101
|
|
|
{ |
102
|
4 |
|
if (!$this->structure) { |
103
|
4 |
|
if (!is_file($this->getComposerPath())) { |
104
|
2 |
|
throw new \LogicException('composer.json does not exist'); |
105
|
|
|
} |
106
|
2 |
|
$this->structure = json_decode(file_get_contents($this->getComposerPath())); |
107
|
2 |
|
if (!$this->structure) { |
108
|
1 |
|
throw new \LogicException('composer.json invalid structure'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
1 |
|
return $this->structure; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.