X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fpharext%2FInstaller.php;h=7cbe5e921b99f7854215f05bcb4d2b2464e1f5d5;hb=d86a65973ac3cd8647354274ab9af6ce8c8257fc;hp=94f814950178a8a978a0084cef85dd3fa45ecf90;hpb=8b991a8b633610f7309d76f15b388bffd073d1f7;p=pharext%2Fpharext diff --git a/src/pharext/Installer.php b/src/pharext/Installer.php index 94f8149..7cbe5e9 100644 --- a/src/pharext/Installer.php +++ b/src/pharext/Installer.php @@ -3,59 +3,108 @@ namespace pharext; use Phar; -use pharext\Cli\Args as CliArgs; -use pharext\Cli\Command as CliCommand; +use SplObjectStorage; /** * The extension install command executed by the extension phar */ class Installer implements Command { - use CliCommand; - - /** - * The temporary directory we should operate in - * @var string - */ - private $tmp; + use Cli\Command; /** - * The directory we came from - * @var string + * Cleanups + * @var array */ - private $cwd; + private $cleanup = []; /** * Create the command */ public function __construct() { - $this->args = new CliArgs([ - ["h", "help", "Display help", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT], + $this->args = new Cli\Args([ + ["h", "help", "Display help", + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT], ["v", "verbose", "More output", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG], ["q", "quiet", "Less output", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG], ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG], + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::REQARG], ["n", "common-name", "PHP common program name, e.g. php5 or zts-php", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG, + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::REQARG, "php"], ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag", - CliArgs::OPTIONAL|CliArgs::MULTI|CliArgs::REQARG], + Cli\Args::OPTIONAL|Cli\Args::MULTI|Cli\Args::REQARG], ["s", "sudo", "Installation might need increased privileges", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::OPTARG, + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::OPTARG, "sudo -S %s"], ["i", "ini", "Activate in this php.ini instead of loaded default php.ini", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG], + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::REQARG], + [null, "signature", "Show package signature", + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT], + [null, "license", "Show package license", + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT], + [null, "name", "Show package name", + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT], + [null, "date", "Show package release date", + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT], + [null, "release", "Show package release version", + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT], + [null, "version", "Show pharext version", + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT], ]); } - + /** - * Cleanup temp directory + * Perform cleaniup */ - public function __destruct() { - $this->cleanup(); + 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; } /** @@ -63,230 +112,117 @@ class Installer implements Command * @see \pharext\Command::run() */ public function run($argc, array $argv) { - $this->cwd = getcwd(); - $this->tmp = $this->tempname(basename(Phar::running(false))); - - $phar = new Phar(Phar::running(false)); - foreach ($phar as $entry) { - if (fnmatch("*.ext.phar*", $entry->getBaseName())) { - $temp = new Tempdir($entry->getBaseName()); - $phar->extractTo($temp, $entry->getFilename(), true); - $phars[$temp] = new Phar($temp."/".$entry->getFilename()); - } + 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); } - $phars[$this->tmp] = $phar; - foreach ($phars as $phar) { - if (isset($phar["pharext_install.php"])) { - $callable = include $phar["pharext_install.php"]; - if (is_callable($callable)) { - $recv[] = $callable($this); - } - } - } - + /* standard arg stuff */ $errs = []; $prog = array_shift($argv); foreach ($this->args->parse(--$argc, $argv) as $error) { $errs[] = $error; } - + if ($this->args["help"]) { $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) { $errs[] = $error; } - + if ($errs) { if (!$this->args["quiet"]) { $this->header(); } foreach ($errs as $err) { $this->error("%s\n", $err); - } + } if (!$this->args["quiet"]) { $this->help($prog); } - exit(1); - } - - if (isset($recv)) { - foreach ($recv as $r) { - $r($this); - } - } - foreach ($phars as $temp => $phar) { - $this->installPackage($phar, $temp); + exit(self::EARGS); } - } - /** - * Prepares, configures, builds and installs the extension - */ - private function installPackage(Phar $phar, $temp) { - $this->info("Installing %s ... \n", basename($phar->getAlias())); try { - $phar->extractTo($temp, null, true); + /* post process hooks */ + foreach ($hook as $sdir) { + $sdir->setArgs($this->args); + } } catch (\Exception $e) { $this->error("%s\n", $e->getMessage()); - exit(3); + exit(self::EARGS); } - if (!chdir($temp)) { - $this->error(null); - exit(4); - } - - $this->build(); - $this->activate(); - $this->cleanup($temp); - } - - /** - * Phpize + trinity - */ - private function build() { + /* install packages */ 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"]); + 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())); } - $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()); + exit(self::EINSTALL); } } /** - * Perform any cleanups - */ - private function cleanup($temp = null) { - if (!isset($temp)) { - $temp = $this->tmp; - } - if (is_dir($temp)) { - chdir($this->cwd); - $this->info("Cleaning up %s ...\n", $temp); - $this->rm($temp); - } - } - - /** - * Construct a command from prefix common-name and suffix - * @param type $suffix - * @return string + * Phpize + trinity */ - private function php($suffix) { - $cmd = $this->args["common-name"] . $suffix; - if (isset($this->args->prefix)) { - $cmd = $this->args->prefix . "/bin/" . $cmd; - } - return $cmd; + 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()); } - /** - * Activate extension in php.ini - */ - private function activate() { + 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(); } - $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); - } - } - - // 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); + $sudo = isset($this->args->sudo) ? $this->args->sudo : null; + $type = $this->metadata("type") ?: "extension"; - 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); - } + $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"); } } }