cleanup
[pharext/pharext] / src / pharext / Installer.php
1 <?php
2
3 namespace pharext;
4
5 use Phar;
6
7 /**
8 * The extension install command executed by the extension phar
9 */
10 class Installer implements Command
11 {
12 use CliCommand;
13
14 /**
15 * The temporary directory we should operate in
16 * @var string
17 */
18 private $tmp;
19
20 /**
21 * The directory we came from
22 * @var string
23 */
24 private $cwd;
25
26 /**
27 * Create the command
28 */
29 public function __construct() {
30 $this->args = new CliArgs([
31 ["h", "help", "Display help",
32 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
33 ["v", "verbose", "More output",
34 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
35 ["q", "quiet", "Less output",
36 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
37 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
38 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
39 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
40 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
41 "php"],
42 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
43 CliArgs::OPTIONAL|CliArgs::MULTI|CliArgs::REQARG],
44 ["s", "sudo", "Installation might need increased privileges",
45 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::OPTARG,
46 "sudo -S %s"]
47 ]);
48 }
49
50 /**
51 * Cleanup temp directory
52 */
53 public function __destruct() {
54 $this->cleanup();
55 }
56
57 /**
58 * @inheritdoc
59 * @see \pharext\Command::run()
60 */
61 public function run($argc, array $argv) {
62 $this->cwd = getcwd();
63 $this->tmp = $this->tempname(basename(Phar::running(false)));
64
65 $phar = new Phar(Phar::running(false));
66 foreach ($phar as $entry) {
67 if (fnmatch("*.ext.phar*", $entry->getBaseName())) {
68 $temp = $this->newtemp($entry->getBaseName());
69 $phar->extractTo($temp, $entry->getFilename(), true);
70 $phars[$temp] = new Phar($temp."/".$entry->getFilename());
71 }
72 }
73 $phars[$this->tmp] = $phar;
74
75 foreach ($phars as $phar) {
76 if (($hook = $phar["pharext_install.php"])) {
77 $callable = include $phar["pharext_install.php"];
78 if (is_callable($callable)) {
79 $recv[] = $callable($this);
80 }
81 }
82 }
83
84 $errs = [];
85 $prog = array_shift($argv);
86 foreach ($this->args->parse(--$argc, $argv) as $error) {
87 $errs[] = $error;
88 }
89
90 if ($this->args["help"]) {
91 $this->header();
92 $this->help($prog);
93 exit;
94 }
95
96 foreach ($this->args->validate() as $error) {
97 $errs[] = $error;
98 }
99
100 if ($errs) {
101 if (!$this->args["quiet"]) {
102 $this->header();
103 }
104 foreach ($errs as $err) {
105 $this->error("%s\n", $err);
106 }
107 if (!$this->args["quiet"]) {
108 $this->help($prog);
109 }
110 exit(1);
111 }
112
113 if (isset($recv)) {
114 foreach ($recv as $r) {
115 $r($this);
116 }
117 }
118 foreach ($phars as $temp => $phar) {
119 $this->installPackage($phar, $temp);
120 }
121 }
122
123 /**
124 * Create a new temp directory
125 * @param string $prefix
126 * @return string
127 */
128 private function newtemp($prefix) {
129 $temp = $this->tempname($prefix);
130 if (!is_dir($temp)) {
131 if (!mkdir($temp, 0750, true)) {
132 $this->error(null);
133 exit(3);
134 }
135 }
136 return $temp;
137 }
138
139 /**
140 * Prepares, configures, builds and installs the extension
141 */
142 private function installPackage(Phar $phar, $temp) {
143 $this->info("Installing %s ... \n", basename($phar->getAlias()));
144 try {
145 $phar->extractTo($temp, null, true);
146 } catch (\Exception $e) {
147 $this->error("%s\n", $e->getMessage());
148 exit(3);
149 }
150
151 if (!chdir($temp)) {
152 $this->error(null);
153 exit(4);
154 }
155
156 $this->exec("phpize", $this->php("ize"));
157 $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ".
158 implode(" ", (array) $this->args->configure));
159 $this->exec("make", $this->args->verbose ? "make -j3" : "make -sj3");
160 $this->exec("install", $this->args->verbose ? "make install" : "make -s install", true);
161
162 $this->cleanup($temp);
163
164 $this->info("Don't forget to activiate the extension in your php.ini!\n\n");
165 }
166
167 /**
168 * Perform any cleanups
169 */
170 private function cleanup($temp = null) {
171 if (!isset($temp)) {
172 $temp = $this->tmp;
173 }
174 if (is_dir($temp)) {
175 chdir($this->cwd);
176 $this->info("Cleaning up %s ...\n", $temp);
177 $this->rm($temp);
178 }
179 }
180
181 /**
182 * rm -r
183 * @param string $dir
184 */
185 private function rm($dir) {
186 foreach (scandir($dir) as $entry) {
187 if ($entry === "." || $entry === "..") {
188 continue;
189 } elseif (is_dir("$dir/$entry")) {
190 $this->rm("$dir/$entry");
191 } elseif (!unlink("$dir/$entry")) {
192 $this->error(null);
193 }
194 }
195 if (!rmdir($dir)) {
196 $this->error(null);
197 }
198 }
199
200 /**
201 * Execute a program with escalated privileges handling interactive password prompt
202 * @param string $command
203 * @param string $output
204 * @return int
205 */
206 private function sudo($command, &$output) {
207 if (!($proc = proc_open($command, [STDIN,["pipe","w"],["pipe","w"]], $pipes))) {
208 return -1;
209 }
210 $stdout = $pipes[1];
211 $passwd = 0;
212 while (!feof($stdout)) {
213 $R = [$stdout]; $W = []; $E = [];
214 if (!stream_select($R, $W, $E, null)) {
215 continue;
216 }
217 $data = fread($stdout, 0x1000);
218 /* only check a few times */
219 if ($passwd++ < 10) {
220 if (stristr($data, "password")) {
221 printf("\n%s", $data);
222 }
223 }
224 $output .= $data;
225 }
226 return proc_close($proc);
227 }
228 /**
229 * Execute a system command
230 * @param string $name pretty name
231 * @param string $command full command
232 * @param bool $sudo whether the command may need escalated privileges
233 */
234 private function exec($name, $command, $sudo = false) {
235 $this->info("Running %s ...%s", $this->args->verbose ? $command : $name, $this->args->verbose ? "\n" : " ");
236 if ($sudo && isset($this->args->sudo)) {
237 $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $command), $output);
238 } elseif ($this->args->verbose) {
239 passthru($command ." 2>&1", $retval);
240 } else {
241 exec($command ." 2>&1", $output, $retval);
242 $output = implode("\n", $output);
243 }
244 if ($retval) {
245 $this->error("Command %s failed with (%s)\n", $command, $retval);
246 if (isset($output) && !$this->args->quiet) {
247 printf("%s\n", $output);
248 }
249 exit(2);
250 }
251 $this->info("OK\n");
252 }
253
254 /**
255 * Construct a command from prefix common-name and suffix
256 * @param type $suffix
257 * @return string
258 */
259 private function php($suffix) {
260 $cmd = $this->args["common-name"] . $suffix;
261 if (isset($this->args->prefix)) {
262 $cmd = $this->args->prefix . "/bin/" . $cmd;
263 }
264 return $cmd;
265 }
266 }