download progress, cleanups
[pharext/pharext] / src / pharext / Installer.php
index c88b0d69f8d7b49a72ea4b0214d9bb79ab4f3140..94f814950178a8a978a0084cef85dd3fa45ecf90 100644 (file)
@@ -69,7 +69,7 @@ class Installer implements Command
                $phar = new Phar(Phar::running(false));
                foreach ($phar as $entry) {
                        if (fnmatch("*.ext.phar*", $entry->getBaseName())) {
-                               $temp = $this->newtemp($entry->getBaseName());
+                               $temp = new Tempdir($entry->getBaseName());
                                $phar->extractTo($temp, $entry->getFilename(), true);
                                $phars[$temp] = new Phar($temp."/".$entry->getFilename());
                        }
@@ -140,37 +140,60 @@ class Installer implements Command
                        $this->error(null);
                        exit(4);
                }
-
-               // phpize
-               $this->exec("phpize", $this->php("ize"));
-
-               // configure
-               $args = ["--with-php-config=". $this->php("-config")];
-               if ($this->args->configure) {
-                       $args = array_merge($args, $this->args->configure);
-               }
-               $this->exec("configure", "./configure", $args);
-
-               // make
-               if ($this->args->verbose) {
-                       $this->exec("make", "make", ["-j3"]);
-               } else {
-                       $this->exec("make", "make", ["-j3", "-s"]);
-               }
-
-               // install
-               if ($this->args->verbose) {
-                       $this->exec("install", "make", ["install"], true);
-               } else {
-                       $this->exec("install", "make", ["install", "-s"], true);
-               }
-
-               // activate
+               
+               $this->build();
                $this->activate();
-
-               // cleanup
                $this->cleanup($temp);
        }
+       
+       /**
+        * Phpize + trinity
+        */
+       private function build() {
+               try {
+                       // phpize
+                       $this->info("Runnin phpize ... ");
+                       $cmd = new ExecCmd($this->php("ize"), $this->args->verbose);
+                       $cmd->run();
+                       $this->info("OK\n");
+                               
+                       // configure
+                       $this->info("Running configure ... ");
+                       $args = ["--with-php-config=". $this->php("-config")];
+                       if ($this->args->configure) {
+                               $args = array_merge($args, $this->args->configure);
+                       }
+                       $cmd = new ExecCmd("./configure", $this->args->verbose);
+                       $cmd->run($args);
+                       $this->info("OK\n");
+                               
+                       // make
+                       $this->info("Running make ... ");
+                       $cmd = new ExecCmd("make", $this->args->verbose);
+                       if ($this->args->verbose) {
+                               $cmd->run(["-j3"]);
+                       } else {
+                               $cmd->run(["-j3", "-s"]);
+                       }
+                       $this->info("OK\n");
+               
+                       // install
+                       $this->info("Running make install ... ");
+                       if (isset($this->args->sudo)) {
+                               $cmd->setSu($this->args->sudo);
+                       }
+                       if ($this->args->verbose) {
+                               $cmd->run(["install"]);
+                       } else {
+                               $cmd->run(["install", "-s"]);
+                       }
+                       $this->info("OK\n");
+               
+               } catch (\Exception $e) {
+                       $this->error("%s\n", $e->getMessage());
+                       $this->error("%s\n", $cmd->getOutput());
+               }
+       }
 
        /**
         * Perform any cleanups
@@ -186,75 +209,6 @@ class Installer implements Command
                }
        }
 
-       /**
-        * 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
-        * @param string $command command
-        * @param array $args command arguments
-        * @param bool $sudo whether the command may need escalated privileges
-        */
-       private function exec($name, $command, array $args = null, $sudo = false) {
-               $exec = escapeshellcmd($command);
-               if ($args) {
-                       $exec .= " ". implode(" ", array_map("escapeshellarg", (array) $args));
-               }
-
-               if ($this->args->verbose) {
-                       $this->info("Running %s ...\n", $exec);
-               } else {
-                       $this->info("Running %s ... ", $name);
-               }
-
-               if ($sudo && isset($this->args->sudo)) {
-                       $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $exec), $output);
-               } elseif ($this->args->verbose) {
-                       passthru($exec ." 2>&1", $retval);
-               } else {
-                       exec($exec ." 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", $output);
-                       }
-                       exit(2);
-               }
-               if (!$this->args->verbose) {
-                       // we already have a bunch of output
-                       $this->info("OK\n");
-               }
-       }
-       
        /**
         * Construct a command from prefix common-name and suffix
         * @param type $suffix
@@ -268,6 +222,9 @@ class Installer implements Command
                return $cmd;
        }
 
+       /**
+        * Activate extension in php.ini
+        */
        private function activate() {
                if ($this->args->ini) {
                        $files = [realpath($this->args->ini)];
@@ -299,13 +256,37 @@ class Installer implements Command
                        $path = $temp->getPathname();
                        $stat = stat($file);
 
-                       $ugid = sprintf("%d:%d", $stat["uid"], $stat["gid"]);
-                       $this->exec("INI owner transfer", "chown", [$ugid, $path], true);
-                       
-                       $perm = decoct($stat["mode"] & 0777);
-                       $this->exec("INI permission transfer", "chmod", [$perm, $path], true);
-                       
-                       $this->exec("INI activation", "mv", [$path, $file], true);
+                       try {
+                               $this->info("Running INI owner transfer ... ");
+                               $ugid = sprintf("%d:%d", $stat["uid"], $stat["gid"]);
+                               $cmd = new ExecCmd("chown", $this->args->verbose);
+                               if (isset($this->args->sudo)) {
+                                       $cmd->setSu($this->args->sudo);
+                               }
+                               $cmd->run([$ugid, $path]);
+                               $this->info("OK\n");
+                               
+                               $this->info("Running INI permission transfer ... ");
+                               $perm = decoct($stat["mode"] & 0777);
+                               $cmd = new ExecCmd("chmod", $this->args->verbose);
+                               if (isset($this->args->sudo)) {
+                                       $cmd->setSu($this->args->sudo);
+                               }
+                               $cmd->run([$perm, $path]);
+                               $this->info("OK\n");
+       
+                               $this->info("Running INI activation ... ");
+                               $cmd = new ExecCmd("mv", $this->args->verbose);
+                               if (isset($this->args->sudo)) {
+                                       $cmd->setSu($this->args->sudo);
+                               }
+                               $cmd->run([$path, $file]);
+                               $this->info("OK\n");
+                       } catch (\Exception $e) {
+                               $this->error("%s\n", $e->getMessage());
+                               $this->error("%s\n", $cmd->getOutput());
+                               exit(5);
+                       }
                }
        }
 }