-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConfigurePhpCompatibleVersionCommand.php
More file actions
161 lines (131 loc) · 5.21 KB
/
ConfigurePhpCompatibleVersionCommand.php
File metadata and controls
161 lines (131 loc) · 5.21 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
declare(strict_types=1);
namespace App\Command\Configure\Php;
use App\{
Command\AbstractCommand,
Command\Behavior\GetComposerConfigurationTrait,
PhpVersion\PhpVersion,
PhpVersion\PhpVersionArray,
Utils\Path
};
use Symfony\Component\Console\Input\InputOption;
final class ConfigurePhpCompatibleVersionCommand extends AbstractCommand
{
use GetComposerConfigurationTrait;
/** @var string */
protected static $defaultName = 'configure:php:compatible-version';
protected function configure(): void
{
parent::configure();
$this
->setDescription('Create configuration for each compatible PHP version')
->addOption(
'php-versions',
null,
InputOption::VALUE_REQUIRED,
'PHP version separated by "," (exemple: 7.1,7.2)'
);
}
protected function doExecute(): int
{
$this->outputTitle('Configuration of PHP compatibles versions');
$compatiblesPhpVersions = $this->getCompatiblesPhpVersions();
if ($compatiblesPhpVersions === null || $compatiblesPhpVersions->count() === 0) {
$compatiblesPhpVersions = $this->askCompatiblesPhpVersions();
}
foreach (PhpVersion::getAll() as $phpVersion) {
$phpConfigurationPath = Path::getPhpConfigurationPath($phpVersion);
if ($compatiblesPhpVersions->exists($phpVersion)) {
$this
->createDirectory($phpConfigurationPath)
->writeFileFromTemplate(Path::rmPrefix($phpConfigurationPath) . '/php.ini');
if ($phpVersion->isPreloadAvailable()) {
$this->writeFileFromTemplate(Path::rmPrefix(Path::getPreloadPath($phpVersion)));
}
} else {
$this->removeDirectory($phpConfigurationPath);
}
}
return 0;
}
private function getCompatiblesPhpVersions(): ?PhpVersionArray
{
$phpVersions = $this->getInput()->getOption('php-versions');
if (is_string($phpVersions)) {
$return = new PhpVersionArray();
foreach (explode(',', $phpVersions) as $phpVersion) {
[$major, $minor] = explode('.', $phpVersion);
$return[] = new PhpVersion((int) $major, (int) $minor);
}
return $return;
}
$composerConfiguration = $this->getComposerConfiguration();
/** @var string|null $phpVersionConfiguration */
$phpVersionConfiguration = $composerConfiguration['require']['php'] ?? null;
if (is_string($phpVersionConfiguration) === false) {
return null;
}
$versionModifier = $this->getVersionModifier($phpVersionConfiguration);
if (is_string($versionModifier)) {
$phpVersionConfiguration = ltrim($phpVersionConfiguration, $versionModifier);
}
if (
preg_match('/^([0-9]).([0-9])$/', $phpVersionConfiguration, $phpVersionParts) === 1
|| preg_match('/^([0-9]).([0-9]).[0-9*]$/', $phpVersionConfiguration, $phpVersionParts) === 1
) {
$major = (int) $phpVersionParts[1];
$minor = (int) $phpVersionParts[2];
if ($versionModifier === '^') {
return $this->getPhpVersionsFromCarretVersionRange($major, $minor);
} elseif ($versionModifier === null) {
return $this->getPhpVersionsFromNullModifier($major, $minor, $phpVersionConfiguration);
}
}
return null;
}
private function askCompatiblesPhpVersions(): PhpVersionArray
{
$return = new PhpVersionArray();
foreach (PhpVersion::getAll() as $phpVersion) {
if ($this->askConfirmationQuestion('Is PHP ' . $phpVersion->toString() . ' compatible?')) {
$return[] = $phpVersion;
}
}
return $return;
}
private function getVersionModifier(string $version): ?string
{
$versionModifier = substr($version, 0, 1);
return $versionModifier === '^' || $versionModifier === '~' ? $versionModifier : null;
}
private function getPhpVersionsFromNullModifier(
int $major,
int $minor,
string $phpVersionConfiguration
): PhpVersionArray {
$phpVersion = new PhpVersion($major, $minor);
if (PhpVersion::getAll()->exists($phpVersion) === false) {
throw new \Exception(
'PHP version ' . $phpVersionConfiguration . ' is not compatible with Benchmark kit.'
. ' Compatibles PHP versions: ' . PhpVersion::getAll()->toString() . '.'
);
}
return new PhpVersionArray([$phpVersion]);
}
private function getPhpVersionsFromCarretVersionRange(int $major, int $minor): PhpVersionArray
{
$return = new PhpVersionArray();
foreach (PhpVersion::getAll() as $phpVersion) {
if (
$phpVersion->getMajor() > $major
|| (
$phpVersion->getMajor() === $major
&& $phpVersion->getMinor() >= $minor
)
) {
$return[] = $phpVersion;
}
}
return $return;
}
}