forked from phpbrew/phpbrew
/
InstallCommand.php
298 lines (233 loc) · 10 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
<?php
namespace PhpBrew\Command;
use Exception;
use PhpBrew\Config;
use PhpBrew\PhpSource;
use PhpBrew\Builder;
use PhpBrew\VariantParser;
use PhpBrew\VariantBuilder;
use PhpBrew\Tasks\DownloadTask;
use PhpBrew\Tasks\PrepareDirectoryTask;
use PhpBrew\Tasks\CleanTask;
use PhpBrew\Tasks\InstallTask;
use PhpBrew\Tasks\BuildTask;
use PhpBrew\Tasks\DSymTask;
use PhpBrew\Tasks\TestTask;
use PhpBrew\Build;
use CLIFramework\Command;
/*
* TODO: refactor tasks to Task class.
*/
class InstallCommand extends Command
{
public function brief()
{
return 'install php';
}
public function usage()
{
return 'phpbrew install [php-version] ([+variant...])';
}
public function arguments($args) {
$args->add('version')->suggestions(array( '5.3', '5.4', '5.5' ) );
$args->add('variants')->multiple()->suggestions(function() {
$variants = new VariantBuilder;
$list = $variants->getVariantNames();
sort($list);
return array_map(function($n) { return '+' . $n; }, $list);
});
}
/**
* @param \GetOptionKit\OptionSpecCollection $opts
*/
public function options($opts)
{
$opts->add('test', 'run tests');
$opts->add('name:', 'prefix name');
$opts->add('clean', 'Run make clean before building.');
$opts->add('post-clean', 'Run make clean after building PHP.');
$opts->add('production', 'Use production configuration');
$opts->add('n|nice:', 'process nice level');
$opts->add('patch+:', 'apply patch before build')
->isa('file')
;
$opts->add('old', 'install old phps (less than 5.3)');
$opts->add('f|force', 'force');
$opts->add('d|dryrun', 'dryrun');
$opts->add('like:', 'inherit variants from previous build');
$opts->add('j|make-jobs:', 'Specifies the number of jobs to run simultaneously (make -jN).');
}
public function execute($version)
{
if (!preg_match('/^php-/', $version)) {
$version = 'php-' . $version;
}
$version = $this->getLatestMinorVersion($version, $this->options->old);
$options = $this->options;
$logger = $this->logger;
// get options and variants for building php
$args = func_get_args();
// the first argument is the target version.
array_shift($args);
$name = $this->options->name ?: $version;
// find inherited variants
$inheritedVariants = array();
if ($this->options->like) {
$inheritedVariants = VariantParser::getInheritedVariants($this->options->like);
}
// ['extra_options'] => the extra options to be passed to ./configure command
// ['enabled_variants'] => enabeld variants
// ['disabled_variants'] => disabled variants
$variantInfo = VariantParser::parseCommandArguments($args, $inheritedVariants);
$info = PhpSource::getVersionInfo($version, $this->options->old);
if (!$info) {
throw new Exception("Version $version not found.");
}
$prepare = new PrepareDirectoryTask($this->logger);
$prepare->prepareForVersion($version);
// 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;
}
// Move to to build directory, because we are going to download distribution.
$buildDir = Config::getBuildDir();
chdir($buildDir);
$download = new DownloadTask($this->logger);
$targetDir = $download->downloadByVersionString($version, $this->options->old, $this->options->force);
if (!file_exists($targetDir)) {
throw new Exception("Download failed.");
}
// Change directory to the downloaded source directory.
chdir($targetDir);
$buildPrefix = Config::getVersionBuildPrefix($version);
if (!file_exists($buildPrefix)) {
mkdir($buildPrefix, 0755, true);
}
// write variants info.
$variantInfoFile = $buildPrefix . DIRECTORY_SEPARATOR . 'phpbrew.variants';
$this->logger->debug("Writing variant info to $variantInfoFile");
file_put_contents($variantInfoFile, serialize($variantInfo));
// The build object, contains the information to build php.
$build = new Build($version, $name, $buildPrefix);
$build->setInstallPrefix($buildPrefix);
$build->setSourceDirectory($targetDir);
$builder = new Builder($targetDir, $version);
$builder->logger = $this->logger;
$builder->options = $this->options;
$this->logger->debug('Build Directory: ' . realpath($targetDir));
foreach ($variantInfo['enabled_variants'] as $name => $value) {
$build->enableVariant($name, $value);
}
foreach ($variantInfo['disabled_variants'] as $name => $value) {
$build->disableVariant($name);
if ($build->hasVariant($name)) {
$this->logger->warn("Removing variant $name since we've disabled it from command.");
$build->removeVariant($name);
}
}
$build->setExtraOptions($variantInfo['extra_options']);
if ($options->clean) {
$clean = new CleanTask($this->logger);
$clean->cleanByVersion($version);
}
$buildLogFile = Config::getVersionBuildLogPath($version);
$configure = new \PhpBrew\Tasks\ConfigureTask($this->logger);
$configure->configure($build, $this->options);
$buildTask = new BuildTask($this->logger);
$buildTask->setLogPath($buildLogFile);
$buildTask->build($build, $this->options);
if ($options->{'test'}) {
$test = new TestTask($this->logger);
$test->setLogPath($buildLogFile);
$test->test($build, $this->options);
}
$install = new InstallTask($this->logger);
$install->setLogPath($buildLogFile);
$install->install($build, $this->options);
if ($options->{'post-clean'}) {
$clean = new CleanTask($this->logger);
$clean->cleanByVersion($version);
}
/** POST INSTALLATION **/
$dsym = new DSymTask($this->logger);
$dsym->patch($build, $this->options);
// copy php-fpm config
$this->logger->info("---> Creating php-fpm.conf");
$phpFpmConfigPath = "sapi/fpm/php-fpm.conf";
$phpFpmTargetConfigPath = Config::getVersionEtcPath($version) . DIRECTORY_SEPARATOR . 'php-fpm.conf';
if (file_exists($phpFpmConfigPath)) {
if (!file_exists($phpFpmTargetConfigPath)) {
copy($phpFpmConfigPath, $phpFpmTargetConfigPath);
} else {
$this->logger->notice("Found existing $phpFpmTargetConfigPath.");
}
}
$phpConfigPath = $options->production ? 'php.ini-production' : 'php.ini-development';
$this->logger->info("---> Copying $phpConfigPath ");
if (file_exists($phpConfigPath)) {
$targetConfigPath = Config::getVersionEtcPath($version) . 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);
// replace current timezone
$timezone = ini_get('date.timezone');
$pharReadonly = ini_get('phar.readonly');
if ($timezone || $pharReadonly) {
// patch default config
$content = file_get_contents($targetConfigPath);
if ($timezone) {
$this->logger->info("---> Found date.timezone, patch config timezone with $timezone");
$content = preg_replace('/^date.timezone\s*=\s*.*/im', "date.timezone = $timezone", $content);
}
if (! $pharReadonly) {
$this->logger->info("---> Disable phar.readonly option.");
$content = preg_replace('/^phar.readonly\s*=\s*.*/im', "phar.readonly = 0", $content);
}
file_put_contents($targetConfigPath, $content);
}
}
}
$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);
$this->logger->info("Congratulations! Now you have PHP with $version.");
echo <<<EOT
To use the newly built PHP, try the line(s) below:
$ phpbrew use $version
Or you can use switch command to switch your default php version to $version:
$ phpbrew switch $version
Enjoy!
EOT;
}
private function getLatestMinorVersion($majorVersion, $includeOld)
{
$latestMinorVersion = '';
foreach (array_keys(PhpSource::getAllVersions($includeOld)) as $version) {
if (strpos($version, $majorVersion) === 0) {
$latestMinorVersion = $version;
break;
}
}
return $latestMinorVersion;
}
}