forked from phpbrew/phpbrew
/
InstallCommand.php
510 lines (406 loc) · 19.4 KB
/
InstallCommand.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<?php
namespace PhpBrew\Command;
use Exception;
use PhpBrew\Config;
use PhpBrew\VariantParser;
use PhpBrew\VariantBuilder;
use PhpBrew\Tasks\DownloadTask;
use PhpBrew\Tasks\PrepareDirectoryTask;
use PhpBrew\Tasks\MakeTask;
use PhpBrew\Tasks\InstallTask;
use PhpBrew\Tasks\ExtractTask;
use PhpBrew\Tasks\ConfigureTask;
use PhpBrew\Tasks\BuildTask;
use PhpBrew\Tasks\DSymTask;
use PhpBrew\Tasks\TestTask;
use PhpBrew\Build;
use PhpBrew\ReleaseList;
use PhpBrew\VersionDslParser;
use PhpBrew\BuildSettings\DefaultBuildSettings;
use PhpBrew\Distribution\DistributionUrlPolicy;
use PhpBrew\Exception\SystemCommandException;
use CLIFramework\ValueCollection;
use CLIFramework\Command;
class InstallCommand extends Command
{
public function brief()
{
return 'Install php';
}
public function aliases()
{
return array('i','ins');
}
public function usage()
{
return 'phpbrew install [php-version] ([+variant...])';
}
public function arguments($args) {
$args->add('version')->suggestions(function() {
$releaseList = ReleaseList::getReadyInstance();
$releases = $releaseList->getReleases();
$collection = new ValueCollection;
foreach($releases as $major => $versions) {
$collection->group($major, "PHP $major", array_keys($versions));
}
$collection->group('pseudo', 'pseudo', array('latest', 'next'));
return $collection;
});
$args->add('variants')->multiple()->suggestions(function() {
$variants = new VariantBuilder;
$list = $variants->getVariantNames();
sort($list);
return array_map(function($n) { return '+' . $n; }, $list);
});
}
public function parseSemanticOptions(array &$args)
{
$settings = array();
$definitions = array(
'as' => '*',
'like' => '*',
'using' => '*+',
);
// XXX: support 'using'
foreach ($definitions as $k => $requirement) {
$idx = array_search($k, $args);
if ($idx !== false) {
if ($requirement == '*') {
// Find the value next to the position
list($key, $val) = array_splice($args, $idx, 2);
$settings[$key] = $val;
} elseif ($requirement == '*+') {
$values = array_splice($args, $idx, 2);
$key = array_shift($values);
$settings[$key] = $values;
}
}
}
return $settings;
}
/**
* @param \GetOptionKit\OptionCollection $opts
*/
public function options($opts)
{
$opts->add('test', 'Run tests after the installation.');
$opts->add('name:', 'The name of the installation. By default the installed path is equal to the release version name (php-5.x.x), however you can specify a custom name instead of the default `php-5.x.x`. For example, `myphp-5.3.2-dbg`')
->valueName('name');
$opts->add('mirror:', 'Use specified mirror site. phpbrew will download the files from [mirror]/distributions/*');
$opts->add('post-clean', 'Run make clean after the installation.');
$opts->add('production', 'Use production configuration file. this installer will copy the php-production.ini into the etc directory.');
$opts->add('build-dir:','Specify the build directory. '
. 'the distribution tarball will be extracted to the directory you specified '
. 'instead of $PHPBREW_ROOT/build/{name}.')
->isa('dir')
;
$opts->add('root','specify phpbrew root instead of PHPBREW_ROOT');
$opts->add('home','specify phpbrew home instead of PHPBREW_HOME');
$opts->add('no-clean', 'Do not clean previously compiled objects before building PHP. '
. 'By default phpbrew will run `make clean` before running the configure script '
. 'to ensure everything is cleaned up.')
;
$opts->add('no-patch', 'Do not apply any patch');
$opts->add('no-configure', 'Do not run configure script');
$opts->add('no-install', 'Do not install, just run build the target');
$opts->add('n|nice:', 'Runs build processes at an altered scheduling priority. The priority can be adjusted over a range of -20 (the highest) to 20 (the lowest).')
->valueName('priority');
$opts->add('patch+:', 'Apply patch before build.')
->isa('file');
$opts->add('old', 'Install phpbrew incompatible phps (< 5.3)');
$opts->add('http-proxy:', 'The HTTP Proxy to download PHP distributions. e.g. --http-proxy=22.33.44.55:8080')
->valueName('proxy host')
;
$opts->add('http-proxy-auth:', 'The HTTP Proxy Auth to download PHP distributions. user:pass')
->valueName('user:pass')
;
$opts->add('user-config','Allow users create their own config file (php.ini or extension config init files)');
$opts->add('connect-timeout:', 'The system aborts the command if downloading '
. 'of a php version not starts during this limit. This option '
. 'overrides a value of CONNECT_TIMEOUT environment variable.')
->valueName('seconds')
;
$opts->add('f|force', 'Force the installation (redownloads source).')
->defaultValue(false)
;
$opts->add('d|dryrun', 'Do not build, but run through all the tasks.');
$opts->add('like:', 'Inherit variants from an existing build. This option would require an existing build directory from the {version}.')
->valueName('version');
$opts->add('j|jobs:', 'Specifies the number of jobs to run build simultaneously (make -jN).')
->valueName('concurrent job number')
;
$opts->add('stdout', 'Outputs install logs to stdout.');
}
public function execute($version)
{
$distUrl = NULL;
$versionInfo = array();
$releaseList = ReleaseList::getReadyInstance();
$versionDslParser = new VersionDslParser();
$clean = new MakeTask($this->logger, $this->options);
$clean->setQuiet();
if ($root = $this->options->root) {
Config::setPhpbrewRoot($root);
}
if ($home = $this->options->home) {
Config::setPhpbrewHome($home);
}
if ('latest' === strtolower($version)) {
$version = $releaseList->getLatestVersion();
}
// this should point to master or the latest version branch yet to be released
if ('next' === strtolower($version)) {
$version = "github.com/php/php-src:master";
}
if ($info = $versionDslParser->parse($version)) {
$version = $info['version'];
$distUrl = $info['url'];
// always redownload when installing from github master
// beware to keep this behavior after clean up the TODO below
$this->options['force']->setValue(true);
} else {
// TODO ↓ clean later ↓ d.d.d versions should be part of the DSL too
$version = preg_replace('/^php-/', '', $version);
$versionInfo = $releaseList->getVersion($version);
if (!$versionInfo) {
throw new Exception("Version $version not found.");
}
$version = $versionInfo['version'];
$distUrlPolicy = new DistributionUrlPolicy();
if ($this->options->mirror) {
$distUrlPolicy->setMirrorSite($this->options->mirror);
}
$distUrl = $distUrlPolicy->buildUrl($version, $versionInfo['filename']);
}
// get options and variants for building php
// and skip the first argument since it's the target version.
$args = func_get_args();
array_shift($args); // shift the version name
$semanticOptions = $this->parseSemanticOptions($args);
$buildAs = isset($semanticOptions['as']) ? $semanticOptions['as'] : $this->options->name;
$buildLike = isset($semanticOptions['like']) ? $semanticOptions['like'] : $this->options->like;
// convert patch to realpath
if ($this->options->patch) {
$patchPaths = array();
foreach ($this->options->patch as $patch) {
/** @var \SplFileInfo $patch */
$patchPath = realpath($patch);
if ($patchPath !== false) {
$patchPaths[(string) $patch] = $patchPath;
}
}
// rewrite patch paths
$this->options->keys['patch']->value = $patchPaths;
}
// Initialize the build object, contains the information to build php.
$build = new Build($version, $buildAs);
$installPrefix = Config::getInstallPrefix() . DIRECTORY_SEPARATOR . $build->getName();
if (!file_exists($installPrefix)) {
mkdir($installPrefix, 0755, true);
}
$build->setInstallPrefix($installPrefix);
// find inherited variants
if ($buildLike) {
if ($parentBuild = Build::findByName($buildLike)) {
$build->loadVariantInfo($parentBuild->settings->toArray());
}
}
$msg = "===> phpbrew will now build {$build->getVersion()}";
if ($buildLike) {
$msg .= ' using variants from ' . $buildLike;
}
if (isset($semanticOptions['using'])) {
$msg .= ' plus custom variants: ' . join(', ',$semanticOptions['using']);
$args = array_merge($args, $semanticOptions['using']);
}
if ($buildAs) {
$msg .= ' as ' . $buildAs;
}
$this->logger->info($msg);
if (!empty($args)) {
$this->logger->debug("---> Parsing variants from command arguments '" . join(' ', $args) . "'");
}
// ['extra_options'] => the extra options to be passed to ./configure command
// ['enabled_variants'] => enabeld variants
// ['disabled_variants'] => disabled variants
$variantInfo = VariantParser::parseCommandArguments($args);
$build->loadVariantInfo($variantInfo); // load again
// assume +default variant if no build config is given and warn about that
if (!$variantInfo['enabled_variants']) {
$build->setBuildSettings(new DefaultBuildSettings());
$this->logger->notice("You haven't set any variant. A default set of extensions will be installed for the minimum requirement:");
$this->logger->notice('[' . implode(', ', array_keys($build->getVariants())) . ']');
$this->logger->notice("Please run 'phpbrew variants' for more information.\n");
}
if (preg_match('/5\.3\./',$version)) {
$this->logger->notice("PHP 5.3 requires +intl, enabled by default.");
$build->enableVariant('intl');
}
// always add +xml by default unless --without-pear is present
// TODO: This can be done by "-pear"
if(! in_array('--without-pear', $variantInfo['extra_options'])){
$build->enableVariant('xml');
}
$this->logger->info('===> Loading and resolving variants...');
$removedVariants = $build->loadVariantInfo($variantInfo);
if (!empty($removedVariants)) {
$this->logger->debug('Removed variants: ' . join(',', $removedVariants));
}
{
$prepareTask = new PrepareDirectoryTask($this->logger, $this->options);
$prepareTask->run($build);
}
// Move to to build directory, because we are going to download distribution.
$buildDir = $this->options->{'build-dir'} ?: Config::getBuildDir();
if (!file_exists($buildDir)) {
mkdir($buildDir, 0755, true);
}
$variantBuilder = new VariantBuilder;
$configureOptions = $variantBuilder->build($build);
$distFileDir = Config::getDistFileDir();
$downloadTask = new DownloadTask($this->logger, $this->options);
$targetFilePath = $downloadTask->download($distUrl, $distFileDir, isset($versionInfo['md5']) ? $versionInfo['md5'] : NULL);
if (!file_exists($targetFilePath)) {
throw new Exception("Download failed, $targetFilePath does not exist.");
}
unset($downloadTask);
$extractTask = new ExtractTask($this->logger, $this->options);
$targetDir = $extractTask->extract($build, $targetFilePath, $buildDir);
if (!file_exists($targetDir)) {
throw new Exception("Extract failed, $targetDir does not exist.");
}
unset($extractTask);
// Update build source directory
$this->logger->debug('Source Directory: ' . realpath($targetDir));
$build->setSourceDirectory($targetDir);
if (!$this->options->{'no-clean'} && file_exists($targetDir . DIRECTORY_SEPARATOR . 'Makefile') ) {
$this->logger->info("Found existing Makefile, running make clean to ensure everything will be rebuilt.");
$this->logger->info("You can append --no-clean option after the install command if you don't want to rebuild.");
$clean->clean($build);
}
// Change directory to the downloaded source directory.
chdir($targetDir);
// Write variants info.
$variantInfoFile = $build->getInstallPrefix() . DIRECTORY_SEPARATOR . 'phpbrew.variants';
$this->logger->debug("Writing variant info to $variantInfoFile");
if ( false === $build->writeVariantInfoFile($variantInfoFile)) {
$this->logger->warn("Can't store variant info.");
}
$buildLogFile = $build->getBuildLogPath();
try {
if (!$this->options->{'no-configure'}) {
$configureTask = new ConfigureTask($this->logger, $this->options);
$configureTask->run($build, $configureOptions);
unset($configureTask); // trigger __destruct
}
{
$buildTask = new BuildTask($this->logger, $this->options);
$buildTask->run($build);
unset($buildTask); // trigger __destruct
}
if ($this->options->{'test'}) {
$testTask = new TestTask($this->logger, $this->options);
$testTask->run($build);
unset($testTask); // trigger __destruct
}
if (!$this->options->{'no-install'}) {
$installTask = new InstallTask($this->logger, $this->options);
$installTask->install($build);
unset($installTask); // trigger __destruct
}
if ($this->options->{'post-clean'}) {
$clean->clean($build);
}
/** POST INSTALLATION **/
{
$dsym = new DSymTask($this->logger, $this->options);
$dsym->patch($build, $this->options);
}
} catch (SystemCommandException $e) {
$buildLog = $e->getLogFile();
$this->logger->error("Error: " . $e->getMessage());
$this->logger->error("Configure options: " . join(' ', $configureOptions));
if (file_exists($buildLog)) {
$this->logger->error("Last 5 lines in the log file:");
$lines = array_slice(file($buildLog), -5);
foreach ($lines as $line) {
echo $line , PHP_EOL;
}
$this->logger->error("Please checkout the build log file for more details:");
$this->logger->error("\t tail $buildLog");
}
return;
}
// copy php-fpm config
$this->logger->info("---> Creating php-fpm.conf");
$etcDirectory = $build->getEtcDirectory();
$phpFpmConfigPath = "sapi/fpm/php-fpm.conf";
$phpFpmTargetConfigPath = $etcDirectory . DIRECTORY_SEPARATOR . 'php-fpm.conf';
if (file_exists($phpFpmConfigPath)) {
if (!file_exists($phpFpmTargetConfigPath)) {
copy($phpFpmConfigPath, $phpFpmTargetConfigPath);
} else {
$this->logger->notice("Found existing $phpFpmTargetConfigPath.");
}
}
$this->logger->info("---> Creating php.ini");
$phpConfigPath = $build->getSourceDirectory()
. DIRECTORY_SEPARATOR . ($this->options->production ? 'php.ini-production' : 'php.ini-development');
$this->logger->info("---> Copying $phpConfigPath ");
if (file_exists($phpConfigPath) && ! $this->options->dryrun) {
$targetConfigPath = $etcDirectory . DIRECTORY_SEPARATOR . 'php.ini';
if (file_exists($targetConfigPath)) {
$this->logger->notice("Found existing $targetConfigPath.");
} else {
// TODO: Move this to PhpConfigPatchTask
// move config file to target location
copy($phpConfigPath, $targetConfigPath);
}
if (!$this->options->{'no-patch'}) {
$config = parse_ini_file($targetConfigPath, true);
$configContent = file_get_contents($targetConfigPath);
$patched = false;
if (!isset($config['date']['timezone'])) {
$this->logger->info('---> Found date.timezone is not set, patching...');
// Replace current timezone
if ($timezone = ini_get('date.timezone')) {
$this->logger->info("---> Found date.timezone, patching config timezone with $timezone");
$configContent = preg_replace('/^;?date.timezone\s*=\s*.*/im', "date.timezone = $timezone", $configContent);
}
$patched = true;
}
if (!isset($config['phar']['readonly'])) {
$pharReadonly = ini_get('phar.readonly');
// 0 or "" means readonly is disabled manually
if (!$pharReadonly) {
$this->logger->info("---> Disabling phar.readonly option.");
$configContent = preg_replace('/^;?phar.readonly\s*=\s*.*/im', "phar.readonly = 0", $configContent);
}
}
file_put_contents($targetConfigPath, $configContent);
}
}
if ($build->isEnabledVariant('pear')) {
$this->logger->info("Initializing pear config...");
$home = Config::getPhpbrewHome();
@mkdir("$home/tmp/pear/temp", 0755, true);
@mkdir("$home/tmp/pear/cache_dir", 0755, true);
@mkdir("$home/tmp/pear/download_dir", 0755, true);
system("pear config-set temp_dir $home/tmp/pear/temp");
system("pear config-set cache_dir $home/tmp/pear/cache_dir");
system("pear config-set download_dir $home/tmp/pear/download_dir");
$this->logger->info("Enabling pear auto-discover...");
system("pear config-set auto_discover 1");
}
$this->logger->debug("Source directory: " . $targetDir);
$buildName = $build->getName();
$this->logger->info("Congratulations! Now you have PHP with $version as $buildName");
echo <<<EOT
To use the newly built PHP, try the line(s) below:
$ phpbrew use $buildName
Or you can use switch command to switch your default php to $buildName:
$ phpbrew switch $buildName
Enjoy!
EOT;
}
}