Ship your dependencies as phars inside the phar
[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 private function newtemp($prefix) {
124 $temp = $this->tempname($prefix);
125 if (!is_dir($temp)) {
126 if (!mkdir($temp, 0750, true)) {
127 $this->error(null);
128 exit(3);
129 }
130 }
131 return $temp;
132 }
133
134 /**
135 * Prepares, configures, builds and installs the extension
136 */
137 private function installPackage(Phar $phar, $temp) {
138 $this->info("Installing %s ... \n", basename($phar->getAlias()));
139 try {
140 $phar->extractTo($temp, null, true);
141 } catch (\Exception $e) {
142 $this->error("%s\n", $e->getMessage());
143 exit(3);
144 }
145
146 if (!chdir($temp)) {
147 $this->error(null);
148 exit(4);
149 }
150
151 $this->exec("phpize", $this->php("ize"));
152 $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ".
153 implode(" ", (array) $this->args->configure));
154 $this->exec("make", $this->args->verbose ? "make -j3" : "make -sj3");
155 $this->exec("install", $this->args->verbose ? "make install" : "make -s install", true);
156
157 $this->cleanup($temp);
158
159 $this->info("Don't forget to activiate the extension in your php.ini!\n\n");
160 }
161
162 /**
163 * Perform any cleanups
164 */
165 private function cleanup($temp = null) {
166 if (!isset($temp)) {
167 $temp = $this->tmp;
168 }
169 if (is_dir($temp)) {
170 chdir($this->cwd);
171 $this->info("Cleaning up %s ...\n", $temp);
172 $this->rm($temp);
173 }
174 }
175
176 /**
177 * rm -r
178 * @param string $dir
179 */
180 private function rm($dir) {
181 foreach (scandir($dir) as $entry) {
182 if ($entry === "." || $entry === "..") {
183 continue;
184 } elseif (is_dir("$dir/$entry")) {
185 $this->rm("$dir/$entry");
186 } elseif (!unlink("$dir/$entry")) {
187 $this->error(null);
188 }
189 }
190 if (!rmdir($dir)) {
191 $this->error(null);
192 }
193 }
194
195 /**
196 * Execute a program with escalated privileges handling interactive password prompt
197 * @param string $command
198 * @param string $output
199 * @return int
200 */
201 private function sudo($command, &$output) {
202 if (!($proc = proc_open($command, [STDIN,["pipe","w"],["pipe","w"]], $pipes))) {
203 return -1;
204 }
205 $stdout = $pipes[1];
206 $passwd = 0;
207 while (!feof($stdout)) {
208 $R = [$stdout]; $W = []; $E = [];
209 if (!stream_select($R, $W, $E, null)) {
210 continue;
211 }
212 $data = fread($stdout, 0x1000);
213 /* only check a few times */
214 if ($passwd++ < 10) {
215 if (stristr($data, "password")) {
216 printf("\n%s", $data);
217 }
218 }
219 $output .= $data;
220 }
221 return proc_close($proc);
222 }
223 /**
224 * Execute a system command
225 * @param string $name pretty name
226 * @param string $command full command
227 * @param bool $sudo whether the command may need escalated privileges
228 */
229 private function exec($name, $command, $sudo = false) {
230 $this->info("Running %s ...%s", $this->args->verbose ? $command : $name, $this->args->verbose ? "\n" : " ");
231 if ($sudo && isset($this->args->sudo)) {
232 $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $command), $output);
233 } elseif ($this->args->verbose) {
234 passthru($command ." 2>&1", $retval);
235 } else {
236 exec($command ." 2>&1", $output, $retval);
237 $output = implode("\n", $output);
238 }
239 if ($retval) {
240 $this->error("Command %s failed with (%s)\n", $command, $retval);
241 if (isset($output) && !$this->args->quiet) {
242 printf("%s\n", $output);
243 }
244 exit(2);
245 }
246 $this->info("OK\n");
247 }
248
249 /**
250 * Construct a command from prefix common-name and suffix
251 * @param type $suffix
252 * @return string
253 */
254 private function php($suffix) {
255 $cmd = $this->args["common-name"] . $suffix;
256 if (isset($this->args->prefix)) {
257 $cmd = $this->args->prefix . "/bin/" . $cmd;
258 }
259 return $cmd;
260 }
261 }