8 * The extension install command executed by the extension phar
10 class Installer
implements Command
15 * The temporary directory we should operate in
21 * The directory we came from
29 public function __construct() {
30 $this->args
= new CliArgs([
31 ["h", "help", "Display help",
32 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
33 ["v", "verbose", "More output",
34 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
35 ["q", "quiet", "Less output",
36 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
37 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
38 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
],
39 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
40 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
42 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
43 CliArgs
::OPTIONAL|CliArgs
::MULTI|CliArgs
::REQARG
],
44 ["s", "sudo", "Installation might need increased privileges",
45 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::OPTARG
,
51 * Cleanup temp directory
53 public function __destruct() {
59 * @see \pharext\Command::run()
61 public function run($argc, array $argv) {
62 $this->cwd
= getcwd();
63 $this->tmp
= $this->tempname(basename(Phar
::running(false)));
65 $phar = new Phar(Phar
::running(false));
66 foreach ($phar as $entry) {
67 if (fnmatch("*.ext.phar*", $entry->getBaseName())) {
68 $temp = $this->newtemp($entry->getBaseName());
69 $phar->extractTo($temp, $entry->getFilename(), true);
70 $phars[$temp] = new Phar($temp."/".$entry->getFilename());
73 $phars[$this->tmp
] = $phar;
75 foreach ($phars as $phar) {
76 if (isset($phar["pharext_install.php"])) {
77 $callable = include $phar["pharext_install.php"];
78 if (is_callable($callable)) {
79 $recv[] = $callable($this);
85 $prog = array_shift($argv);
86 foreach ($this->args
->parse(--$argc, $argv) as $error) {
90 if ($this->args
["help"]) {
96 foreach ($this->args
->validate() as $error) {
101 if (!$this->args
["quiet"]) {
104 foreach ($errs as $err) {
105 $this->error("%s\n", $err);
107 if (!$this->args
["quiet"]) {
114 foreach ($recv as $r) {
118 foreach ($phars as $temp => $phar) {
119 $this->installPackage($phar, $temp);
124 * Prepares, configures, builds and installs the extension
126 private function installPackage(Phar
$phar, $temp) {
127 $this->info("Installing %s ... \n", basename($phar->getAlias()));
129 $phar->extractTo($temp, null, true);
130 } catch (\Exception
$e) {
131 $this->error("%s\n", $e->getMessage());
140 $this->exec("phpize", $this->php("ize"));
141 $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ".
142 implode(" ", (array) $this->args
->configure
));
143 $this->exec("make", $this->args
->verbose ?
"make -j3" : "make -sj3");
144 $this->exec("install", $this->args
->verbose ?
"make install" : "make -s install", true);
146 $this->cleanup($temp);
148 $this->info("Don't forget to activiate the extension in your php.ini!\n\n");
152 * Perform any cleanups
154 private function cleanup($temp = null) {
160 $this->info("Cleaning up %s ...\n", $temp);
166 * Execute a program with escalated privileges handling interactive password prompt
167 * @param string $command
168 * @param string $output
171 private function sudo($command, &$output) {
172 if (!($proc = proc_open($command, [STDIN
,["pipe","w"],["pipe","w"]], $pipes))) {
177 while (!feof($stdout)) {
178 $R = [$stdout]; $W = []; $E = [];
179 if (!stream_select($R, $W, $E, null)) {
182 $data = fread($stdout, 0x1000);
183 /* only check a few times */
184 if ($passwd++
< 10) {
185 if (stristr($data, "password")) {
186 printf("\n%s", $data);
191 return proc_close($proc);
194 * Execute a system command
195 * @param string $name pretty name
196 * @param string $command full command
197 * @param bool $sudo whether the command may need escalated privileges
199 private function exec($name, $command, $sudo = false) {
200 $this->info("Running %s ...%s", $this->args
->verbose ?
$command : $name, $this->args
->verbose ?
"\n" : " ");
201 if ($sudo && isset($this->args
->sudo
)) {
202 $retval = $this->sudo(sprintf($this->args
->sudo
." 2>&1", $command), $output);
203 } elseif ($this->args
->verbose
) {
204 passthru($command ." 2>&1", $retval);
206 exec($command ." 2>&1", $output, $retval);
207 $output = implode("\n", $output);
210 $this->error("Command %s failed with (%s)\n", $command, $retval);
211 if (isset($output) && !$this->args
->quiet
) {
212 printf("%s\n", $output);
220 * Construct a command from prefix common-name and suffix
221 * @param type $suffix
224 private function php($suffix) {
225 $cmd = $this->args
["common-name"] . $suffix;
226 if (isset($this->args
->prefix
)) {
227 $cmd = $this->args
->prefix
. "/bin/" . $cmd;