-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathGeneratePhp.php
More file actions
350 lines (315 loc) · 11.7 KB
/
GeneratePhp.php
File metadata and controls
350 lines (315 loc) · 11.7 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\CloudDocker\Command\Image;
use Composer\Semver\Semver;
use Magento\CloudDocker\App\ConfigurationMismatchException;
use Magento\CloudDocker\Cli;
use Magento\CloudDocker\Compose\Php\ExtensionResolver;
use Magento\CloudDocker\Compose\Php\ConfigFilesResolver;
use Magento\CloudDocker\Filesystem\DirectoryList;
use Magento\CloudDocker\Filesystem\FileNotFoundException;
use Magento\CloudDocker\Filesystem\Filesystem;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Generates PHP images.
*/
class GeneratePhp extends Command
{
private const NAME = 'image:generate:php';
private const SUPPORTED_VERSIONS = ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3'];
private const VERSION_MAP = [
'7.2' => '7.2',
'7.3' => '7.3',
'7.4' => '7.4',
'8.0' => '8.0.14',
'8.1' => '8.1.1',
'8.2' => '8.2',
'8.3' => '8.3'
];
private const EDITION_CLI = 'cli';
private const EDITION_FPM = 'fpm';
private const EDITIONS = [self::EDITION_CLI, self::EDITION_FPM];
private const ARGUMENT_VERSION = 'version';
private const DEFAULT_PACKAGES_PHP_FPM = [
'apt-utils',
'sendmail-bin',
'sendmail',
'sudo',
'iproute2',
'git',
];
private const DEFAULT_PACKAGES_PHP_CLI = [
'apt-utils',
'cron',
'git',
'mariadb-client',
'nano',
'nodejs',
'python3',
'python3-pip',
'redis-tools',
'sendmail-bin',
'sendmail',
'sudo',
'unzip',
'vim',
'openssh-client',
];
private const PHP_EXTENSIONS_ENABLED_BY_DEFAULT = [
'bcmath',
'bz2',
'calendar',
'exif',
'gd',
'gettext',
'imagick',
'intl',
'mysqli',
'mcrypt',
'pcntl',
'pdo_mysql',
'soap',
'sockets',
'sysvmsg',
'sysvsem',
'sysvshm',
'redis',
'opcache',
'xsl',
'zip',
'sodium'
];
private const DOCKERFILE = 'Dockerfile';
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var Semver
*/
private $semver;
/**
* @var DirectoryList
*/
private $directoryList;
/**
* @param Filesystem $filesystem
* @param Semver $semver
* @param DirectoryList $directoryList
*/
public function __construct(Filesystem $filesystem, Semver $semver, DirectoryList $directoryList)
{
$this->filesystem = $filesystem;
$this->semver = $semver;
$this->directoryList = $directoryList;
parent::__construct();
}
/**
* @inheritdoc
*/
protected function configure(): void
{
$this->setName(self::NAME)
->setDescription('Generates proper configs')
->addArgument(
self::ARGUMENT_VERSION,
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
'Generates PHP configuration',
self::SUPPORTED_VERSIONS
);
parent::configure();
}
/**
* {@inheritdoc}
*
* @throws ConfigurationMismatchException
* @throws FileNotFoundException
* @throws \Magento\CloudDocker\Filesystem\FileSystemException
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$versions = $input->getArgument(self::ARGUMENT_VERSION);
if ($diff = array_diff($versions, self::SUPPORTED_VERSIONS)) {
throw new ConfigurationMismatchException(sprintf(
'Not supported versions %s',
implode(' ', $diff)
));
}
foreach ($versions as $version) {
foreach (self::EDITIONS as $edition) {
$this->build($version, $edition);
}
}
$output->writeln('<info>Done</info>');
return Cli::SUCCESS;
}
/**
* @param string $version
* @param string $edition
* @throws ConfigurationMismatchException
* @throws FileNotFoundException
* @throws \Magento\CloudDocker\Filesystem\FileSystemException
*/
private function build(string $version, string $edition): void
{
$destination = $this->directoryList->getImagesRoot()
. '/php/'
. $version . '-' . $edition;
$dataDir = $this->directoryList->getImagesRoot() . '/php/' . $edition;
$dockerfile = $destination . '/' . self::DOCKERFILE;
$this->filesystem->deleteDirectory($destination);
$this->filesystem->makeDirectory($destination);
$this->filesystem->copyDirectory($dataDir, $destination, null, false);
foreach (ConfigFilesResolver::getFolders($edition) as $folder) {
$this->filesystem->copyDirectory(
$dataDir . '/' . $folder,
$destination . '/' . $folder
);
}
$this->filesystem->makeDirectory($destination . '/etc');
foreach (ConfigFilesResolver::getConfigFiles($edition) as $config) {
foreach ($config as $constraint => $paths) {
if (!$this->semver::satisfies($version, $constraint)) {
continue;
}
$this->filesystem->copy(
$dataDir . '/' . $paths[ConfigFilesResolver::FROM_PATH],
$destination . '/' . $paths[ConfigFilesResolver::TO_PATH]
);
}
}
$this->filesystem->put($dockerfile, $this->buildDockerfile($dockerfile, $version, $edition));
}
/**
* @param string $dockerfile
* @param string $phpVersion
* @param string $edition
* @return string
* @throws ConfigurationMismatchException
* @throws FileNotFoundException
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function buildDockerfile(string $dockerfile, string $phpVersion, string $edition): string
{
$phpExtConfigs = ExtensionResolver::getConfig();
$packages = self::EDITION_CLI === $edition ? self::DEFAULT_PACKAGES_PHP_CLI : self::DEFAULT_PACKAGES_PHP_FPM;
$phpExtCore = [];
$phpExtCoreConfigOptions = [];
$phpExtPecl = [];
$phpExtInstScripts = [];
$phpExtEnabledDefault = [];
foreach ($phpExtConfigs as $phpExtName => $phpExtConfig) {
if (!is_string($phpExtName)) {
throw new ConfigurationMismatchException('Extension name not set');
}
foreach ($phpExtConfig as $phpExtConstraint => $phpExtInstallConfig) {
if (!$this->semver::satisfies($phpVersion, $phpExtConstraint)) {
continue;
}
$phpExtType = $phpExtInstallConfig[ExtensionResolver::EXTENSION_TYPE];
switch ($phpExtType) {
case ExtensionResolver::EXTENSION_TYPE_CORE:
$phpExtCore[] = $phpExtInstallConfig[ExtensionResolver::EXTENSION_PACKAGE_NAME] ?? $phpExtName;
if (isset($phpExtInstallConfig[ExtensionResolver::EXTENSION_CONFIGURE_OPTIONS])) {
$phpExtCoreConfigOptions[] = sprintf(
"RUN docker-php-ext-configure \\\n %s %s",
$phpExtName,
implode(' ', $phpExtInstallConfig[ExtensionResolver::EXTENSION_CONFIGURE_OPTIONS])
);
}
break;
case ExtensionResolver::EXTENSION_TYPE_PECL:
$phpExtPecl[] = $phpExtInstallConfig[ExtensionResolver::EXTENSION_PACKAGE_NAME] ?? $phpExtName;
break;
case ExtensionResolver::EXTENSION_TYPE_INSTALLATION_SCRIPT:
$phpExtInstScripts[] = implode(" \\\n", array_map(static function (string $command) {
return strpos($command, 'RUN') === false ? ' && ' . $command : $command;
}, explode(
"\n",
'RUN ' . $phpExtInstallConfig[ExtensionResolver::EXTENSION_INSTALLATION_SCRIPT]
)));
break;
default:
throw new ConfigurationMismatchException(sprintf(
'PHP extension %s. The type %s not supported',
$phpExtName,
$phpExtType
));
}
if (isset($phpExtInstallConfig[ExtensionResolver::EXTENSION_OS_DEPENDENCIES])) {
$packages = array_merge(
$packages,
$phpExtInstallConfig[ExtensionResolver::EXTENSION_OS_DEPENDENCIES]
);
}
if (in_array($phpExtName, self::PHP_EXTENSIONS_ENABLED_BY_DEFAULT, true)) {
$phpExtEnabledDefault[] = $phpExtName;
}
}
}
if ($this->semver::satisfies($phpVersion, '>=8.2')) {
$packages[] = 'python3-yaml';
$pythonPackages = '';
} else {
$pythonPackages = 'RUN pip3 install --upgrade setuptools && pip3 install pyyaml';
}
$volumes = [
'root' => [
'def' => 'VOLUME ${MAGENTO_ROOT}',
'cmd' => 'RUN mkdir -p ${MAGENTO_ROOT}'
]
];
$volumesCmd = '';
$volumesDef = '';
foreach ($volumes as $data) {
$volumesCmd .= $data['cmd'] ? $data['cmd'] . "\n" : '';
$volumesDef .= $data['def'] ? $data['def'] . "\n" : '';
}
return strtr(
$this->filesystem->get($dockerfile),
[
'{%note%}' => '# This file is automatically generated. Do not edit directly. #',
'{%version%}' => self::VERSION_MAP[$phpVersion],
'{%composer_version%}' => $this->getComposerVersion($phpVersion),
'{%packages%}' => implode(" \\\n ", array_unique($packages)),
'{%docker-php-ext-configure%}' => implode(PHP_EOL, $phpExtCoreConfigOptions),
'{%docker-php-ext-install%}' => $phpExtCore
? "RUN docker-php-ext-install -j$(nproc) \\\n " . implode(" \\\n ", $phpExtCore)
: '',
'{%php-pecl-extensions%}' => $phpExtPecl
? "RUN pecl install -o -f \\\n " . implode(" \\\n ", $phpExtPecl)
: '',
'{%installation_scripts%}' => $phpExtInstScripts
? implode(PHP_EOL, $phpExtInstScripts)
: '',
'{%env_php_extensions%}' => $phpExtEnabledDefault
? 'ENV PHP_EXTENSIONS ' . implode(' ', $phpExtEnabledDefault)
: '',
'{%python_packages%}' => $pythonPackages,
'{%volumes_cmd%}' => rtrim($volumesCmd),
'{%volumes_def%}' => rtrim($volumesDef)
]
);
}
/**
* @param string $phpVersion
* @return string
*/
private function getComposerVersion(string $phpVersion) : string
{
if ($this->semver::satisfies($phpVersion, '<8.0')) {
return '1.10.22';
}
return $this->semver::satisfies($phpVersion, '<8.2') ? '2.1.14' : '2.2.18';
}
}