-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,476 additions
and
699 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,20 @@ | ||
Version Helper for PHP projects | ||
=============================== | ||
|
||
This module normalize versions as Composer do. Parse constraints as Composer do too. | ||
And tests if a version match a constraint. | ||
|
||
Example: | ||
|
||
```php | ||
<?php | ||
|
||
$parser = new \Version\VersionParser(); | ||
|
||
echo $parser->parseStability('1.2-RC2'); // RC | ||
echo $parser->parseStability('2.0b'); // beta | ||
echo $parser->parseConstraints('1.0'); // stable | ||
|
||
echo $parser->normalize('2.0b1'); // 2.0.0.0-beta1 | ||
|
||
$c = $parser->parseConstraints('>=1.2.5,<2.0'); | ||
echo $c->match('1.2.0'); // false | ||
echo $c->match('1.5'); // true | ||
echo $c->match('2.0'); // false | ||
?> | ||
``` | ||
Versions and Constraints for PHP | ||
================================ | ||
|
||
This library parse versions, | ||
E.x.: | ||
<code>1.0.0</code> | ||
<code>1.0.2-stable</code> | ||
<code>1.0.20-alpha2</code>. | ||
It can parse constraints (like Composer versions), | ||
E.x.: | ||
<code>>=1.0 >=1.0,<2.0 >=1.0,<1.1 | >=1.2</code>, | ||
<code>1.0.*</code>, | ||
<code>~1.2</code>. | ||
|
||
The goal of that is to let you check if a version matches a constraint, | ||
or to check if a constraint is a subset of another constraint. | ||
|
||
All that is done to let us select which version is compatible with a user constraints. | ||
|
||
It works with the same rules of Composer versioning. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
<?php | ||
|
||
namespace Version; | ||
|
||
use Version\Constraint\AnythingConstraint; | ||
use Version\Constraint\MultiConstraint; | ||
use Version\Constraint\SimpleConstraint; | ||
|
||
abstract class Constraint | ||
{ | ||
public abstract function matches(Constraint $constraint); | ||
|
||
/** | ||
* @param string $input | ||
* @return Constraint | ||
*/ | ||
public static function parse($input) | ||
{ | ||
$input = trim($input); | ||
|
||
if (strlen($input) == 0) { | ||
throw new \UnexpectedValueException('Empty.'); | ||
} | ||
|
||
$input = explode(',', $input); | ||
if (count($input) > 1) { | ||
$and = true; | ||
} else { | ||
$input = explode('|', $input[0]); | ||
$and = false; | ||
} | ||
|
||
if (count($input) > 1) { | ||
$constraints = array(); | ||
foreach ($input as $constraint) { | ||
$constraints[] = self::parse($constraint); | ||
} | ||
return new MultiConstraint($constraints, $and); | ||
} | ||
|
||
$input = $input[0]; | ||
|
||
$regex = '/^' . | ||
'(?:([\*|x])\.)?' . | ||
'(?:([\*|x])\.)?' . | ||
'(?:([\*|x])\.)?' . | ||
'(?:([\*|x]))?' . | ||
'$/'; | ||
|
||
if (preg_match($regex, $input, $matches)) { | ||
return new AnythingConstraint(); | ||
} | ||
|
||
$regex = '/^' . | ||
'(?:(' . Operator::REGEX . '))? *' . | ||
'(?:(\d+|\*|x)\.)?' . | ||
'(?:(\d+|\*|x)\.)?' . | ||
'(?:(\d+|\*|x)\.)?' . | ||
'(?:(\d+|\*|x))?' . | ||
'(?:' . Stability::REGEX . ')?' . | ||
'$/'; | ||
|
||
if(!preg_match($regex, $input, $matches)) { | ||
throw new \UnexpectedValueException('Invalid type: ' . $input); | ||
} | ||
|
||
if (isset($matches[1]) && strlen($matches[1]) > 0) { | ||
$operator = $matches[1]; | ||
} else { | ||
$operator = '='; | ||
} | ||
$operator = new Operator($operator); | ||
|
||
$parts = array(); | ||
|
||
if (isset($matches[2]) && strlen($matches[2]) > 0) $parts[] = $matches[2]; | ||
if (isset($matches[3]) && strlen($matches[3]) > 0) $parts[] = $matches[3]; | ||
if (isset($matches[4]) && strlen($matches[4]) > 0) $parts[] = $matches[4]; | ||
if (isset($matches[5]) && strlen($matches[5]) > 0) $parts[] = $matches[5]; | ||
|
||
if ((string)$operator == '~') { | ||
$end = count($parts); | ||
} else { | ||
$end = null; | ||
} | ||
|
||
while (count($parts) < 4) { | ||
$parts[] = 0; | ||
} | ||
|
||
$max = $parts; | ||
|
||
if ($end) { | ||
if ($end == 1) { | ||
$max[0]++; | ||
} elseif ($end == 2) { | ||
$max[0]++; | ||
$max[1] = 0; | ||
} elseif ($end == 3) { | ||
$max[1]++; | ||
$max[2] = 0; | ||
} elseif ($end == 4) { | ||
$max[2]++; | ||
$max[3] = 0; | ||
} else { | ||
echo $end; | ||
die($end); | ||
} | ||
} | ||
|
||
if ($parts[3] === 'x' || $parts[3] === '*') { | ||
$parts[3] = 0; | ||
$max[3] = 0; | ||
$max[2]++; | ||
} | ||
|
||
if ($parts[2] === 'x' || $parts[2] === '*') { | ||
$parts[2] = 0; | ||
$max[2] = 0; | ||
$max[1]++; | ||
} | ||
|
||
if ($parts[1] === 'x' || $parts[1] === '*') { | ||
$parts[1] = 0; | ||
$max[1] = 0; | ||
$max[0]++; | ||
} | ||
|
||
$version = new Version($parts[0]); | ||
if (isset($parts[1])) $version->setMinor($parts[1]); | ||
if (isset($parts[2])) $version->setRevision($parts[2]); | ||
if (isset($parts[3])) $version->setMicro($parts[3]); | ||
|
||
if (isset($matches[6]) && strlen($matches[6]) > 0) { | ||
if (strtolower($matches[5]) == 'rc') { | ||
$stability = 'RC'; | ||
} elseif (in_array(strtolower($matches[6]), array('pl', 'patch', 'p'))) { | ||
$stability = 'patch'; | ||
} elseif (in_array(strtolower($matches[6]), array('beta', 'b'))) { | ||
$stability = 'beta'; | ||
} elseif (strtolower($matches[6]) == 'stable') { | ||
$stability = 'stable'; | ||
} else { | ||
throw new \UnexpectedValueException('Invalid type: ' . $input); | ||
} | ||
$version->setStability(new Stability($stability, $matches[7])); | ||
} | ||
|
||
foreach ($parts as $k => $v) { | ||
if ($v != $max[$k]) { | ||
if ($input == '<=1.2.3') { | ||
print_r($parts); | ||
print_r($max); | ||
print_r(array_diff($parts, $max)); | ||
die; | ||
} | ||
$maxVersion = new Version($max[0]); | ||
if (isset($max[1])) $maxVersion->setMinor($max[1]); | ||
if (isset($max[2])) $maxVersion->setRevision($max[2]); | ||
if (isset($max[3])) $maxVersion->setMicro($max[3]); | ||
|
||
if ((string)$version == '0.0.0.0') { | ||
return new SimpleConstraint(new Operator('<'), $maxVersion); | ||
} | ||
if (isset($matches[6]) && strtolower($matches[6]) == 'stable') { | ||
$version->setStability(new Stability()); | ||
} | ||
return new MultiConstraint(array( | ||
new SimpleConstraint(new Operator('>='), $version), | ||
new SimpleConstraint(new Operator('<'), $maxVersion) | ||
)); | ||
} | ||
} | ||
|
||
return new SimpleConstraint($operator, $version); | ||
} | ||
|
||
abstract public function isSubsetOf(Constraint $constraint); | ||
|
||
public function isIncluding(Constraint $constraint) | ||
{ | ||
return $constraint->isSubsetOf($this); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Version\Constraint; | ||
|
||
use Version\Constraint; | ||
|
||
class AnythingConstraint extends Constraint | ||
{ | ||
public function __toString() | ||
{ | ||
return '*'; | ||
} | ||
|
||
public function matches(Constraint $constraint) | ||
{ | ||
return true; | ||
} | ||
|
||
public function isSubsetOf(Constraint $constraint) | ||
{ | ||
throw new \Exception('Constraint comparison of * with constraint ' . | ||
$constraint . ' Not implemented yet'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.