589abd611a3384d888826271324cb3aba08d5dfd
[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 ]);
50 }
51
52 /**
53 * Cleanup temp directory
54 */
55 public function __destruct() {
56 $this->cleanup();
57 }
58
59 /**
60 * @inheritdoc
61 * @see \pharext\Command::run()
62 */
63 public function run($argc, array $argv) {
64 $this->cwd = getcwd();
65 $this->tmp = $this->tempname(basename(Phar::running(false)));
66
67 $phar = new Phar(Phar::running(false));
68 foreach ($phar as $entry) {
69 if (fnmatch("*.ext.phar*", $entry->getBaseName())) {
70 $temp = $this->newtemp($entry->getBaseName());
71 $phar->extractTo($temp, $entry->getFilename(), true);
72 $phars[$temp] = new Phar($temp."/".$entry->getFilename());
73 }
74 }
75 $phars[$this->tmp] = $phar;
76
77 foreach ($phars as $phar) {
78 if (isset($phar["pharext_install.php"])) {
79 $callable = include $phar["pharext_install.php"];
80 if (is_callable($callable)) {
81 $recv[] = $callable($this);
82 }
83 }
84 }
85
86 $errs = [];
87 $prog = array_shift($argv);
88 foreach ($this->args->parse(--$argc, $argv) as $error) {
89 $errs[] = $error;
90 }
91
92 if ($this->args["help"]) {
93 $this->header();
94 $this->help($prog);
95 exit;
96 }
97
98 foreach ($this->args->validate() as $error) {
99 $errs[] = $error;
100 }
101
102 if ($errs) {
103 if (!$this->args["quiet"]) {
104 $this->header();
105 }
106 foreach ($errs as $err) {
107 $this->error("%s\n", $err);
108 }
109 if (!$this->args["quiet"]) {
110 $this->help($prog);
111 }
112 exit(1);
113 }
114
115 if (isset($recv)) {
116 foreach ($recv as $r) {
117 $r($this);
118 }
119 }
120 foreach ($phars as $temp => $phar) {
121 $this->installPackage($phar, $temp);
122 }
123 }
124
125 /**
126 * Prepares, configures, builds and installs the extension
127 */
128 private function installPackage(Phar $phar, $temp) {
129 $this->info("Installing %s ... \n", basename($phar->getAlias()));
130 try {
131 $phar->extractTo($temp, null, true);
132 } catch (\Exception $e) {
133 $this->error("%s\n", $e->getMessage());
134 exit(3);
135 }
136
137 if (!chdir($temp)) {
138 $this->error(null);
139 exit(4);
140 }
141
142 $this->exec("phpize", $this->php("ize"));
143 $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ".
144 implode(" ", (array) $this->args->configure));
145 $this->exec("make", $this->args->verbose ? "make -j3" : "make -sj3");
146 $this->exec("install", $this->args->verbose ? "make install" : "make -s install", true);
147
148 $this->cleanup($temp);
149
150 $this->info("Don't forget to activiate the extension in your php.ini!\n\n");
151 }
152
153 /**
154 * Perform any cleanups
155 */
156 private function cleanup($temp = null) {
157 if (!isset($temp)) {
158 $temp = $this->tmp;
159 }
160 if (is_dir($temp)) {
161 chdir($this->cwd);
162 $this->info("Cleaning up %s ...\n", $temp);
163 $this->rm($temp);
164 }
165 }
166
167 /**
168 * Execute a program with escalated privileges handling interactive password prompt
169 * @param string $command
170 * @param string $output
171 * @return int
172 */
173 private function sudo($command, &$output) {
174 if (!($proc = proc_open($command, [STDIN,["pipe","w"],["pipe","w"]], $pipes))) {
175 return -1;
176 }
177 $stdout = $pipes[1];
178 $passwd = 0;
179 while (!feof($stdout)) {
180 $R = [$stdout]; $W = []; $E = [];
181 if (!stream_select($R, $W, $E, null)) {
182 continue;
183 }
184 $data = fread($stdout, 0x1000);
185 /* only check a few times */
186 if ($passwd++ < 10) {
187 if (stristr($data, "password")) {
188 printf("\n%s", $data);
189 }
190 }
191 $output .= $data;
192 }
193 return proc_close($proc);
194 }
195 /**
196 * Execute a system command
197 * @param string $name pretty name
198 * @param string $command full command
199 * @param bool $sudo whether the command may need escalated privileges
200 */
201 private function exec($name, $command, $sudo = false) {
202 $this->info("Running %s ...%s", $this->args->verbose ? $command : $name, $this->args->verbose ? "\n" : " ");
203 if ($sudo && isset($this->args->sudo)) {
204 $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $command), $output);
205 } elseif ($this->args->verbose) {
206 passthru($command ." 2>&1", $retval);
207 } else {
208 exec($command ." 2>&1", $output, $retval);
209 $output = implode("\n", $output);
210 }
211 if ($retval) {
212 $this->error("Command %s failed with (%s)\n", $command, $retval);
213 if (isset($output) && !$this->args->quiet) {
214 printf("%s\n", $output);
215 }
216 exit(2);
217 }
218 $this->info("OK\n");
219 }
220
221 /**
222 * Construct a command from prefix common-name and suffix
223 * @param type $suffix
224 * @return string
225 */
226 private function php($suffix) {
227 $cmd = $this->args["common-name"] . $suffix;
228 if (isset($this->args->prefix)) {
229 $cmd = $this->args->prefix . "/bin/" . $cmd;
230 }
231 return $cmd;
232 }
233 }