|
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
|
|
|
private static $parser; |
|
26
|
|
|
|
|
27
|
1 |
|
public static function getParser() |
|
28
|
|
|
{ |
|
29
|
1 |
|
if (static::$parser === null) { |
|
|
|
|
|
|
30
|
1 |
|
static::$parser = new VersionParser(); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
return static::$parser; |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public static 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 |
|
public static 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
|
|
|
} 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 |
|
public static function findMax(array $versions) |
|
65
|
|
|
{ |
|
66
|
1 |
|
$versions = array_values(array_unique($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
|
|
|
} |
|
76
|
|
|
} |
|
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
|
|
|
public static function isDisjunctive($constraint) |
|
88
|
|
|
{ |
|
89
|
|
|
return strpos($constraint, '|') !== false; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: