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
{
use CliCommand;
+ /**
+ * The temporary directory we should operate in
+ * @var string
+ */
+ private $tmp;
+
+ /**
+ * The directory we came from
+ * @var string
+ */
+ private $cwd;
+
/**
* Create the 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 = new Tempdir(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[(string) $temp] = new Phar($temp."/".$entry->getFilename());
+ }
+ }
+ $phars[(string) $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) {
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);
+ }
+
+ $this->build();
+ $this->activate();
+ $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
+ * Phpize + trinity
*/
- 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);
+ 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);
+ }
+ $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 {
- $retval = -1;
+ $cmd->run(["-j3", "-s"]);
}
- } elseif ($this->args->verbose) {
- passthru($command ." 2>&1", $retval);
- } else {
- exec($command ." 2>&1", $output, $retval);
- }
- if ($retval) {
- $this->error("Command %s failed with (%s)\n", $command, $retval);
- if (isset($output) && !$this->args->quiet) {
- printf("%s\n", implode("\n", $output));
+ $this->info("OK\n");
+
+ // install
+ $this->info("Running make install ... ");
+ if (isset($this->args->sudo)) {
+ $cmd->setSu($this->args->sudo);
}
- exit(2);
+ 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());
}
- $this->info("OK\n");
}
-
+
+ /**
+ * 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 $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);
+
+ 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);
+ }
+ }
}
}