1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Behat Environment Extension. |
4
|
|
|
* (c) Erik Wohllebe <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Wohlie\Behat\Environment\Resolver; |
11
|
|
|
|
12
|
|
|
use xformat\Properties; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class BootstrapFileService |
16
|
|
|
* |
17
|
|
|
* @package Wohlie\Behat\Environment |
18
|
|
|
*/ |
19
|
|
|
class Version |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Holds the current file path. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $filePath; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Version constructor. |
30
|
|
|
* |
31
|
|
|
* @param string $filePath |
32
|
|
|
*/ |
33
|
|
|
public function __construct($filePath) |
34
|
|
|
{ |
35
|
|
|
$this->setFilePath($filePath); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Parse the property file to extract the version. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function get() |
44
|
|
|
{ |
45
|
|
|
$version = null; |
46
|
|
|
$filePath = $this->getFilePath(); |
47
|
|
|
|
48
|
|
|
if ($this->isPackageJson($filePath) || $this->isComposerJson($filePath)) { |
49
|
|
|
$json = json_decode(file_get_contents($filePath), true); |
50
|
|
|
if (!empty($json['version'])) { |
51
|
|
|
$version = $json['version']; |
52
|
|
|
} |
53
|
|
|
} elseif ($this->isPropertyFile($filePath)) { |
54
|
|
|
$properties = new Properties(); |
55
|
|
|
$properties = $properties->getProperties($filePath); |
56
|
|
|
if (!empty($properties['artifact.version'])) { |
57
|
|
|
$version = $properties['artifact.version']; |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
|
|
throw new \UnexpectedValueException("The property file '{$filePath}' is not supported."); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!$version) { |
64
|
|
|
throw new \UnexpectedValueException( |
65
|
|
|
"The version can not determine form the property file '{$filePath}'." |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $version; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return true if the given file can be parsed. |
74
|
|
|
* |
75
|
|
|
* @param string $filePath |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function isValid($filePath) |
79
|
|
|
{ |
80
|
|
|
return $this->isFile($filePath) && ( |
81
|
|
|
$this->isPropertyFile($filePath) |
82
|
|
|
|| $this->isPackageJson($filePath) |
83
|
|
|
|| $this->isComposerJson($filePath) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return true if the given file path can be handel as file. |
89
|
|
|
* |
90
|
|
|
* @param string $filePath |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
protected function isFile($filePath) |
94
|
|
|
{ |
95
|
|
|
return file_exists($filePath) && is_file($filePath) && is_readable($filePath); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return true if the given file is a package json. |
100
|
|
|
* |
101
|
|
|
* @param string $filePath |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
protected function isPackageJson($filePath) |
105
|
|
|
{ |
106
|
|
|
return 'package.json' === strtolower(pathinfo($filePath, PATHINFO_BASENAME)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return true if the given file is a composer json. |
111
|
|
|
* |
112
|
|
|
* @param string $filePath |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
protected function isComposerJson($filePath) |
116
|
|
|
{ |
117
|
|
|
return 'composer.json' === strtolower(pathinfo($filePath, PATHINFO_BASENAME)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return true if the given file is a property file. |
122
|
|
|
* |
123
|
|
|
* @param string $filePath |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
protected function isPropertyFile($filePath) |
127
|
|
|
{ |
128
|
|
|
return 'property' === strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set the given file path to resolve the version. |
133
|
|
|
* |
134
|
|
|
* @param string $filePath |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function setFilePath($filePath) |
138
|
|
|
{ |
139
|
|
|
if (!$this->isValid($filePath)) { |
140
|
|
|
throw new \RuntimeException("The given file '{$filePath}' does not exists."); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->filePath = $filePath; |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Return the file path. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function getFilePath() |
153
|
|
|
{ |
154
|
|
|
return $this->filePath; |
155
|
|
|
} |
156
|
|
|
} |