8 * The extension install command executed by the extension phar
10 class Installer
implements Command
15 * The temporary directory we should operate in
21 * The directory we came from
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
,
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
,
51 * Cleanup temp directory
53 public function __destruct() {
59 * @see \pharext\Command::run()
61 public function run($argc, array $argv) {
62 if (($hook = stream_resolve_include_path("pharext_install.php"))) {
63 $callable = include $hook;
64 if (is_callable($callable)) {
65 $recv = $callable($this);
70 $prog = array_shift($argv);
71 foreach ($this->args
->parse(--$argc, $argv) as $error) {
75 if ($this->args
["help"]) {
81 foreach ($this->args
->validate() as $error) {
86 if (!$this->args
["quiet"]) {
89 foreach ($errs as $err) {
90 $this->error("%s\n", $err);
92 if (!$this->args
["quiet"]) {
102 $this->installPackage();
106 * Prepares, configures, builds and installs the extension
108 private function installPackage() {
111 if (!chdir($this->tmp
)) {
116 $this->exec("phpize", $this->php("ize"));
117 $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ".
118 implode(" ", (array) $this->args
->configure
));
119 $this->exec("make", $this->args
->verbose ?
"make -j3" : "make -sj3");
120 $this->exec("install", $this->args
->verbose ?
"make install" : "make -s install", true);
124 $this->info("\nDon't forget to activiate the extension in your php.ini!\n");
128 * Perform any cleanups
130 private function cleanup() {
131 if (is_dir($this->tmp
)) {
133 $this->info("Cleaning up %s ...\n", $this->tmp
);
134 $this->rm($this->tmp
);
139 * Extract the phar to a temporary directory
141 private function extract() {
142 if (!$file = Phar
::running(false)) {
143 $this->error("Did your run the ext.phar?\n");
147 $temp = $this->tempname(basename($file));
148 if (!is_dir($temp)) {
149 if (!mkdir($temp, 0750, true)) {
155 $this->cwd
= getcwd();
158 $phar = new Phar($file);
159 $phar->extractTo($temp, null, true);
160 } catch (\Exception
$e) {
161 $this->error("%s\n", $e->getMessage());
170 private function rm($dir) {
171 foreach (scandir($dir) as $entry) {
172 if ($entry === "." ||
$entry === "..") {
174 } elseif (is_dir("$dir/$entry")) {
175 $this->rm("$dir/$entry");
176 } elseif (!unlink("$dir/$entry")) {
186 * Execute a program with escalated privileges handling interactive password prompt
187 * @param string $command
188 * @param string $output
191 private function sudo($command, &$output) {
192 if (!($proc = proc_open($command, [STDIN
,["pipe","w"],["pipe","w"]], $pipes))) {
197 while (!feof($stdout)) {
198 $R = [$stdout]; $W = []; $E = [];
199 if (!stream_select($R, $W, $E, null)) {
202 $data = fread($stdout, 0x1000);
203 /* only check a few times */
204 if ($passwd++
< 10) {
205 if (stristr($data, "password")) {
206 printf("\n%s", $data);
211 return proc_close($proc);
214 * Execute a system command
215 * @param string $name pretty name
216 * @param string $command full command
217 * @param bool $sudo whether the command may need escalated privileges
219 private function exec($name, $command, $sudo = false) {
220 $this->info("Running %s ...%s", $this->args
->verbose ?
$command : $name, $this->args
->verbose ?
"\n" : " ");
221 if ($sudo && isset($this->args
->sudo
)) {
222 $retval = $this->sudo(sprintf($this->args
->sudo
." 2>&1", $command), $output);
223 } elseif ($this->args
->verbose
) {
224 passthru($command ." 2>&1", $retval);
226 exec($command ." 2>&1", $output, $retval);
227 $output = implode("\n", $output);
230 $this->error("Command %s failed with (%s)\n", $command, $retval);
231 if (isset($output) && !$this->args
->quiet
) {
232 printf("%s\n", $output);
240 * Construct a command from prefix common-name and suffix
241 * @param type $suffix
244 private function php($suffix) {
245 $cmd = $this->args
["common-name"] . $suffix;
246 if (isset($this->args
->prefix
)) {
247 $cmd = $this->args
->prefix
. "/bin/" . $cmd;