download progress, cleanups
[pharext/pharext] / src / pharext / Installer.php
index c30e11a5137d5c7a57f80013e3b6424d2d3f9000..94f814950178a8a978a0084cef85dd3fa45ecf90 100644 (file)
@@ -3,6 +3,8 @@
 namespace pharext;
 
 use Phar;
+use pharext\Cli\Args as CliArgs;
+use pharext\Cli\Command as CliCommand;
 
 /**
  * The extension install command executed by the extension phar
@@ -43,7 +45,9 @@ class Installer implements Command
                                CliArgs::OPTIONAL|CliArgs::MULTI|CliArgs::REQARG],
                        ["s", "sudo", "Installation might need increased privileges",
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::OPTARG,
-                               "sudo -S %s"]
+                               "sudo -S %s"],
+                       ["i", "ini", "Activate in this php.ini instead of loaded default php.ini",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
                ]);
        }
        
@@ -65,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());
                        }
@@ -120,22 +124,6 @@ class Installer implements Command
                }
        }
 
-       /**
-        * Create a new temp directory
-        * @param string $prefix
-        * @return string
-        */
-       private function newtemp($prefix) {
-               $temp = $this->tempname($prefix);
-               if (!is_dir($temp)) {
-                       if (!mkdir($temp, 0750, true)) {
-                               $this->error(null);
-                               exit(3);
-                       }
-               }
-               return $temp;
-       }
-       
        /**
         * Prepares, configures, builds and installs the extension
         */
@@ -152,16 +140,59 @@ class Installer implements Command
                        $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->build();
+               $this->activate();
                $this->cleanup($temp);
-
-               $this->info("Don't forget to activiate the extension in your php.ini!\n\n");
+       }
+       
+       /**
+        * 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());
+               }
        }
 
        /**
@@ -179,88 +210,83 @@ class Installer implements Command
        }
 
        /**
-        * 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
+        * Construct a command from prefix common-name and suffix
+        * @param type $suffix
+        * @return string
         */
-       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;
+       private function php($suffix) {
+               $cmd = $this->args["common-name"] . $suffix;
+               if (isset($this->args->prefix)) {
+                       $cmd = $this->args->prefix . "/bin/" . $cmd;
                }
-               return proc_close($proc);
+               return $cmd;
        }
+
        /**
-        * Execute a system command
-        * @param string $name pretty name
-        * @param string $command full command
-        * @param bool $sudo whether the command may need escalated privileges
+        * Activate extension in php.ini
         */
-       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)) {
-                       $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $command), $output);
-               } elseif ($this->args->verbose) {
-                       passthru($command ." 2>&1", $retval);
+       private function activate() {
+               if ($this->args->ini) {
+                       $files = [realpath($this->args->ini)];
                } else {
-                       exec($command ." 2>&1", $output, $retval);
-                       $output = implode("\n", $output);
+                       $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
+                       $files[] = php_ini_loaded_file();
                }
-               if ($retval) {
-                       $this->error("Command %s failed with (%s)\n", $command, $retval);
-                       if (isset($output) && !$this->args->quiet) {
-                               printf("%s\n", $output);
+
+               $extension = basename(current(glob("modules/*.so")));
+               $pattern = preg_quote($extension);
+
+               foreach ($files as $index => $file) {
+                       $temp = new Tempfile("phpini");
+                       foreach (file($file) as $line) {
+                               if (preg_match("/^\s*extension\s*=\s*[\"']?{$pattern}[\"']?\s*(;.*)?\$/", $line)) {
+                                       // already there
+                                       $this->info("Extension already activated\n");
+                                       return;
+                               }
+                               fwrite($temp->getStream(), $line);
                        }
-                       exit(2);
                }
-               $this->info("OK\n");
-       }
+
+               // not found, add extension line to the last process file
+               if (isset($temp, $file)) {
+                       fprintf($temp->getStream(), "extension=%s\n", $extension);
+                       $temp->closeStream();
+
+                       $path = $temp->getPathname();
+                       $stat = stat($file);
+
+                       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");
        
-       /**
-        * Construct a command from prefix common-name and suffix
-        * @param type $suffix
-        * @return string
-        */
-       private function php($suffix) {
-               $cmd = $this->args["common-name"] . $suffix;
-               if (isset($this->args->prefix)) {
-                       $cmd = $this->args->prefix . "/bin/" . $cmd;
+                               $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);
+                       }
                }
-               return $cmd;
        }
 }