add bin/pharext.update
[pharext/pharext] / src / pharext / Installer.php
index 83a40ad9769d4babb16dfd063c27d10f3dd6d5f0..b5dba6d9e0565f4e70bbab6422f308aee878aa67 100644 (file)
@@ -14,13 +14,19 @@ use SplObjectStorage;
 class Installer implements Command
 {
        use CliCommand;
-       
+
+       /**
+        * Cleanups
+        * @var array
+        */
+       private $cleanup = [];
+
        /**
         * Create the command
         */
        public function __construct() {
                $this->args = new CliArgs([
-                       ["h", "help", "Display help", 
+                       ["h", "help", "Display help",
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
                        ["v", "verbose", "More output",
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
@@ -29,7 +35,7 @@ class Installer implements Command
                        ["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, 
+                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
                                "php"],
                        ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
                                CliArgs::OPTIONAL|CliArgs::MULTI|CliArgs::REQARG],
@@ -52,23 +58,56 @@ class Installer implements Command
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
                ]);
        }
-       
-       private function extract(Phar $phar) {
-               $this->debug("Extracting %s ...\n", basename($phar->getPath()));
-               return (new Task\Extract($phar))->run($this->args->verbose);
+
+       /**
+        * 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) {
-               $hooks = [];
+               $hook = [];
                foreach ($phars as $phar) {
-                       if (isset($phar["pharext_install.php"])) {
-                               $callable = include $phar["pharext_install.php"];
-                               if (is_callable($callable)) {
-                                       $hooks[] = $callable($this);
+                       if (isset($phar["pharext_package.php"])) {
+                               $sdir = include $phar["pharext_package.php"];
+                               if ($sdir instanceof SourceDir) {
+                                       $this->args->compile($sdir->getArgs());
+                                       $hook[] = $sdir;
                                }
                        }
                }
-               return $hooks;
+               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;
        }
 
        /**
@@ -77,20 +116,8 @@ class Installer implements Command
         */
        public function run($argc, array $argv) {
                try {
-                       $list = new SplObjectStorage();
-                       $phar = new Phar(Phar::running(false));
-                       $temp = $this->extract($phar);
-
-                       foreach ($phar as $entry) {
-                               $dep_file = $entry->getBaseName();
-                               if (fnmatch("*.ext.phar*", $dep_file)) {
-                                       $dep_phar = new Phar("$temp/$dep_file");
-                                       $list[$dep_phar] = $this->extract($dep_phar);
-                               }
-                       }
-                       /* the actual ext.phar at last */
-                       $list[$phar] = $temp;
-
+                       /* load the phar(s) */
+                       $list = $this->load();
                        /* installer hooks */
                        $hook = $this->hooks($list);
                } catch (\Exception $e) {
@@ -141,10 +168,8 @@ class Installer implements Command
 
                try {
                        /* post process hooks */
-                       foreach ($hook as $callback) {
-                               if (is_callable($callback)) {
-                                       $callback($this);
-                               }
+                       foreach ($hook as $sdir) {
+                               $sdir->setArgs($this->args);
                        }
                } catch (\Exception $e) {
                        $this->error("%s\n", $e->getMessage());
@@ -157,7 +182,6 @@ class Installer implements Command
                                $this->info("Installing %s ...\n", basename($phar->getPath()));
                                $this->install($list[$phar]);
                                $this->activate($list[$phar]);
-                               $this->cleanup($list[$phar]);
                                $this->info("Successfully installed %s!\n", basename($phar->getPath()));
                        }
                } catch (\Exception $e) {
@@ -165,44 +189,32 @@ class Installer implements Command
                        exit(self::EINSTALL);
                }
        }
-       
+
        /**
         * Phpize + trinity
         */
        private function install($temp) {
                // phpize
-               $this->info("Running phpize ...\n");
                $phpize = new Task\Phpize($temp, $this->args->prefix, $this->args->{"common-name"});
-               $phpize->run($this->args->verbose);
+               $phpize->run($this->verbosity());
 
                // configure
-               $this->info("Running configure ...\n");
-               $configure = new Task\Configure($temp, $this->args->configure, $this->args->prefix, $this->args{"common-name"});
-               $configure->run($this->args->verbose);
+               $configure = new Task\Configure($temp, $this->args->configure, $this->args->prefix, $this->args->{"common-name"});
+               $configure->run($this->verbosity());
 
                // make
-               $this->info("Running make ...\n");
                $make = new Task\Make($temp);
-               $make->run($this->args->verbose);
+               $make->run($this->verbosity());
 
                // install
-               $this->info("Running make install ...\n");
                $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
                $install = new Task\Make($temp, ["install"], $sudo);
-               $install->run($this->args->verbose);
-       }
-
-       private function cleanup($temp) {
-               if (is_dir($temp)) {
-                       $this->rm($temp);
-               } elseif (file_exists($temp)) {
-                       unlink($temp);
-               }
+               $install->run($this->verbosity());
        }
 
        private function activate($temp) {
                if ($this->args->ini) {
-                       $files = [realpath($this->args->ini)];
+                       $files = [$this->args->ini];
                } else {
                        $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
                        $files[] = php_ini_loaded_file();
@@ -210,10 +222,9 @@ class Installer implements Command
 
                $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
                $type = $this->metadata("type") ?: "extension";
-               
-               $this->info("Running INI activation ...\n");
+
                $activate = new Task\Activate($temp, $files, $type, $this->args->prefix, $this->args{"common-name"}, $sudo);
-               if (!$activate->run($this->args->verbose)) {
+               if (!$activate->run($this->verbosity())) {
                        $this->info("Extension already activated ...\n");
                }
        }