add ExecCmd and Tempdir
[pharext/pharext] / src / pharext / Installer.php
1 <?php
2
3 namespace pharext;
4
5 use Phar;
6 use pharext\Cli\Args as CliArgs;
7 use pharext\Cli\Command as CliCommand;
8
9 /**
10 * The extension install command executed by the extension phar
11 */
12 class Installer implements Command
13 {
14 use CliCommand;
15
16 /**
17 * The temporary directory we should operate in
18 * @var string
19 */
20 private $tmp;
21
22 /**
23 * The directory we came from
24 * @var string
25 */
26 private $cwd;
27
28 /**
29 * Create the command
30 */
31 public function __construct() {
32 $this->args = new CliArgs([
33 ["h", "help", "Display help",
34 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
35 ["v", "verbose", "More output",
36 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
37 ["q", "quiet", "Less output",
38 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
39 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
40 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
41 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
42 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
43 "php"],
44 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
45 CliArgs::OPTIONAL|CliArgs::MULTI|CliArgs::REQARG],
46 ["s", "sudo", "Installation might need increased privileges",
47 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::OPTARG,
48 "sudo -S %s"],
49 ["i", "ini", "Activate in this php.ini instead of loaded default php.ini",
50 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
51 ]);
52 }
53
54 /**
55 * Cleanup temp directory
56 */
57 public function __destruct() {
58 $this->cleanup();
59 }
60
61 /**
62 * @inheritdoc
63 * @see \pharext\Command::run()
64 */
65 public function run($argc, array $argv) {
66 $this->cwd = getcwd();
67 $this->tmp = $this->tempname(basename(Phar::running(false)));
68
69 $phar = new Phar(Phar::running(false));
70 foreach ($phar as $entry) {
71 if (fnmatch("*.ext.phar*", $entry->getBaseName())) {
72 $temp = new Tempdir($entry->getBaseName());
73 $phar->extractTo($temp, $entry->getFilename(), true);
74 $phars[$temp] = new Phar($temp."/".$entry->getFilename());
75 }
76 }
77 $phars[$this->tmp] = $phar;
78
79 foreach ($phars as $phar) {
80 if (isset($phar["pharext_install.php"])) {
81 $callable = include $phar["pharext_install.php"];
82 if (is_callable($callable)) {
83 $recv[] = $callable($this);
84 }
85 }
86 }
87
88 $errs = [];
89 $prog = array_shift($argv);
90 foreach ($this->args->parse(--$argc, $argv) as $error) {
91 $errs[] = $error;
92 }
93
94 if ($this->args["help"]) {
95 $this->header();
96 $this->help($prog);
97 exit;
98 }
99
100 foreach ($this->args->validate() as $error) {
101 $errs[] = $error;
102 }
103
104 if ($errs) {
105 if (!$this->args["quiet"]) {
106 $this->header();
107 }
108 foreach ($errs as $err) {
109 $this->error("%s\n", $err);
110 }
111 if (!$this->args["quiet"]) {
112 $this->help($prog);
113 }
114 exit(1);
115 }
116
117 if (isset($recv)) {
118 foreach ($recv as $r) {
119 $r($this);
120 }
121 }
122 foreach ($phars as $temp => $phar) {
123 $this->installPackage($phar, $temp);
124 }
125 }
126
127 /**
128 * Prepares, configures, builds and installs the extension
129 */
130 private function installPackage(Phar $phar, $temp) {
131 $this->info("Installing %s ... \n", basename($phar->getAlias()));
132 try {
133 $phar->extractTo($temp, null, true);
134 } catch (\Exception $e) {
135 $this->error("%s\n", $e->getMessage());
136 exit(3);
137 }
138
139 if (!chdir($temp)) {
140 $this->error(null);
141 exit(4);
142 }
143
144 $this->build();
145 $this->activate();
146 $this->cleanup($temp);
147 }
148
149 /**
150 * Phpize + trinity
151 */
152 private function build() {
153 try {
154 // phpize
155 $this->info("Runnin phpize ... ");
156 $cmd = new ExecCmd($this->php("ize"), $this->args->verbose);
157 $cmd->run();
158 $this->info("OK\n");
159
160 // configure
161 $this->info("Running configure ... ");
162 $args = ["--with-php-config=". $this->php("-config")];
163 if ($this->args->configure) {
164 $args = array_merge($args, $this->args->configure);
165 }
166 $cmd = new ExecCmd("./configure", $this->args->verbose);
167 $cmd->run($args);
168 $this->info("OK\n");
169
170 // make
171 $this->info("Running make ... ");
172 $cmd = new ExecCmd("make", $this->args->verbose);
173 if ($this->args->verbose) {
174 $cmd->run(["-j3"]);
175 } else {
176 $cmd->run(["-j3", "-s"]);
177 }
178 $this->info("OK\n");
179
180 // install
181 $this->info("Running make install ... ");
182 $cmd->setSu($this->args->sudo);
183 if ($this->args->verbose) {
184 $cmd->run(["install"]);
185 } else {
186 $cmd->run(["install", "-s"]);
187 }
188 $this->info("OK\n");
189
190 } catch (\Exception $e) {
191 $this->error("%s\n", $e->getMessage());
192 $this->error("%s\n", $cmd->getOutput());
193 }
194 }
195
196 /**
197 * Perform any cleanups
198 */
199 private function cleanup($temp = null) {
200 if (!isset($temp)) {
201 $temp = $this->tmp;
202 }
203 if (is_dir($temp)) {
204 chdir($this->cwd);
205 $this->info("Cleaning up %s ...\n", $temp);
206 $this->rm($temp);
207 }
208 }
209
210 /**
211 * Construct a command from prefix common-name and suffix
212 * @param type $suffix
213 * @return string
214 */
215 private function php($suffix) {
216 $cmd = $this->args["common-name"] . $suffix;
217 if (isset($this->args->prefix)) {
218 $cmd = $this->args->prefix . "/bin/" . $cmd;
219 }
220 return $cmd;
221 }
222
223 /**
224 * Activate extension in php.ini
225 */
226 private function activate() {
227 if ($this->args->ini) {
228 $files = [realpath($this->args->ini)];
229 } else {
230 $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
231 $files[] = php_ini_loaded_file();
232 }
233
234 $extension = basename(current(glob("modules/*.so")));
235 $pattern = preg_quote($extension);
236
237 foreach ($files as $index => $file) {
238 $temp = new Tempfile("phpini");
239 foreach (file($file) as $line) {
240 if (preg_match("/^\s*extension\s*=\s*[\"']?{$pattern}[\"']?\s*(;.*)?\$/", $line)) {
241 // already there
242 $this->info("Extension already activated\n");
243 return;
244 }
245 fwrite($temp->getStream(), $line);
246 }
247 }
248
249 // not found, add extension line to the last process file
250 if (isset($temp, $file)) {
251 fprintf($temp->getStream(), "extension=%s\n", $extension);
252 $temp->closeStream();
253
254 $path = $temp->getPathname();
255 $stat = stat($file);
256
257 try {
258 $this->info("Running INI owner transfer ... ");
259 $ugid = sprintf("%d:%d", $stat["uid"], $stat["gid"]);
260 $cmd = new ExecCmd("chown", $this->args->verbose);
261 $cmd->setSu($this->args->sudo);
262 $cmd->run([$ugid, $path]);
263 $this->info("OK\n");
264
265 $this->info("Running INI permission transfer ... ");
266 $perm = decoct($stat["mode"] & 0777);
267 $cmd = new ExecCmd("chmod", $this->args->verbose);
268 $cmd->setSu($this->args->sudo);
269 $cmd->run([$perm, $path]);
270 $this->info("OK\n");
271
272 $this->info("Running INI activation ... ");
273 $cmd = new ExecCmd("mv", $this->args->verbose);
274 $cmd->setSu($this->args->sudo);
275 $cmd->run([$path, $file]);
276 $this->info("OK\n");
277 } catch (\Exception $e) {
278 $this->error("%s\n", $e->getMessage());
279 $this->error("%s\n", $cmd->getOutput());
280 exit(5);
281 }
282 }
283 }
284 }