-
Notifications
You must be signed in to change notification settings - Fork 5
/
version-constraints.php
125 lines (103 loc) · 3.63 KB
/
version-constraints.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* This file is part of Dockerize, a PHP Experts, Inc., Project.
*
* Copyright © 2024 PHP Experts, Inc.
* Author: Theodore R. Smith <theodore@phpexperts.pro>
* GPG Fingerprint: 4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690
* https://www.phpexperts.pro/
* https://github.com/PHPExpertsInc/dockerize
*
* This file is licensed under the MIT License.
*/
class PhpVersionExtractor
{
private $allVersions = ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3'];
private $composerJson;
public $versionConstraint;
public function __construct($composerJson)
{
$this->composerJson = $composerJson;
}
public function getPhpVersionConstraint()
{
if ($this->versionConstraint === null) {
$this->getPhpVersions();
}
return $this->versionConstraint;
}
public function getPhpVersions()
{
$phpConstraint = $this->extractPhpConstraint();
$this->versionConstraint = $phpConstraint;
if (is_null($phpConstraint)) {
return implode(' ', $this->allVersions);
}
$selectedVersions = [];
foreach ($this->allVersions as $version) {
if ($this->versionSatisfies($version, $phpConstraint)) {
$selectedVersions[] = $version;
}
}
return implode(' ', $selectedVersions);
}
private function extractPhpConstraint()
{
if (!is_readable($this->composerJson)) {
return null;
}
$data = json_decode(file_get_contents($this->composerJson), true);
if (isset($data['require']) && isset($data['require']['php'])) {
return $data['require']['php'];
}
return null;
}
private function versionSatisfies($version, $constraints)
{
$constraints = str_replace(' ', '', $constraints);
$constraints = explode(',', $constraints);
foreach ($constraints as $constraint) {
if (preg_match('/^(>=|<=|>|<|=|~|\^)?(\d+\.\d+)/', $constraint, $matches)) {
$operator = isset($matches[1]) ? $matches[1] : '=';
$constraintVersion = $matches[2];
if (!$this->compareVersion($version, $operator, $constraintVersion)) {
return false;
}
}
}
return true;
}
private function compareVersion($version, $operator, $constraintVersion)
{
switch ($operator) {
case '>=':
return version_compare($version, $constraintVersion, '>=');
case '<=':
return version_compare($version, $constraintVersion, '<=');
case '>':
return version_compare($version, $constraintVersion, '>');
case '<':
return version_compare($version, $constraintVersion, '<');
case '~':
case '^':
return version_compare($version, $constraintVersion, '>=') && version_compare($version, $this->getUpperVersionLimit($constraintVersion, $operator), '<');
case '=':
default:
return version_compare($version, $constraintVersion, '==');
}
}
private function getUpperVersionLimit($version, $operator)
{
list($major, $minor) = explode('.', $version);
if ($operator === '^') {
return ($major + 1) . '.0';
} elseif ($operator === '~') {
return $major . '.' . ($minor + 1);
}
return $version;
}
}
// Usage example
$composerJson = 'composer.json';
$extractor = new PhpVersionExtractor($composerJson);
echo $extractor->getPhpVersions();