|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright (c) 2014-2017 brian ridley |
|
5
|
|
|
* @author brian ridley <[email protected]> |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ptlis\SemanticVersion; |
|
13
|
|
|
|
|
14
|
|
|
use ptlis\SemanticVersion\Comparator\ComparatorFactory; |
|
15
|
|
|
use ptlis\SemanticVersion\Parse\Matcher\BranchParser; |
|
16
|
|
|
use ptlis\SemanticVersion\Parse\Matcher\CaretRangeParser; |
|
17
|
|
|
use ptlis\SemanticVersion\Parse\Matcher\ComparatorVersionParser; |
|
18
|
|
|
use ptlis\SemanticVersion\Parse\Matcher\HyphenatedRangeParser; |
|
19
|
|
|
use ptlis\SemanticVersion\Parse\Matcher\TildeRangeParser; |
|
20
|
|
|
use ptlis\SemanticVersion\Parse\Matcher\WildcardRangeParser; |
|
21
|
|
|
use ptlis\SemanticVersion\Parse\VersionParser; |
|
22
|
|
|
use ptlis\SemanticVersion\Parse\VersionTokenizer; |
|
23
|
|
|
use ptlis\SemanticVersion\Version\Label\LabelBuilder; |
|
24
|
|
|
use ptlis\SemanticVersion\Version\VersionBuilder; |
|
25
|
|
|
use ptlis\SemanticVersion\Version\VersionInterface; |
|
26
|
|
|
use ptlis\SemanticVersion\VersionRange\ComparatorVersion; |
|
27
|
|
|
use ptlis\SemanticVersion\VersionRange\VersionRangeInterface; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Simple class to provide version parsing with good defaults. |
|
31
|
|
|
*/ |
|
32
|
|
|
final class VersionEngine |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
/** @var VersionTokenizer */ |
|
35
|
|
|
private $tokenizer; |
|
36
|
|
|
|
|
37
|
|
|
/** @var VersionParser */ |
|
38
|
|
|
private $parser; |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constructor. |
|
43
|
|
|
*/ |
|
44
|
7 |
|
public function __construct() |
|
45
|
|
|
{ |
|
46
|
7 |
|
$comparatorFactory = new ComparatorFactory(); |
|
47
|
7 |
|
$versionBuilder = new VersionBuilder(new LabelBuilder()); |
|
48
|
|
|
|
|
49
|
7 |
|
$wildcardParser = new WildcardRangeParser( |
|
50
|
7 |
|
$comparatorFactory->get('>='), |
|
|
|
|
|
|
51
|
7 |
|
$comparatorFactory->get('<') |
|
|
|
|
|
|
52
|
7 |
|
); |
|
53
|
|
|
|
|
54
|
|
|
$matcherList = [ |
|
55
|
7 |
|
new CaretRangeParser( |
|
56
|
7 |
|
$comparatorFactory->get('>='), |
|
|
|
|
|
|
57
|
7 |
|
$comparatorFactory->get('<') |
|
|
|
|
|
|
58
|
7 |
|
), |
|
59
|
7 |
|
new TildeRangeParser($wildcardParser), |
|
60
|
7 |
|
$wildcardParser, |
|
61
|
7 |
|
new BranchParser($wildcardParser), |
|
62
|
7 |
|
new ComparatorVersionParser( |
|
63
|
7 |
|
$comparatorFactory, |
|
64
|
|
|
$versionBuilder |
|
65
|
7 |
|
), |
|
66
|
7 |
|
new HyphenatedRangeParser( |
|
67
|
7 |
|
$versionBuilder, |
|
68
|
7 |
|
$comparatorFactory->get('>='), |
|
|
|
|
|
|
69
|
7 |
|
$comparatorFactory->get('<'), |
|
|
|
|
|
|
70
|
7 |
|
$comparatorFactory->get('<=') |
|
|
|
|
|
|
71
|
7 |
|
) |
|
72
|
7 |
|
]; |
|
73
|
|
|
|
|
74
|
7 |
|
$this->tokenizer = new VersionTokenizer(); |
|
75
|
7 |
|
$this->parser = new VersionParser($matcherList); |
|
76
|
7 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Parse a semantic version string into an object implementing VersionInterface. |
|
80
|
|
|
* |
|
81
|
|
|
* @todo Hacky - create a better method for handling this. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $versionString |
|
84
|
|
|
* |
|
85
|
|
|
* @throws \InvalidArgumentException When version string is invalid. |
|
86
|
|
|
* |
|
87
|
|
|
* @return VersionInterface |
|
88
|
|
|
*/ |
|
89
|
3 |
|
public function parseVersion($versionString) |
|
90
|
|
|
{ |
|
91
|
3 |
|
$tokenList = $this->tokenizer->tokenize($versionString); |
|
92
|
|
|
|
|
93
|
|
|
try { |
|
94
|
3 |
|
$range = $this->parser->parseRange($tokenList); |
|
95
|
3 |
|
} catch (\RuntimeException $e) { |
|
96
|
1 |
|
throw new \InvalidArgumentException('"' . $versionString . '" is not a valid semantic version number', $e->getCode(), $e); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
if (!($range instanceof ComparatorVersion)) { |
|
100
|
1 |
|
throw new \InvalidArgumentException( |
|
101
|
1 |
|
'"' . $versionString . '" is not a valid semantic version number' |
|
102
|
1 |
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
return $range->getVersion(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Parse a version range & return an object implementing VersionRangeInterface that encodes those rules. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $rangeString |
|
112
|
|
|
* |
|
113
|
|
|
* @throws \InvalidArgumentException When version range string is invalid. |
|
114
|
|
|
* |
|
115
|
|
|
* @return VersionRangeInterface |
|
116
|
|
|
*/ |
|
117
|
4 |
|
public function parseRange($rangeString) |
|
118
|
|
|
{ |
|
119
|
4 |
|
$tokenList = $this->tokenizer->tokenize($rangeString); |
|
120
|
|
|
|
|
121
|
|
|
try { |
|
122
|
4 |
|
$range = $this->parser->parseRange($tokenList); |
|
123
|
4 |
|
} catch (\RuntimeException $e) { |
|
124
|
1 |
|
throw new \InvalidArgumentException('"' . $rangeString . '" is not a valid version range', $e->getCode(), $e); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
3 |
|
return $range; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|