589abd611a3384d888826271324cb3aba08d5dfd
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
,
53 * Cleanup temp directory
55 public function __destruct() {
61 * @see \pharext\Command::run()
63 public function run($argc, array $argv) {
64 $this->cwd
= getcwd();
65 $this->tmp
= $this->tempname(basename(Phar
::running(false)));
67 $phar = new Phar(Phar
::running(false));
68 foreach ($phar as $entry) {
69 if (fnmatch("*.ext.phar*", $entry->getBaseName())) {
70 $temp = $this->newtemp($entry->getBaseName());
71 $phar->extractTo($temp, $entry->getFilename(), true);
72 $phars[$temp] = new Phar($temp."/".$entry->getFilename());
75 $phars[$this->tmp
] = $phar;
77 foreach ($phars as $phar) {
78 if (isset($phar["pharext_install.php"])) {
79 $callable = include $phar["pharext_install.php"];
80 if (is_callable($callable)) {
81 $recv[] = $callable($this);
87 $prog = array_shift($argv);
88 foreach ($this->args
->parse(--$argc, $argv) as $error) {
92 if ($this->args
["help"]) {
98 foreach ($this->args
->validate() as $error) {
103 if (!$this->args
["quiet"]) {
106 foreach ($errs as $err) {
107 $this->error("%s\n", $err);
109 if (!$this->args
["quiet"]) {
116 foreach ($recv as $r) {
120 foreach ($phars as $temp => $phar) {
121 $this->installPackage($phar, $temp);
126 * Prepares, configures, builds and installs the extension
128 private function installPackage(Phar
$phar, $temp) {
129 $this->info("Installing %s ... \n", basename($phar->getAlias()));
131 $phar->extractTo($temp, null, true);
132 } catch (\Exception
$e) {
133 $this->error("%s\n", $e->getMessage());
142 $this->exec("phpize", $this->php("ize"));
143 $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ".
144 implode(" ", (array) $this->args
->configure
));
145 $this->exec("make", $this->args
->verbose ?
"make -j3" : "make -sj3");
146 $this->exec("install", $this->args
->verbose ?
"make install" : "make -s install", true);
148 $this->cleanup($temp);
150 $this->info("Don't forget to activiate the extension in your php.ini!\n\n");
154 * Perform any cleanups
156 private function cleanup($temp = null) {
162 $this->info("Cleaning up %s ...\n", $temp);
168 * Execute a program with escalated privileges handling interactive password prompt
169 * @param string $command
170 * @param string $output
173 private function sudo($command, &$output) {
174 if (!($proc = proc_open($command, [STDIN
,["pipe","w"],["pipe","w"]], $pipes))) {
179 while (!feof($stdout)) {
180 $R = [$stdout]; $W = []; $E = [];
181 if (!stream_select($R, $W, $E, null)) {
184 $data = fread($stdout, 0x1000);
185 /* only check a few times */
186 if ($passwd++
< 10) {
187 if (stristr($data, "password")) {
188 printf("\n%s", $data);
193 return proc_close($proc);
196 * Execute a system command
197 * @param string $name pretty name
198 * @param string $command full command
199 * @param bool $sudo whether the command may need escalated privileges
201 private function exec($name, $command, $sudo = false) {
202 $this->info("Running %s ...%s", $this->args
->verbose ?
$command : $name, $this->args
->verbose ?
"\n" : " ");
203 if ($sudo && isset($this->args
->sudo
)) {
204 $retval = $this->sudo(sprintf($this->args
->sudo
." 2>&1", $command), $output);
205 } elseif ($this->args
->verbose
) {
206 passthru($command ." 2>&1", $retval);
208 exec($command ." 2>&1", $output, $retval);
209 $output = implode("\n", $output);
212 $this->error("Command %s failed with (%s)\n", $command, $retval);
213 if (isset($output) && !$this->args
->quiet
) {
214 printf("%s\n", $output);
222 * Construct a command from prefix common-name and suffix
223 * @param type $suffix
226 private function php($suffix) {
227 $cmd = $this->args
["common-name"] . $suffix;
228 if (isset($this->args
->prefix
)) {
229 $cmd = $this->args
->prefix
. "/bin/" . $cmd;