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\RangeMatcher\BranchParser; |
16
|
|
|
use ptlis\SemanticVersion\Parse\RangeMatcher\CaretRangeParser; |
17
|
|
|
use ptlis\SemanticVersion\Parse\RangeMatcher\ComparatorVersionParser; |
18
|
|
|
use ptlis\SemanticVersion\Parse\RangeMatcher\HyphenatedRangeParser; |
19
|
|
|
use ptlis\SemanticVersion\Parse\RangeMatcher\TildeRangeParser; |
20
|
|
|
use ptlis\SemanticVersion\Parse\RangeMatcher\WildcardRangeParser; |
21
|
|
|
use ptlis\SemanticVersion\Parse\VersionParser; |
22
|
|
|
use ptlis\SemanticVersion\Parse\VersionRangeParser; |
23
|
|
|
use ptlis\SemanticVersion\Parse\VersionTokenizer; |
24
|
|
|
use ptlis\SemanticVersion\Version\Label\LabelBuilder; |
25
|
|
|
use ptlis\SemanticVersion\Version\VersionInterface; |
26
|
|
|
use ptlis\SemanticVersion\VersionRange\VersionRangeInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Simple class to provide version parsing with good defaults. |
30
|
|
|
*/ |
31
|
|
|
final class VersionEngine |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
/** @var VersionTokenizer */ |
34
|
|
|
private $tokenizer; |
35
|
|
|
|
36
|
|
|
/** @var VersionRangeParser */ |
37
|
|
|
private $versionRangeParser; |
38
|
|
|
|
39
|
|
|
/** @var VersionParser */ |
40
|
|
|
private $versionParser; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
*/ |
46
|
7 |
|
public function __construct() |
47
|
|
|
{ |
48
|
7 |
|
$this->versionParser = new VersionParser(new LabelBuilder()); |
49
|
7 |
|
$comparatorFactory = new ComparatorFactory(); |
50
|
|
|
|
51
|
|
|
$matcherList = [ |
52
|
7 |
|
new CaretRangeParser($this->versionParser, $comparatorFactory->get('>='), $comparatorFactory->get('<')), |
53
|
7 |
|
new TildeRangeParser($this->versionParser, $comparatorFactory->get('>='), $comparatorFactory->get('<')), |
54
|
7 |
|
new WildcardRangeParser($this->versionParser, $comparatorFactory->get('>='), $comparatorFactory->get('<')), |
55
|
7 |
|
new BranchParser($this->versionParser, $comparatorFactory->get('>='), $comparatorFactory->get('<')), |
56
|
7 |
|
new ComparatorVersionParser($comparatorFactory, $this->versionParser), |
57
|
7 |
|
new HyphenatedRangeParser( |
58
|
7 |
|
$this->versionParser, |
59
|
7 |
|
$comparatorFactory->get('>='), |
60
|
7 |
|
$comparatorFactory->get('<'), |
61
|
7 |
|
$comparatorFactory->get('<=') |
62
|
|
|
) |
63
|
|
|
]; |
64
|
|
|
|
65
|
7 |
|
$this->tokenizer = new VersionTokenizer(); |
66
|
7 |
|
$this->versionRangeParser = new VersionRangeParser($matcherList); |
67
|
7 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Parse a semantic version string into an object implementing VersionInterface. |
71
|
|
|
* |
72
|
|
|
* @param string $versionString |
73
|
|
|
* |
74
|
|
|
* @throws \InvalidArgumentException When version string is invalid. |
75
|
|
|
* |
76
|
|
|
* @return VersionInterface |
77
|
|
|
*/ |
78
|
3 |
|
public function parseVersion($versionString) |
79
|
|
|
{ |
80
|
3 |
|
$tokenList = $this->tokenizer->tokenize($versionString); |
81
|
|
|
|
82
|
|
|
try { |
83
|
3 |
|
$version = $this->versionParser->parse($tokenList); |
84
|
2 |
|
} catch (\RuntimeException $e) { |
85
|
2 |
|
throw new \InvalidArgumentException('"' . $versionString . '" is not a valid semantic version number', $e->getCode(), $e); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return $version; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Parse a version range & return an object implementing VersionRangeInterface that encodes those rules. |
93
|
|
|
* |
94
|
|
|
* @param string $rangeString |
95
|
|
|
* |
96
|
|
|
* @throws \InvalidArgumentException When version range string is invalid. |
97
|
|
|
* |
98
|
|
|
* @return VersionRangeInterface |
99
|
|
|
*/ |
100
|
4 |
|
public function parseRange($rangeString) |
101
|
|
|
{ |
102
|
4 |
|
$tokenList = $this->tokenizer->tokenize($rangeString); |
103
|
|
|
|
104
|
|
|
try { |
105
|
4 |
|
$range = $this->versionRangeParser->parseRange($tokenList); |
106
|
1 |
|
} catch (\RuntimeException $e) { |
107
|
1 |
|
throw new \InvalidArgumentException('"' . $rangeString . '" is not a valid version range', $e->getCode(), $e); |
108
|
|
|
} |
109
|
|
|
|
110
|
3 |
|
return $range; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|