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
|
|
|
/** |
15
|
|
|
* Constraint helper class. |
16
|
|
|
* |
17
|
|
|
* @author Andrii Vasyliev <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Constraint |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Merges two constraints. |
23
|
|
|
* Doesn't resolve version conflicts. |
24
|
|
|
* @param $a |
25
|
|
|
* @param $b |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
static public function merge($a, $b) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$a = trim($a); |
31
|
|
|
$b = trim($b); |
32
|
|
|
var_dump("a:$a b:$b"); |
|
|
|
|
33
|
|
|
|
34
|
|
|
if ($a === $b || static::isWeaker($b, $a)) { |
35
|
|
|
return $a; |
36
|
|
|
} elseif (static::isWeaker($a, $b)) { |
37
|
|
|
return $b; |
38
|
|
|
} else { |
39
|
|
|
return $a . ' ' . $b; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Check if $a is weaker condition then $b, like: |
45
|
|
|
* - a="*" b="2.2" |
46
|
|
|
* - a="2.2 | 3.3" b="2.2" |
47
|
|
|
* - a="1.1 | 2.2" b="2.2" |
48
|
|
|
* Possible optimization. |
49
|
|
|
* // TODO Rename and implement. |
50
|
|
|
* @param string $a |
51
|
|
|
* @param string $b |
52
|
|
|
* @return boolean |
53
|
|
|
*/ |
54
|
|
|
static public function isWeaker($a, $b) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
return static::isEmpty($a) || static::startsWith($a, $b . ' |') | static::endsWith($a, '| ' . $b); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
static public function startsWith($haystack, $needle) { |
|
|
|
|
60
|
|
|
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
static public function endsWith($haystack, $needle) { |
|
|
|
|
64
|
|
|
return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Checks whether the $version represents any possible version. |
69
|
|
|
* |
70
|
|
|
* @param string $version |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
|
|
static public function isEmpty($version) |
|
|
|
|
74
|
|
|
{ |
75
|
1 |
|
return $version === '' || $version === '*' || $version === '>=0.0.0'; |
76
|
|
|
} |
77
|
1 |
|
|
78
|
|
|
static public function findMax(array $versions) |
|
|
|
|
79
|
|
|
{ |
80
|
1 |
|
$versions = array_unique(array_values($versions)); |
81
|
|
|
if (count($versions)<2) { |
82
|
1 |
|
return reset($versions); |
83
|
1 |
|
} |
84
|
|
|
$max = $versions[0]; |
85
|
|
|
$maxNum = static::toNum($max); |
86
|
1 |
|
for ($i=1; $i<= count($versions); $i++) { |
|
|
|
|
87
|
1 |
|
$cur = $versions[$i]; |
88
|
1 |
|
$curNum = static::toNum($cur); |
89
|
1 |
|
if ($curNum > $maxNum) { |
90
|
1 |
|
$max = $cur; |
91
|
1 |
|
$maxNum = $curNum; |
92
|
1 |
|
} |
93
|
1 |
|
} |
94
|
1 |
|
|
95
|
1 |
|
return $max; |
96
|
|
|
} |
97
|
1 |
|
|
98
|
|
|
static public function toNum($version) |
|
|
|
|
99
|
|
|
{ |
100
|
2 |
|
$version = preg_replace('/[^0-9\.]/', '', $version); |
101
|
|
|
$nums = explode('.', $version); |
102
|
2 |
|
$n1 = isset($nums[0]) ? $nums[0] : 0; |
103
|
2 |
|
$n2 = isset($nums[1]) ? $nums[1] : 0; |
104
|
2 |
|
$n3 = isset($nums[2]) ? $nums[2] : 0; |
105
|
2 |
|
|
106
|
2 |
|
return (($n1*1000) + $n2)*1000 + $n3; |
107
|
|
|
} |
108
|
2 |
|
|
109
|
|
|
} |
110
|
|
|
|