Inital commit
This commit is contained in:
73
core/lib/Thelia/Tools/Version/Constraints/BaseConstraint.php
Normal file
73
core/lib/Thelia/Tools/Version/Constraints/BaseConstraint.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* This file is part of the Thelia package. */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : dev@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||
/* file that was distributed with this source code. */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Tools\Version\Constraints;
|
||||
|
||||
/**
|
||||
* Class BaseConstraint
|
||||
* @package Thelia\Tools\Version\Constraints
|
||||
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
|
||||
*/
|
||||
abstract class BaseConstraint implements ConstraintInterface
|
||||
{
|
||||
protected $operator = "=";
|
||||
|
||||
protected $expression = null;
|
||||
|
||||
public function __construct($expression)
|
||||
{
|
||||
$this->expression = $expression;
|
||||
}
|
||||
|
||||
public function test($version, $strict = false)
|
||||
{
|
||||
$version = $this->normalize($version, $strict);
|
||||
|
||||
return version_compare($version, $this->expression, $this->operator);
|
||||
}
|
||||
|
||||
public function normalize($version, $strict = false)
|
||||
{
|
||||
return $strict ? $version : $this->normalizePrecision($version);
|
||||
}
|
||||
|
||||
protected function normalizePrecision($version, $changeExpression = true)
|
||||
{
|
||||
$expressionElements = preg_split('/[\.\-]/', $this->expression);
|
||||
// cleaning alpha RC beta
|
||||
$version = preg_replace('/\-.*$/', '', $version);
|
||||
$versionElements = preg_split('/\./', $version);
|
||||
|
||||
if (count($expressionElements) < count($versionElements)) {
|
||||
if (true === $changeExpression) {
|
||||
$this->expression = implode(
|
||||
'.',
|
||||
array_merge(
|
||||
$expressionElements,
|
||||
array_fill(
|
||||
count($expressionElements) - 1,
|
||||
count($versionElements) - count($expressionElements),
|
||||
'0'
|
||||
)
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$version = implode(
|
||||
'.',
|
||||
array_slice($versionElements, 0, count($expressionElements))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* This file is part of the Thelia package. */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : dev@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||
/* file that was distributed with this source code. */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Tools\Version\Constraints;
|
||||
|
||||
/**
|
||||
* Class ConstraintEqual
|
||||
* @package Thelia\Tools\Version\Constraints
|
||||
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
|
||||
*/
|
||||
class ConstraintEqual extends BaseConstraint
|
||||
{
|
||||
public function __construct($expression)
|
||||
{
|
||||
$this->expression = str_replace('=', '', $expression);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* This file is part of the Thelia package. */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : dev@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||
/* file that was distributed with this source code. */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Tools\Version\Constraints;
|
||||
|
||||
/**
|
||||
* Class ConstraintGreater
|
||||
* @package Thelia\Tools\Version\Constraints
|
||||
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
|
||||
*/
|
||||
class ConstraintGreater extends BaseConstraint
|
||||
{
|
||||
public function __construct($expression, $strict = false)
|
||||
{
|
||||
$this->operator = $strict ? ">" : ">=";
|
||||
$this->expression = substr(
|
||||
$expression,
|
||||
strlen($this->operator)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* This file is part of the Thelia package. */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : dev@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||
/* file that was distributed with this source code. */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Tools\Version\Constraints;
|
||||
|
||||
/**
|
||||
* Class ContraintInterface
|
||||
* @package Thelia\Tools\Version\Constraints
|
||||
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
|
||||
*/
|
||||
interface ConstraintInterface
|
||||
{
|
||||
/**
|
||||
* Normalize a version number in a version that will be used in `version_compare`
|
||||
*
|
||||
* @param string $version the version expression
|
||||
* @return string the normalized expression
|
||||
*/
|
||||
public function normalize($version, $strict = false);
|
||||
|
||||
/**
|
||||
* Test if the version number is valid
|
||||
*
|
||||
* @param string $version the version number
|
||||
* @param bool $strict if false precision will be normalized. eg: 2.1.0 > 2.1 will become 2.1.0 > 2.1.0 (default false)
|
||||
* @return bool true if the version is equal, otherwise false
|
||||
*/
|
||||
public function test($version, $strict = false);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* This file is part of the Thelia package. */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : dev@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||
/* file that was distributed with this source code. */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Tools\Version\Constraints;
|
||||
|
||||
/**
|
||||
* Class ConstraintLower
|
||||
* @package Thelia\Tools\Version\Constraints
|
||||
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
|
||||
*/
|
||||
class ConstraintLower extends BaseConstraint
|
||||
{
|
||||
public function __construct($expression, $strict = false)
|
||||
{
|
||||
$this->operator = $strict ? "<" : "<=";
|
||||
$this->expression = substr(
|
||||
$expression,
|
||||
strlen($this->operator)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* This file is part of the Thelia package. */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : dev@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||
/* file that was distributed with this source code. */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Tools\Version\Constraints;
|
||||
|
||||
/**
|
||||
* Class ConstraintNearlyEqual
|
||||
* @package Thelia\Tools\Version\Constraints
|
||||
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
|
||||
*/
|
||||
class ConstraintNearlyEqual extends BaseConstraint
|
||||
{
|
||||
public function __construct($expression)
|
||||
{
|
||||
$this->expression = str_replace('~', '', $expression);
|
||||
}
|
||||
|
||||
public function normalize($version, $strict = false)
|
||||
{
|
||||
if (!$strict) {
|
||||
$version = $this->normalizePrecision($version, false);
|
||||
}
|
||||
|
||||
return $version;
|
||||
}
|
||||
}
|
||||
134
core/lib/Thelia/Tools/Version/Version.php
Normal file
134
core/lib/Thelia/Tools/Version/Version.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* This file is part of the Thelia package. */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : dev@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||
/* file that was distributed with this source code. */
|
||||
/*************************************************************************************/
|
||||
|
||||
|
||||
namespace Thelia\Tools\Version;
|
||||
|
||||
use Thelia\Tools\Version\Constraints\ConstraintEqual;
|
||||
use Thelia\Tools\Version\Constraints\ConstraintGreater;
|
||||
use Thelia\Tools\Version\Constraints\ConstraintInterface;
|
||||
use Thelia\Tools\Version\Constraints\ConstraintLower;
|
||||
use Thelia\Tools\Version\Constraints\ConstraintNearlyEqual;
|
||||
|
||||
/**
|
||||
* Class Version
|
||||
* @package Thelia\Tools
|
||||
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
|
||||
*/
|
||||
class Version
|
||||
{
|
||||
/**
|
||||
* Test if a version matched the version contraints.
|
||||
*
|
||||
* constraints can be simple or complex (multiple constraints separated by space) :
|
||||
*
|
||||
* - "~2.1" : version 2.1.*
|
||||
* - "~2.1 <=2.1.4" : version 2.1.* but lower or equal to 2.1.4
|
||||
* - ">=2.1" : version 2.1.*, 2.2, ...
|
||||
* - ">2.1.1 <=2.1.5" : version greater than 2.1.1 but lower or equal than 2.1.5
|
||||
* - ...
|
||||
*
|
||||
* @param string $version the version to test
|
||||
* @param string $constraints the versions constraints
|
||||
* @param bool $strict if true 2.1 is different of 2.1.0, if false version are normalized so 2.1
|
||||
* will be expended to 2.1.0
|
||||
* @param string $defaultComparison the default comparison if nothing provided
|
||||
* @return bool true if version matches the constraints
|
||||
*/
|
||||
public static function test($version, $constraints, $strict = false, $defaultComparison = "=")
|
||||
{
|
||||
$constraints = self::parseConstraints($constraints, $defaultComparison);
|
||||
|
||||
/** @var ConstraintInterface $constraint */
|
||||
foreach ($constraints as $constraint) {
|
||||
if (!$constraint->test($version, $strict)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static function parseConstraints($constraints, $defaultComparison = "=")
|
||||
{
|
||||
$constraintsList = [];
|
||||
|
||||
foreach (explode(" ", $constraints) as $expression) {
|
||||
if (1 === preg_match('/^[0-9]/', $expression)) {
|
||||
$expression = $defaultComparison . $expression;
|
||||
}
|
||||
|
||||
if (strpos($expression, '>=') !== false) {
|
||||
$constraint = new ConstraintGreater($expression);
|
||||
} elseif (strpos($expression, '>') !== false) {
|
||||
$constraint = new ConstraintGreater($expression, true);
|
||||
} elseif (strpos($expression, '<=') !== false) {
|
||||
$constraint = new ConstraintLower($expression);
|
||||
} elseif (strpos($expression, '<') !== false) {
|
||||
$constraint = new ConstraintLower($expression, true);
|
||||
} elseif (strpos($expression, '~') !== false) {
|
||||
$constraint = new ConstraintNearlyEqual($expression);
|
||||
} else {
|
||||
$constraint = new ConstraintEqual($expression);
|
||||
}
|
||||
|
||||
$constraintsList[] = $constraint;
|
||||
}
|
||||
|
||||
return $constraintsList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Split a version into an associative array
|
||||
* [version, major, minus, release, extra]
|
||||
*
|
||||
* @param string $version the version to split
|
||||
* @return array associative array
|
||||
* [
|
||||
* 'version' => 'digit',
|
||||
* 'major' => 'digit',
|
||||
* 'minus' => 'digit',
|
||||
* 'release' => 'digit',
|
||||
* 'extra' => 'alphanumeric'
|
||||
* ]
|
||||
*/
|
||||
public static function parse($version = null)
|
||||
{
|
||||
if (is_null($version)) {
|
||||
$version = \Thelia\Core\Thelia::THELIA_VERSION;
|
||||
}
|
||||
|
||||
$pattern = "`^(?<version>
|
||||
(?<major>[0-9]+)\.
|
||||
(?<minus>[0-9]+)\.
|
||||
(?<release>[0-9]+)
|
||||
-?(?<extra>[a-zA-Z0-9]*) # extra_version will also match empty string
|
||||
)$`x";
|
||||
|
||||
if (!preg_match($pattern, $version, $match)) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf(
|
||||
"Invalid version number provided : %s".PHP_EOL,
|
||||
$version
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return [
|
||||
'version' => $match['version'],
|
||||
'major' => $match['major'],
|
||||
'minus' => $match['minus'],
|
||||
'release' => $match['release'],
|
||||
'extra' => $match['extra'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user