| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of composer/semver. |
| 5 |
* |
| 6 |
* (c) Composer <https://github.com/composer> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view |
| 9 |
* the LICENSE file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Composer\Semver; |
| 13 |
|
| 14 |
use Composer\Semver\Constraint\Constraint; |
| 15 |
use Composer\Semver\Constraint\ConstraintInterface; |
| 16 |
|
| 17 |
/** |
| 18 |
* Helper class to evaluate constraint by compiling and reusing the code to evaluate |
| 19 |
*/ |
| 20 |
class CompilingMatcher |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @var array |
| 24 |
* @phpstan-var array<string, callable> |
| 25 |
*/ |
| 26 |
private static $compiledCheckerCache = array(); |
| 27 |
/** |
| 28 |
* @var array |
| 29 |
* @phpstan-var array<string, bool> |
| 30 |
*/ |
| 31 |
private static $resultCache = array(); |
| 32 |
|
| 33 |
/** @var bool */ |
| 34 |
private static $enabled; |
| 35 |
|
| 36 |
/** |
| 37 |
* @phpstan-var array<Constraint::OP_*, Constraint::STR_OP_*> |
| 38 |
*/ |
| 39 |
private static $transOpInt = array( |
| 40 |
Constraint::OP_EQ => Constraint::STR_OP_EQ, |
| 41 |
Constraint::OP_LT => Constraint::STR_OP_LT, |
| 42 |
Constraint::OP_LE => Constraint::STR_OP_LE, |
| 43 |
Constraint::OP_GT => Constraint::STR_OP_GT, |
| 44 |
Constraint::OP_GE => Constraint::STR_OP_GE, |
| 45 |
Constraint::OP_NE => Constraint::STR_OP_NE, |
| 46 |
); |
| 47 |
|
| 48 |
/** |
| 49 |
* Clears the memoization cache once you are done |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public static function clear() |
| 54 |
{ |
| 55 |
self::$resultCache = array(); |
| 56 |
self::$compiledCheckerCache = array(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Evaluates the expression: $constraint match $operator $version |
| 61 |
* |
| 62 |
* @param ConstraintInterface $constraint |
| 63 |
* @param int $operator |
| 64 |
* @phpstan-param Constraint::OP_* $operator |
| 65 |
* @param string $version |
| 66 |
* |
| 67 |
* @return bool |
| 68 |
*/ |
| 69 |
public static function match(ConstraintInterface $constraint, $operator, $version) |
| 70 |
{ |
| 71 |
$resultCacheKey = $operator.$constraint.';'.$version; |
| 72 |
|
| 73 |
if (isset(self::$resultCache[$resultCacheKey])) { |
| 74 |
return self::$resultCache[$resultCacheKey]; |
| 75 |
} |
| 76 |
|
| 77 |
if (self::$enabled === null) { |
| 78 |
self::$enabled = !\in_array('eval', explode(',', (string) ini_get('disable_functions')), true); |
| 79 |
} |
| 80 |
if (!self::$enabled) { |
| 81 |
return self::$resultCache[$resultCacheKey] = $constraint->matches(new Constraint(self::$transOpInt[$operator], $version)); |
| 82 |
} |
| 83 |
|
| 84 |
$cacheKey = $operator.$constraint; |
| 85 |
if (!isset(self::$compiledCheckerCache[$cacheKey])) { |
| 86 |
$code = $constraint->compile($operator); |
| 87 |
self::$compiledCheckerCache[$cacheKey] = $function = eval('return function($v, $b){return '.$code.';};'); |
| 88 |
} else { |
| 89 |
$function = self::$compiledCheckerCache[$cacheKey]; |
| 90 |
} |
| 91 |
|
| 92 |
return self::$resultCache[$resultCacheKey] = $function($version, strpos($version, 'dev-') === 0); |
| 93 |
} |
| 94 |
} |