X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fpharext%2FInstaller.php;h=4caebc0de6f4b251004b9d5e931102f957a138b2;hb=93b685d846bb6b0868a6111d52a19a6f4e7d7aee;hp=7ff112a38c391ae65bd0d36b46a7d176d910c9e6;hpb=4aa1e062368f8ee8e792951d675a811dd97f8699;p=pharext%2Fpharext diff --git a/src/pharext/Installer.php b/src/pharext/Installer.php index 7ff112a..4caebc0 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], ]); } @@ -73,7 +77,7 @@ class Installer implements Command $phars[$this->tmp] = $phar; foreach ($phars as $phar) { - if (($hook = $phar["pharext_install.php"])) { + if (isset($phar["pharext_install.php"])) { $callable = include $phar["pharext_install.php"]; if (is_callable($callable)) { $recv[] = $callable($this); @@ -120,22 +124,6 @@ class Installer implements Command } } - /** - * Create a new temp directory - * @param string $prefix - * @return string - */ - private function newtemp($prefix) { - $temp = $this->tempname($prefix); - if (!is_dir($temp)) { - if (!mkdir($temp, 0750, true)) { - $this->error(null); - exit(3); - } - } - return $temp; - } - /** * Prepares, configures, builds and installs the extension */ @@ -153,15 +141,35 @@ class Installer implements Command exit(4); } + // phpize $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($temp); + // 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"]); + } - $this->info("Don't forget to activiate the extension in your php.ini!\n\n"); + // 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); } /** @@ -178,25 +186,6 @@ class Installer implements Command } } - /** - * 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)) { - $this->error(null); - } - } - /** * Execute a program with escalated privileges handling interactive password prompt * @param string $command @@ -228,19 +217,31 @@ class Installer implements Command /** * Execute a system command * @param string $name pretty name - * @param string $command full command + * @param string $command command + * @param array $args command arguments * @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" : " "); + private function exec($name, $command, array $args = null, $sudo = false) { + $exec = escapeshellcmd($command); + if ($args) { + $exec .= " ". implode(" ", array_map("escapeshellarg", (array) $args)); + } + + if ($this->args->verbose) { + $this->info("Running %s ...\n", $exec); + } else { + $this->info("Running %s ... ", $name); + } + if ($sudo && isset($this->args->sudo)) { - $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $command), $output); + $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $exec), $output); } elseif ($this->args->verbose) { - passthru($command ." 2>&1", $retval); + passthru($exec ." 2>&1", $retval); } else { - exec($command ." 2>&1", $output, $retval); + exec($exec ." 2>&1", $output, $retval); $output = implode("\n", $output); } + if ($retval) { $this->error("Command %s failed with (%s)\n", $command, $retval); if (isset($output) && !$this->args->quiet) { @@ -248,7 +249,10 @@ class Installer implements Command } exit(2); } - $this->info("OK\n"); + if (!$this->args->verbose) { + // we already have a bunch of output + $this->info("OK\n"); + } } /** @@ -263,4 +267,48 @@ 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); + + $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); + } + } }