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