support for running .ext.phars without ext/phar
[pharext/pharext] / src / pharext / Installer.php
index 0e7cce461abeedb3e1d7c7b8acd1f0a390bf0ef1..0171eaeb6ce695bd7211ce8faa998ca6f2e17f67 100644 (file)
@@ -2,18 +2,24 @@
 
 namespace pharext;
 
+use pharext\Cli\Args as CliArgs;
+use pharext\Cli\Command as CliCommand;
+
 use Phar;
+use SplObjectStorage;
 
 /**
  * The extension install command executed by the extension phar
  */
 class Installer implements Command
 {
+       use CliCommand;
+       
        /**
-        * Command line arguments
-        * @var pharext\CliArgs
+        * Cleanups
+        * @var array
         */
-       private $args;
+       private $cleanup = [];
        
        /**
         * Create the command
@@ -21,7 +27,7 @@ class Installer implements 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",
@@ -35,133 +41,191 @@ 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],
+                       [null, "signature", "Show package signature",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+                       [null, "license", "Show package license",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+                       [null, "name", "Show package name",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+                       [null, "date", "Show package release date",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+                       [null, "release", "Show package release version",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+                       [null, "version", "Show pharext version",
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
                ]);
        }
        
+       /**
+        * Perform cleaniup
+        */
+       function __destruct() {
+               foreach ($this->cleanup as $cleanup) {
+                       $cleanup->run();
+               }
+       }
+       
+       private function extract($phar) {
+               $temp = (new Task\Extract($phar))->run($this->verbosity());
+               $this->cleanup[] = new Task\Cleanup($temp);
+               return $temp;
+       }
+       
+       private function hooks(SplObjectStorage $phars) {
+               $hook = [];
+               foreach ($phars as $phar) {
+                       if (isset($phar["pharext_package.php"])) {
+                               $sdir = include $phar["pharext_package.php"];
+                               if ($sdir instanceof SourceDir) {
+                                       $this->args->compile($sdir->getArgs());
+                                       $hook[] = $sdir;
+                               }
+                       }
+               }
+               return $hook;
+       }
+
+       private function load() {
+               $list = new SplObjectStorage();
+               $phar = extension_loaded("Phar") 
+                       ? new Phar(Phar::running(false))
+                       : new Archive(PHAREXT_PHAR);
+               $temp = $this->extract($phar);
+
+               foreach ($phar as $entry) {
+                       $dep_file = $entry->getBaseName();
+                       if (fnmatch("*.ext.phar*", $dep_file)) {
+                               $dep_phar = extension_loaded("Phar")
+                                       ? new Phar("$temp/$dep_file")
+                                       : new Archive("$temp/$dep_file");
+                               $list[$dep_phar] = $this->extract($dep_phar);
+                       }
+               }
+               
+               /* the actual ext.phar at last */
+               $list[$phar] = $temp;
+               return $list;
+       }
+
        /**
         * @inheritdoc
         * @see \pharext\Command::run()
         */
        public function run($argc, array $argv) {
+               try {
+                       /* load the phar(s) */
+                       $list = $this->load();
+                       /* installer hooks */
+                       $hook = $this->hooks($list);
+               } catch (\Exception $e) {
+                       $this->error("%s\n", $e->getMessage());
+                       exit(self::EEXTRACT);
+               }
+
+               /* standard arg stuff */
+               $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;
                }
-               
+               try {
+                       foreach (["signature", "name", "date", "license", "release", "version"] as $opt) {
+                               if ($this->args[$opt]) {
+                                       printf("%s\n", $this->metadata($opt));
+                                       exit;
+                               }
+                       }
+               } catch (\Exception $e) {
+                       $this->error("%s\n", $e->getMessage());
+                       exit(self::EARGS);
+               }
+
                foreach ($this->args->validate() as $error) {
-                       $this->error("%s\n", $error);
+                       $errs[] = $error;
                }
-               
-               if (isset($error)) {
+
+               if ($errs) {
+                       if (!$this->args["quiet"]) {
+                               $this->header();
+                       }
+                       foreach ($errs as $err) {
+                               $this->error("%s\n", $err);
+                       }
                        if (!$this->args["quiet"]) {
-                               $this->args->help($prog);
+                               $this->help($prog);
                        }
-                       exit(1);
+                       exit(self::EARGS);
                }
-               
-               $this->installPackage();
-       }
-       
-       /**
-        * @inheritdoc
-        * @see \pharext\Command::getArgs()
-        */
-       public function getArgs() {
-               return $this->args;
-       }
-       
-       /**
-        * @inheritdoc
-        * @see \pharext\Command::info()
-        */
-       public function info($fmt) {
-               if (!$this->args->quiet) {
-                       vprintf($fmt, array_slice(func_get_args(), 1));
+
+               try {
+                       /* post process hooks */
+                       foreach ($hook as $sdir) {
+                               $sdir->setArgs($this->args);
+                       }
+               } catch (\Exception $e) {
+                       $this->error("%s\n", $e->getMessage());
+                       exit(self::EARGS);
                }
-       }
-       
-       /**
-        * @inheritdoc
-        * @see \pharext\Command::error()
-        */
-       public function error($fmt) {
-               if (!$this->args->quiet) {
-                       vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1));
+
+               /* install packages */
+               try {
+                       foreach ($list as $phar) {
+                               $this->info("Installing %s ...\n", basename($phar->getPath()));
+                               $this->install($list[$phar]);
+                               $this->activate($list[$phar]);
+                               $this->info("Successfully installed %s!\n", basename($phar->getPath()));
+                       }
+               } catch (\Exception $e) {
+                       $this->error("%s\n", $e->getMessage());
+                       exit(self::EINSTALL);
                }
        }
        
        /**
-        * Extract the phar to a temporary directory
+        * Phpize + trinity
         */
-       private function extract() {
-               if (!$file = Phar::running(false)) {
-                       $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);
+       private function install($temp) {
+               // phpize
+               $phpize = new Task\Phpize($temp, $this->args->prefix, $this->args->{"common-name"});
+               $phpize->run($this->verbosity());
+
+               // configure
+               $configure = new Task\Configure($temp, $this->args->configure, $this->args->prefix, $this->args{"common-name"});
+               $configure->run($this->verbosity());
+
+               // make
+               $make = new Task\Make($temp);
+               $make->run($this->verbosity());
+
+               // install
+               $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
+               $install = new Task\Make($temp, ["install"], $sudo);
+               $install->run($this->verbosity());
        }
-       
-       /**
-        * Execute a system command
-        * @param string $name pretty name
-        * @param string $command full command
-        * @param bool $sudo whether the command may need escalated privileges
-        */
-       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;
-                       }
-               } elseif ($this->args->verbose) {
-                       passthru($command ." 2>&1", $retval);
+
+       private function activate($temp) {
+               if ($this->args->ini) {
+                       $files = [$this->args->ini];
                } else {
-                       exec($command ." 2>&1", $output, $retval);
-               }
-               if ($retval) {
-                       $this->error("Command %s failed with (%s)\n", $command, $retval);
-                       if (isset($output) && !$this->args->quiet) {
-                               printf("%s\n", implode("\n", $output));
-                       }
-                       exit(2);
+                       $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
+                       $files[] = php_ini_loaded_file();
                }
-               $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;
+
+               $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
+               $type = $this->metadata("type") ?: "extension";
+               
+               $activate = new Task\Activate($temp, $files, $type, $this->args->prefix, $this->args{"common-name"}, $sudo);
+               if (!$activate->run($this->verbosity())) {
+                       $this->info("Extension already activated ...\n");
                }
-               return $cmd;
-       }
-       
-       /**
-        * Prepares, configures, builds and installs the extension
-        */
-       private function installPackage() {
-               $this->extract();
-               $this->exec("phpize", $this->php("ize"));
-               $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ". 
-                       implode(" ", (array) $this->args->configure));
-               $this->exec("make", "make -sj3");
-               $this->exec("install", "make -s install", true);
        }
 }