6 use pharext\Cli\Args
as CliArgs
;
7 use pharext\Cli\Command
as CliCommand
;
10 * The extension install command executed by the extension phar
12 class Installer
implements Command
17 * The temporary directory we should operate in
23 * The directory we came from
31 public function __construct() {
32 $this->args
= new CliArgs([
33 ["h", "help", "Display help",
34 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
35 ["v", "verbose", "More output",
36 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
37 ["q", "quiet", "Less output",
38 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
39 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
40 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
],
41 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
42 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
44 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
45 CliArgs
::OPTIONAL|CliArgs
::MULTI|CliArgs
::REQARG
],
46 ["s", "sudo", "Installation might need increased privileges",
47 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::OPTARG
,
49 ["i", "ini", "Activate in this php.ini instead of loaded default php.ini",
50 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
],
55 * Cleanup temp directory
57 public function __destruct() {
63 * @see \pharext\Command::run()
65 public function run($argc, array $argv) {
66 $this->cwd
= getcwd();
67 $this->tmp
= $this->tempname(basename(Phar
::running(false)));
69 $phar = new Phar(Phar
::running(false));
70 foreach ($phar as $entry) {
71 if (fnmatch("*.ext.phar*", $entry->getBaseName())) {
72 $temp = $this->newtemp($entry->getBaseName());
73 $phar->extractTo($temp, $entry->getFilename(), true);
74 $phars[$temp] = new Phar($temp."/".$entry->getFilename());
77 $phars[$this->tmp
] = $phar;
79 foreach ($phars as $phar) {
80 if (isset($phar["pharext_install.php"])) {
81 $callable = include $phar["pharext_install.php"];
82 if (is_callable($callable)) {
83 $recv[] = $callable($this);
89 $prog = array_shift($argv);
90 foreach ($this->args
->parse(--$argc, $argv) as $error) {
94 if ($this->args
["help"]) {
100 foreach ($this->args
->validate() as $error) {
105 if (!$this->args
["quiet"]) {
108 foreach ($errs as $err) {
109 $this->error("%s\n", $err);
111 if (!$this->args
["quiet"]) {
118 foreach ($recv as $r) {
122 foreach ($phars as $temp => $phar) {
123 $this->installPackage($phar, $temp);
128 * Prepares, configures, builds and installs the extension
130 private function installPackage(Phar
$phar, $temp) {
131 $this->info("Installing %s ... \n", basename($phar->getAlias()));
133 $phar->extractTo($temp, null, true);
134 } catch (\Exception
$e) {
135 $this->error("%s\n", $e->getMessage());
145 $this->exec("phpize", $this->php("ize"));
148 $args = ["--with-php-config=". $this->php("-config")];
149 if ($this->args
->configure
) {
150 $args = array_merge($args, $this->args
->configure
);
152 $this->exec("configure", "./configure", $args);
155 if ($this->args
->verbose
) {
156 $this->exec("make", "make", ["-j3"]);
158 $this->exec("make", "make", ["-j3", "-s"]);
162 if ($this->args
->verbose
) {
163 $this->exec("install", "make", ["install"], true);
165 $this->exec("install", "make", ["install", "-s"], true);
172 $this->cleanup($temp);
176 * Perform any cleanups
178 private function cleanup($temp = null) {
184 $this->info("Cleaning up %s ...\n", $temp);
190 * Execute a program with escalated privileges handling interactive password prompt
191 * @param string $command
192 * @param string $output
195 private function sudo($command, &$output) {
196 if (!($proc = proc_open($command, [STDIN
,["pipe","w"],["pipe","w"]], $pipes))) {
201 while (!feof($stdout)) {
202 $R = [$stdout]; $W = []; $E = [];
203 if (!stream_select($R, $W, $E, null)) {
206 $data = fread($stdout, 0x1000);
207 /* only check a few times */
208 if ($passwd++
< 10) {
209 if (stristr($data, "password")) {
210 printf("\n%s", $data);
215 return proc_close($proc);
218 * Execute a system command
219 * @param string $name pretty name
220 * @param string $command command
221 * @param array $args command arguments
222 * @param bool $sudo whether the command may need escalated privileges
224 private function exec($name, $command, array $args = null, $sudo = false) {
225 $exec = escapeshellcmd($command);
227 $exec .= " ". implode(" ", array_map("escapeshellarg", (array) $args));
230 if ($this->args
->verbose
) {
231 $this->info("Running %s ...\n", $exec);
233 $this->info("Running %s ... ", $name);
236 if ($sudo && isset($this->args
->sudo
)) {
237 $retval = $this->sudo(sprintf($this->args
->sudo
." 2>&1", $exec), $output);
238 } elseif ($this->args
->verbose
) {
239 passthru($exec ." 2>&1", $retval);
241 exec($exec ." 2>&1", $output, $retval);
242 $output = implode("\n", $output);
246 $this->error("Command %s failed with (%s)\n", $command, $retval);
247 if (isset($output) && !$this->args
->quiet
) {
248 printf("%s\n", $output);
252 if (!$this->args
->verbose
) {
253 // we already have a bunch of output
259 * Construct a command from prefix common-name and suffix
260 * @param type $suffix
263 private function php($suffix) {
264 $cmd = $this->args
["common-name"] . $suffix;
265 if (isset($this->args
->prefix
)) {
266 $cmd = $this->args
->prefix
. "/bin/" . $cmd;
272 * Activate extension in php.ini
274 private function activate() {
275 if ($this->args
->ini
) {
276 $files = [realpath($this->args
->ini
)];
278 $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
279 $files[] = php_ini_loaded_file();
282 $extension = basename(current(glob("modules/*.so")));
283 $pattern = preg_quote($extension);
285 foreach ($files as $index => $file) {
286 $temp = new Tempfile("phpini");
287 foreach (file($file) as $line) {
288 if (preg_match("/^\s*extension\s*=\s*[\"']?{$pattern}[\"']?\s*(;.*)?\$/", $line)) {
290 $this->info("Extension already activated\n");
293 fwrite($temp->getStream(), $line);
297 // not found, add extension line to the last process file
298 if (isset($temp, $file)) {
299 fprintf($temp->getStream(), "extension=%s\n", $extension);
300 $temp->closeStream();
302 $path = $temp->getPathname();
305 $ugid = sprintf("%d:%d", $stat["uid"], $stat["gid"]);
306 $this->exec("INI owner transfer", "chown", [$ugid, $path], true);
308 $perm = decoct($stat["mode"] & 0777);
309 $this->exec("INI permission transfer", "chmod", [$perm, $path], true);
311 $this->exec("INI activation", "mv", [$path, $file], true);