Skip to content

Commit

Permalink
Add PHP release version information and improve minimum requirements …
Browse files Browse the repository at this point in the history
…detection.
  • Loading branch information
schlessera committed Mar 31, 2016
1 parent 2f60ff4 commit c6d1c32
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 5 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"require": {
"xrstf/composer-php52": "^1",
"brightnucleus/config-52": "^0.1.0"
"brightnucleus/config-52": "^0.1",
"brightnucleus/php-releases": "^0.1"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
Expand Down
49 changes: 47 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/known_features.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
'generator-delegation' => '>=7.0.0',
'anonymous-classes' => '>=7.0.0',
'multiple-namespace-import' => '>=7.0.0',
'pcre-8.38' => '>=7.0.3',
'http-451' => '>=7.0.3',
'libzip-1.1.2' => '>=7.0.5',
'void-return-type' => '>=7.1.0',
'constant-visibility-modifiers' => '>=7.1.0',
);
117 changes: 115 additions & 2 deletions src/PHPFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ class PHPFeature implements FeatureInterface
*/
protected $version;

/**
* Reference to the PHP releases.
*
* @since 0.2.4
*
* @var PHPReleases
*/
protected $releases;

/**
* Instantiate a PHPFeature object.
*
Expand Down Expand Up @@ -241,6 +250,8 @@ protected function checkRequirement($requirement, &$minimumRequired = null)
/**
* Get the required version for a single requirement.
*
* @todo The entire algorithm is only an approximation. A 5.2 SemVer library is needed.
*
* @since 0.2.0
*
* @param string $milestone A version milestone that is used to define the requirement.
Expand All @@ -249,12 +260,114 @@ protected function checkRequirement($requirement, &$minimumRequired = null)
* '<>', 'ne'
*
* @return string Version string that meets a single requirement.
* @throws RuntimeException If the requirement could not be satisfied.
* @throws RuntimeException If the NotEqual is used.
*/
protected function getRequiredVersion($milestone, $operator)
{
if (null === $this->releases) {
$this->releases = new PHPReleases();
}

switch ($operator) {
case '>':
case 'gt':
return $this->getGreaterThanVersion($milestone);
case '<':
case 'lt':
return $this->getLesserThanVersion($milestone);
case '>=':
case 'ge':
return $this->getGreaterEqualVersion($milestone);
case '<=':
case 'le':
return $this->getLesserEqualVersion($milestone);
case '!=':
case '<>':
case 'ne':
throw new RuntimeException('NotEqual operator is not implemented.');
}

// TODO: Algorithm is still missing, the `$operator` is simply ignored
// and the pure `$milestone` is returned.
return $milestone;
}

/**
* Get a version greater than the milestone.
*
* @since 0.2.4
*
* @param string $milestone A version milestone that is used to define the requirement.
*
* @return string Version number that meets the requirement.
* @throws RuntimeException If the requirement could not be satisfied.
*/
protected function getGreaterThanVersion($milestone)
{
$data = $this->releases->getAll();
foreach ($data as $version => $date) {
if (version_compare($version, $milestone, '>')) {
return $version;
}
}

throw new RuntimeException('Could not satisfy version requirements.');
}

/**
* Get a version lesser than the milestone.
*
* @since 0.2.4
*
* @param string $milestone A version milestone that is used to define the requirement.
*
* @return string Version number that meets the requirement.
* @throws RuntimeException If the requirement could not be satisfied.
*/
protected function getLesserThanVersion($milestone)
{
$data = $this->releases->getAll();
foreach ($data as $version => $date) {
if (version_compare($version, $milestone, '<')) {
return $version;
}
}

throw new RuntimeException('Could not satisfy version requirements.');
}

/**
* Get a version greater or equal than the milestone.
*
* @since 0.2.4
*
* @param string $milestone A version milestone that is used to define the requirement.
*
* @return string Version number that meets the requirement.
*/
protected function getGreaterEqualVersion($milestone)
{
if ($this->releases->exists($milestone)) {
return $milestone;
}

return $this->getGreaterThanVersion($milestone);
}

/**
* Get a version lesser or equal than the milestone.
*
* @since 0.2.4
*
* @param string $milestone A version milestone that is used to define the requirement.
*
* @return string Version number that meets the requirement.
*/
protected function getLesserEqualVersion($milestone)
{
if ($this->releases->exists($milestone)) {
return $milestone;
}

return $this->getLesserThanVersion($milestone);
}
}

0 comments on commit c6d1c32

Please sign in to comment.