X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fpharext%2FInstaller.php;h=94f814950178a8a978a0084cef85dd3fa45ecf90;hb=8b991a8b633610f7309d76f15b388bffd073d1f7;hp=57bbf06d23664f0a37395f0d5c1e0917a16fab0b;hpb=b6a353107552e33aec01f9885362d91af0dad21c;p=pharext%2Fpharext diff --git a/src/pharext/Installer.php b/src/pharext/Installer.php index 57bbf06..94f8149 100644 --- a/src/pharext/Installer.php +++ b/src/pharext/Installer.php @@ -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], ]); } @@ -59,10 +63,25 @@ class Installer implements Command * @see \pharext\Command::run() */ public function run($argc, array $argv) { - if (($hook = stream_resolve_include_path("pharext_install.php"))) { - $callable = include $hook; - if (is_callable($callable)) { - $recv = $callable($this); + $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()); + } + } + $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); + } } } @@ -96,146 +115,100 @@ class Installer implements Command } if (isset($recv)) { - $recv($this); - } - - $this->installPackage(); - } - - /** - * Prepares, configures, builds and installs the extension - */ - private function installPackage() { - $this->extract(); - - if (!chdir($this->tmp)) { - $this->error(null); - exit(4); + foreach ($recv as $r) { + $r($this); + } } - - $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->cleanup(); - - $this->info("\nDon't forget to activiate the extension in your php.ini!\n"); - } - - /** - * Perform any cleanups - */ - private function cleanup() { - if (is_dir($this->tmp)) { - chdir($this->cwd); - $this->info("Cleaning up %s ...\n", $this->tmp); - $this->rm($this->tmp); + 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"); - exit(3); - } - - $temp = $this->tempname(basename($file)); - if (!is_dir($temp)) { - if (!mkdir($temp, 0750, true)) { - $this->error(null); - exit(3); - } - } - $this->tmp = $temp; - $this->cwd = getcwd(); - + private function installPackage(Phar $phar, $temp) { + $this->info("Installing %s ... \n", basename($phar->getAlias())); try { - $phar = new Phar($file); $phar->extractTo($temp, null, true); } catch (\Exception $e) { $this->error("%s\n", $e->getMessage()); exit(3); } - } - - /** - * 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)) { + + if (!chdir($temp)) { $this->error(null); + exit(4); } + + $this->build(); + $this->activate(); + $this->cleanup($temp); } /** - * Execute a program with escalated privileges handling interactive password prompt - * @param string $command - * @param string $output - * @return int + * Phpize + trinity */ - 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; + 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); } - $data = fread($stdout, 0x1000); - /* only check a few times */ - if ($passwd++ < 10) { - if (stristr($data, "password")) { - printf("\n%s", $data); - } + $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"]); } - $output .= $data; + $this->info("OK\n"); + + } catch (\Exception $e) { + $this->error("%s\n", $e->getMessage()); + $this->error("%s\n", $cmd->getOutput()); } - return proc_close($proc); } + /** - * 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)) { - $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $command), $output); - } elseif ($this->args->verbose) { - passthru($command ." 2>&1", $retval); - } else { - exec($command ." 2>&1", $output, $retval); - $output = implode("\n", $output); + 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", $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 @@ -248,4 +221,72 @@ class Installer implements Command } return $cmd; } + + /** + * Activate extension in php.ini + */ + 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); + + 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); + } + } + } }