X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fpharext%2FInstaller.php;h=5e86f3189f4c74fa1d7825164ed6c50cf3b54714;hb=b3b7f2dddc97f6f6c2ca83f6a708fb09ae2ed64c;hp=ccbc385213cf2300a7d7e13e46defb0b98d8c28f;hpb=7c5473b7e08ec0297c94c88248b85c2e1793ad42;p=pharext%2Fpharext diff --git a/src/pharext/Installer.php b/src/pharext/Installer.php index ccbc385..5e86f31 100644 --- a/src/pharext/Installer.php +++ b/src/pharext/Installer.php @@ -3,25 +3,35 @@ 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 */ class Installer implements Command { + use CliCommand; + /** - * Command line arguments - * @var pharext\CliArgs + * The temporary directory we should operate in + * @var string */ - private $args; - + private $tmp; + + /** + * The directory we came from + * @var string + */ + private $cwd; + /** * Create the 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,111 +45,147 @@ 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], ]); } + /** + * Cleanup temp directory + */ + public function __destruct() { + $this->cleanup(); + } + /** * @inheritdoc * @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 = $this->newtemp($entry->getBaseName()); + $phar->extractTo($temp, $entry->getFilename(), true); + $phars[$temp] = new Phar($temp."/".$entry->getFilename()); + } + } + $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); + } + } + } + + $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; } foreach ($this->args->validate() as $error) { - $this->error("%s\n", $error); + $errs[] = $error; } - if (isset($error)) { + if ($errs) { if (!$this->args["quiet"]) { - $this->args->help($prog); + $this->header(); + } + foreach ($errs as $err) { + $this->error("%s\n", $err); + } + if (!$this->args["quiet"]) { + $this->help($prog); } exit(1); } - $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)); + if (isset($recv)) { + foreach ($recv as $r) { + $r($this); + } } - } - - /** - * @inheritdoc - * @see \pharext\Command::error() - */ - public function error($fmt) { - if (!$this->args->quiet) { - vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1)); + foreach ($phars as $temp => $phar) { + $this->installPackage($phar, $temp); } } - + /** - * Extract the phar to a temporary directory + * Prepares, configures, builds and installs the extension */ - private function extract() { - if (!$file = Phar::running(false)) { - $this->error("Did your run the ext.phar?\n"); + private function installPackage(Phar $phar, $temp) { + $this->info("Installing %s ... \n", basename($phar->getAlias())); + try { + $phar->extractTo($temp, null, true); + } catch (\Exception $e) { + $this->error("%s\n", $e->getMessage()); 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); + + if (!chdir($temp)) { + $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->activate(); + + // cleanup + $this->cleanup($temp); } - + /** - * Execute a system command - * @param string $name pretty name - * @param string $command full command - * @param bool $sudo whether the command may need escalated privileges + * Perform any cleanups */ - 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); - } else { - exec($command ." 2>&1", $output, $retval); + private function cleanup($temp = null) { + if (!isset($temp)) { + $temp = $this->tmp; } - 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); + if (is_dir($temp)) { + chdir($this->cwd); + $this->info("Cleaning up %s ...\n", $temp); + $this->rm($temp); } - $this->info("OK\n"); } - + /** * Construct a command from prefix common-name and suffix * @param type $suffix @@ -152,16 +198,48 @@ class Installer implements Command } return $cmd; } - + /** - * Prepares, configures, builds and installs the extension + * Activate extension in php.ini */ - 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); + private function activate() { + if ($this->args->ini) { + $files = [realpath($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); + + $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); + } } }