|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HumbugBox451\Composer\Semver; |
|
4
|
|
|
|
|
5
|
|
|
use HumbugBox451\Composer\Semver\Constraint\Constraint; |
|
6
|
|
|
use HumbugBox451\Composer\Semver\Constraint\ConstraintInterface; |
|
7
|
|
|
/** @internal */ |
|
8
|
|
|
class CompilingMatcher |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
@phpstan-var |
|
12
|
|
|
*/ |
|
13
|
|
|
private static $compiledCheckerCache = array(); |
|
14
|
|
|
/** |
|
15
|
|
|
@phpstan-var |
|
16
|
|
|
*/ |
|
17
|
|
|
private static $resultCache = array(); |
|
18
|
|
|
private static $enabled; |
|
19
|
|
|
/** |
|
20
|
|
|
@phpstan-var |
|
21
|
|
|
*/ |
|
22
|
|
|
private static $transOpInt = array(Constraint::OP_EQ => Constraint::STR_OP_EQ, Constraint::OP_LT => Constraint::STR_OP_LT, Constraint::OP_LE => Constraint::STR_OP_LE, Constraint::OP_GT => Constraint::STR_OP_GT, Constraint::OP_GE => Constraint::STR_OP_GE, Constraint::OP_NE => Constraint::STR_OP_NE); |
|
23
|
|
|
public static function clear() |
|
24
|
|
|
{ |
|
25
|
|
|
self::$resultCache = array(); |
|
26
|
|
|
self::$compiledCheckerCache = array(); |
|
27
|
|
|
} |
|
28
|
|
|
/** |
|
29
|
|
|
@phpstan-param |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function match(ConstraintInterface $constraint, $operator, $version) |
|
32
|
|
|
{ |
|
33
|
|
|
$resultCacheKey = $operator . $constraint . ';' . $version; |
|
34
|
|
|
if (isset(self::$resultCache[$resultCacheKey])) { |
|
35
|
|
|
return self::$resultCache[$resultCacheKey]; |
|
36
|
|
|
} |
|
37
|
|
|
if (self::$enabled === null) { |
|
38
|
|
|
self::$enabled = !\in_array('eval', \explode(',', (string) \ini_get('disable_functions')), \true); |
|
39
|
|
|
} |
|
40
|
|
|
if (!self::$enabled) { |
|
41
|
|
|
return self::$resultCache[$resultCacheKey] = $constraint->matches(new Constraint(self::$transOpInt[$operator], $version)); |
|
42
|
|
|
} |
|
43
|
|
|
$cacheKey = $operator . $constraint; |
|
44
|
|
|
if (!isset(self::$compiledCheckerCache[$cacheKey])) { |
|
45
|
|
|
$code = $constraint->compile($operator); |
|
46
|
|
|
self::$compiledCheckerCache[$cacheKey] = $function = eval('return function($v, $b){return ' . $code . ';};'); |
|
|
|
|
|
|
47
|
|
|
} else { |
|
48
|
|
|
$function = self::$compiledCheckerCache[$cacheKey]; |
|
49
|
|
|
} |
|
50
|
|
|
return self::$resultCache[$resultCacheKey] = $function($version, \strpos($version, 'dev-') === 0); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|