-
Notifications
You must be signed in to change notification settings - Fork 365
/
Build.php
255 lines (216 loc) · 6.18 KB
/
Build.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
<?php
namespace PhpBrew;
use PhpBrew\BuildSettings\BuildSettings;
/**
* A build object contains version information,
* variant configuration,
* paths and an build identifier (BuildId).
*
* @method array getEnabledVariants()
* @method array getDisabledVariants()
* @method bool isEnabledVariant(string $variant)
* @method bool isDisabledVariant(string $variant)
* @method array getExtraOptions()
* @method enableVariant(string $variant, string $value = null)
* @method disableVariant(string $variant)
* @method removeVariant(string $variant)
* @method array resolveVariants()
*/
class Build implements Buildable
{
/**
* States that describe finished task.
*/
const STATE_NONE = 0;
const STATE_DOWNLOAD = 1;
const STATE_EXTRACT = 2;
const STATE_CONFIGURE = 3;
const STATE_BUILD = 4;
const STATE_INSTALL = 5;
public $name;
public $version;
/**
* @var string The source directory
*/
public $sourceDirectory;
/**
* @var string the directory that contains bin/php, var/..., includes/
*/
public $installPrefix;
/**
* @var BuildSettings
*/
public $settings;
/**
* Build state.
*
* @var string
*/
public $state;
/**
* environment related information (should be moved to environment class).
*/
public $osName;
public $osRelease;
public $osArch;
/**
* Construct a Build object,.
*
* A build object contains the information of all build options, prefix, paths... etc
*
* @param string $version build version
* @param string $name build name
* @param string $installPrefix install prefix
*/
public function __construct($version, $name = null, $installPrefix = null)
{
if (substr($version, 0, 4) === 'php-') {
$version = substr($version, 4);
}
if ($name === null) {
$name = 'php-' . $version;
}
$this->version = $version;
$this->name = $name;
if ($installPrefix) {
$this->setInstallPrefix($installPrefix);
}
$this->setBuildSettings(new BuildSettings());
$this->osName = php_uname('s');
$this->osRelease = php_uname('r');
$this->osArch = php_uname('m');
}
public function getName()
{
return $this->name;
}
public function getVersion()
{
return $this->version;
}
public function compareVersion($version)
{
return version_compare($this->version, $version);
}
/**
* PHP Source directory, this method returns value only when source directory is set.
*/
public function setSourceDirectory($dir)
{
$this->sourceDirectory = $dir;
}
public function getSourceDirectory()
{
if ($this->sourceDirectory && !file_exists($this->sourceDirectory)) {
mkdir($this->sourceDirectory, 0755, true);
}
return $this->sourceDirectory;
}
public function isBuildable()
{
return file_exists($this->sourceDirectory . DIRECTORY_SEPARATOR . 'Makefile');
}
public function getBuildLogPath()
{
$dir = $this->getSourceDirectory() . DIRECTORY_SEPARATOR . 'build.log';
return $dir;
}
public function setInstallPrefix($prefix)
{
$this->installPrefix = $prefix;
}
public function getBinDirectory()
{
return $this->installPrefix . DIRECTORY_SEPARATOR . 'bin';
}
public function getEtcDirectory()
{
$etc = $this->installPrefix . DIRECTORY_SEPARATOR . 'etc';
if (!file_exists($etc)) {
mkdir($etc, 0755, true);
}
return $etc;
}
public function getInstallPrefix()
{
return $this->installPrefix;
}
public function getPath($subpath)
{
return $this->installPrefix . DIRECTORY_SEPARATOR . $subpath;
}
public function setBuildSettings(BuildSettings $settings)
{
$this->settings = $settings;
if (!$this->getInstallPrefix()) {
return;
}
// TODO: in future, we only stores build meta information, and that
// also contains the variant info,
// but for backward compatibility, we still need a method to handle
// the variant info file..
$variantFile = $this->getInstallPrefix() . DIRECTORY_SEPARATOR . 'phpbrew.variants';
if (file_exists($variantFile)) {
$this->settings->loadVariantInfoFile($variantFile);
}
}
/**
* Find a installed build by name,
* currently a $name is a php version, but in future we may have customized
* name for users.
*
* @param string $name
*
* @return Build
*/
public static function findByName($name)
{
$prefix = Config::getVersionInstallPrefix($name);
if (file_exists($prefix)) {
// a installation exists
return new self($name, null, $prefix);
}
return;
}
/**
* Where we store the last finished state, currently for:.
*
* - FALSE or NULL - nothing done yet.
* - "download" - distribution file was downloaded.
* - "extract" - distribution file was extracted to the build directory.
* - "configure" - configure was done.
* - "make" - make was done.
* - "install" - installation was done.
*
* Not used yet.
*/
public function getStateFile()
{
if ($dir = $this->getInstallPrefix()) {
return $dir . DIRECTORY_SEPARATOR . 'phpbrew_status';
}
}
public function setState($state)
{
$this->state = $state;
if ($path = $this->getStateFile()) {
file_put_contents($path, $state);
}
}
public function getState()
{
if ($this->state) {
return $this->state;
}
if ($path = $this->getStateFile()) {
if (file_exists($path)) {
return $this->state = intval(file_get_contents($path)) || self::STATE_NONE;
}
}
return self::STATE_NONE;
}
public function __call($m, $a)
{
return call_user_func_array(array($this->settings, $m), $a);
}
}