1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Composer plugin for bower/npm assets |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/composer-asset-plugin |
7
|
|
|
* @package composer-asset-plugin |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hiqdev\composerassetplugin; |
13
|
|
|
|
14
|
|
|
use Composer\Semver\Comparator; |
15
|
|
|
use Composer\Semver\Constraint\EmptyConstraint; |
16
|
|
|
use Composer\Semver\VersionParser; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constraint helper class. |
20
|
|
|
* |
21
|
|
|
* @author Andrii Vasyliev <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Constraint |
24
|
|
|
{ |
25
|
|
|
static private $parser; |
26
|
|
|
|
27
|
1 |
|
static public function getParser() |
|
|
|
|
28
|
|
|
{ |
29
|
1 |
|
if (static::$parser === null) { |
|
|
|
|
30
|
1 |
|
static::$parser = new VersionParser(); |
|
|
|
|
31
|
1 |
|
} |
32
|
|
|
|
33
|
1 |
|
return static::$parser; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
static public function parse($constraint) |
|
|
|
|
37
|
|
|
{ |
38
|
1 |
|
return static::getParser()->parseConstraints($constraint); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Merges two constraints. |
43
|
|
|
* Doesn't resolve version conflicts. |
44
|
|
|
* @param string $a |
45
|
|
|
* @param string $b |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
1 |
|
static public function merge($a, $b) |
|
|
|
|
49
|
|
|
{ |
50
|
1 |
|
$acon = static::parse($a); |
51
|
1 |
|
$bcon = static::parse($b); |
52
|
|
|
|
53
|
1 |
|
if ($acon instanceof EmptyConstraint) { |
54
|
1 |
|
return $b; |
55
|
1 |
|
} elseif ($bcon instanceof EmptyConstraint) { |
56
|
1 |
|
return $a; |
57
|
1 |
|
} elseif ($acon->matches($bcon) || $bcon->matches($acon)) { |
58
|
1 |
|
return strlen($a)>strlen($b) ? $b : $a; |
59
|
|
|
} else { |
60
|
1 |
|
return $a . ' ' . $b; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
static public function findMax(array $versions) |
|
|
|
|
65
|
|
|
{ |
66
|
1 |
|
$versions = array_unique(array_values($versions)); |
67
|
1 |
|
if (count($versions)<2) { |
68
|
|
|
return reset($versions); |
69
|
|
|
} |
70
|
1 |
|
$max = $versions[0]; |
71
|
1 |
|
for ($i=1; $i<= count($versions); $i++) { |
|
|
|
|
72
|
1 |
|
$cur = $versions[$i]; |
73
|
1 |
|
if (Comparator::compare($cur, '>', $max)) { |
74
|
1 |
|
$max = $cur; |
75
|
1 |
|
} |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
return trim($max); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Is constraint disjunctive. |
83
|
|
|
* TODO redo after Semver will have such function. |
84
|
|
|
* @param string $constraint |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
static public function isDisjunctive($constraint) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
return strpos($constraint, '|') !== false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|