better handling of sudo output
[pharext/pharext] / src / pharext / Installer.php
index d8c7feb3ee4768c0f09fdd419cba191b916debc3..57bbf06d23664f0a37395f0d5c1e0917a16fab0b 100644 (file)
@@ -9,30 +9,37 @@ use Phar;
  */
 class Installer implements Command
 {
+       use CliCommand;
+       
        /**
-        * Command line arguments
-        * @var pharext\CliArgs
+        * The temporary directory we should operate in
+        * @var string
         */
-       private $args;
-       
+       private $tmp;
+
+       /**
+        * The directory we came from
+        * @var string
+        */
+       private $cwd;
+
        /**
         * Create the command
         */
        public function __construct() {
                $this->args = new CliArgs([
                        ["h", "help", "Display help", 
-                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
                        ["v", "verbose", "More output",
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
                        ["q", "quiet", "Less output",
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
-                       ["p", "prefix", "PHP installation directory", 
-                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG, 
-                               "/usr"],
-                       ["n", "common-name", "PHP common program name, e.g. php5", 
+                       ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
+                       ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG, 
                                "php"],
-                       ["c", "configure", "Additional extension configure flags", 
+                       ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
                                CliArgs::OPTIONAL|CliArgs::MULTI|CliArgs::REQARG],
                        ["s", "sudo", "Installation might need increased privileges",
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::OPTARG,
@@ -40,63 +47,94 @@ class Installer implements Command
                ]);
        }
        
+       /**
+        * Cleanup temp directory
+        */
+       public function __destruct() {
+               $this->cleanup();
+       }
+
        /**
         * @inheritdoc
         * @see \pharext\Command::run()
         */
        public function run($argc, array $argv) {
+               if (($hook = stream_resolve_include_path("pharext_install.php"))) {
+                       $callable = include $hook;
+                       if (is_callable($callable)) {
+                               $recv = $callable($this);
+                       }
+               }
+               
+               $errs = [];
                $prog = array_shift($argv);
                foreach ($this->args->parse(--$argc, $argv) as $error) {
-                       $this->error("%s\n", $error);
+                       $errs[] = $error;
                }
                
                if ($this->args["help"]) {
-                       $this->args->help($prog);
+                       $this->header();
+                       $this->help($prog);
                        exit;
                }
                
                foreach ($this->args->validate() as $error) {
-                       $this->error("%s\n", $error);
+                       $errs[] = $error;
                }
                
-               if (isset($error)) {
+               if ($errs) {
                        if (!$this->args["quiet"]) {
-                               $this->args->help($prog);
+                               $this->header();
+                       }
+                       foreach ($errs as $err) {
+                               $this->error("%s\n", $err);
+                       } 
+                       if (!$this->args["quiet"]) {
+                               $this->help($prog);
                        }
                        exit(1);
                }
                
+               if (isset($recv)) {
+                       $recv($this);
+               }
+               
                $this->installPackage();
        }
        
        /**
-        * @inheritdoc
-        * @see \pharext\Command::getArgs()
-        */
-       public function getArgs() {
-               return $this->args;
-       }
-       
-       /**
-        * @inheritdoc
-        * @see \pharext\Command::info()
+        * Prepares, configures, builds and installs the extension
         */
-       public function info($fmt) {
-               if (!$this->args->quiet) {
-                       vprintf($fmt, array_slice(func_get_args(), 1));
+       private function installPackage() {
+               $this->extract();
+
+               if (!chdir($this->tmp)) {
+                       $this->error(null);
+                       exit(4);
                }
+
+               $this->exec("phpize", $this->php("ize"));
+               $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ". 
+                       implode(" ", (array) $this->args->configure));
+               $this->exec("make", $this->args->verbose ? "make -j3" : "make -sj3");
+               $this->exec("install", $this->args->verbose ? "make install" : "make -s install", true);
+
+               $this->cleanup();
+
+               $this->info("\nDon't forget to activiate the extension in your php.ini!\n");
        }
-       
+
        /**
-        * @inheritdoc
-        * @see \pharext\Command::error()
+        * Perform any cleanups
         */
-       public function error($fmt) {
-               if (!$this->args->quiet) {
-                       vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1));
+       private function cleanup() {
+               if (is_dir($this->tmp)) {
+                       chdir($this->cwd);
+                       $this->info("Cleaning up %s ...\n", $this->tmp);
+                       $this->rm($this->tmp);
                }
        }
-       
+
        /**
         * Extract the phar to a temporary directory
         */
@@ -105,13 +143,73 @@ class Installer implements Command
                        $this->error("Did your run the ext.phar?\n");
                        exit(3);
                }
-               $temp = sys_get_temp_dir()."/".basename($file, ".ext.phar");
-               is_dir($temp) or mkdir($temp, 0750, true);
-               $phar = new Phar($file);
-               $phar->extractTo($temp, null, true);
-               chdir($temp);
+
+               $temp = $this->tempname(basename($file));
+               if (!is_dir($temp)) {
+                       if (!mkdir($temp, 0750, true)) {
+                               $this->error(null);
+                               exit(3);
+                       }
+               }
+               $this->tmp = $temp;
+               $this->cwd = getcwd();
+
+               try {
+                       $phar = new Phar($file);
+                       $phar->extractTo($temp, null, true);
+               } catch (\Exception $e) {
+                       $this->error("%s\n", $e->getMessage());
+                       exit(3);
+               }
        }
        
+       /**
+        * rm -r
+        * @param string $dir
+        */
+       private function rm($dir) {
+               foreach (scandir($dir) as $entry) {
+                       if ($entry === "." || $entry === "..") {
+                               continue;
+                       } elseif (is_dir("$dir/$entry")) {
+                               $this->rm("$dir/$entry");
+                       } elseif (!unlink("$dir/$entry")) {
+                               $this->error(null);
+                       }
+               }
+               if (!rmdir($dir)) {
+                       $this->error(null);
+               }
+       }
+       
+       /**
+        * Execute a program with escalated privileges handling interactive password prompt
+        * @param string $command
+        * @param string $output
+        * @return int
+        */
+       private function sudo($command, &$output) {
+               if (!($proc = proc_open($command, [STDIN,["pipe","w"],["pipe","w"]], $pipes))) {
+                       return -1;
+               }
+               $stdout = $pipes[1];
+               $passwd = 0;
+               while (!feof($stdout)) {
+                       $R = [$stdout]; $W = []; $E = [];
+                       if (!stream_select($R, $W, $E, null)) {
+                               continue;
+                       }
+                       $data = fread($stdout, 0x1000);
+                       /* only check a few times */
+                       if ($passwd++ < 10) {
+                               if (stristr($data, "password")) {
+                                       printf("\n%s", $data);
+                               }
+                       }
+                       $output .= $data;
+               }
+               return proc_close($proc);
+       }
        /**
         * Execute a system command
         * @param string $name pretty name
@@ -121,20 +219,17 @@ class Installer implements Command
        private function exec($name, $command, $sudo = false) {
                $this->info("Running %s ...%s", $this->args->verbose ? $command : $name, $this->args->verbose ? "\n" : " ");
                if ($sudo && isset($this->args->sudo)) {
-                       if ($proc = proc_open(sprintf($this->args->sudo, $command)." 2>&1", [STDIN,STDOUT,STDERR], $pipes)) {
-                               $retval = proc_close($proc);
-                       } else {
-                               $retval = -1;
-                       }
+                       $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $command), $output);
                } elseif ($this->args->verbose) {
                        passthru($command ." 2>&1", $retval);
                } else {
                        exec($command ." 2>&1", $output, $retval);
+                       $output = implode("\n", $output);
                }
                if ($retval) {
                        $this->error("Command %s failed with (%s)\n", $command, $retval);
                        if (isset($output) && !$this->args->quiet) {
-                               printf("%s\n", implode("\n", $output));
+                               printf("%s\n", $output);
                        }
                        exit(2);
                }
@@ -142,13 +237,15 @@ class Installer implements Command
        }
        
        /**
-        * Prepares, configures, builds and installs the extension
+        * Construct a command from prefix common-name and suffix
+        * @param type $suffix
+        * @return string
         */
-       private function installPackage() {
-               $this->extract();
-               $this->exec("phpize", "{$this->args->prefix}/bin/{$this->args->{'common-name'}}ize");
-               $this->exec("configure", "./configure --with-php-config={$this->args->prefix}/bin/{$this->args->{'common-name'}}-config ". implode(" ", (array) $this->args->configure));
-               $this->exec("make", "make -sj3");
-               $this->exec("install", "make -s install", true);
+       private function php($suffix) {
+               $cmd = $this->args["common-name"] . $suffix;
+               if (isset($this->args->prefix)) {
+                       $cmd = $this->args->prefix . "/bin/" . $cmd;
+               }
+               return $cmd;
        }
 }