8 * The extension install command executed by the extension phar
10 class Installer
implements Command
17 public function __construct() {
18 $this->args
= new CliArgs([
19 ["h", "help", "Display help",
20 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
21 ["v", "verbose", "More output",
22 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
23 ["q", "quiet", "Less output",
24 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
25 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
26 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
],
27 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
28 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
30 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
31 CliArgs
::OPTIONAL|CliArgs
::MULTI|CliArgs
::REQARG
],
32 ["s", "sudo", "Installation might need increased privileges",
33 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::OPTARG
,
40 * @see \pharext\Command::run()
42 public function run($argc, array $argv) {
44 $prog = array_shift($argv);
45 foreach ($this->args
->parse(--$argc, $argv) as $error) {
49 if ($this->args
["help"]) {
55 foreach ($this->args
->validate() as $error) {
60 if (!$this->args
["quiet"]) {
63 foreach ($errs as $err) {
64 $this->error("%s\n", $err);
66 if (!$this->args
["quiet"]) {
72 $this->installPackage();
77 * @see \pharext\Command::getArgs()
79 public function getArgs() {
85 * @see \pharext\Command::info()
87 public function info($fmt) {
88 if (!$this->args
->quiet
) {
89 vprintf($fmt, array_slice(func_get_args(), 1));
95 * @see \pharext\Command::error()
97 public function error($fmt) {
98 if (!$this->args
->quiet
) {
99 vfprintf(STDERR
, "ERROR: $fmt", array_slice(func_get_args(), 1));
104 * Extract the phar to a temporary directory
106 private function extract() {
107 if (!$file = Phar
::running(false)) {
108 $this->error("Did your run the ext.phar?\n");
111 $temp = sys_get_temp_dir()."/".basename($file, ".ext.phar");
112 is_dir($temp) or mkdir($temp, 0750, true);
113 $phar = new Phar($file);
114 $phar->extractTo($temp, null, true);
119 * Execute a system command
120 * @param string $name pretty name
121 * @param string $command full command
122 * @param bool $sudo whether the command may need escalated privileges
124 private function exec($name, $command, $sudo = false) {
125 $this->info("Running %s ...%s", $this->args
->verbose ?
$command : $name, $this->args
->verbose ?
"\n" : " ");
126 if ($sudo && isset($this->args
->sudo
)) {
127 if (($proc = proc_open(sprintf($this->args
->sudo
, $command)." 2>&1", [STDIN
,STDOUT
,STDERR
], $pipes))) {
128 $retval = proc_close($proc);
132 } elseif ($this->args
->verbose
) {
133 passthru($command ." 2>&1", $retval);
135 exec($command ." 2>&1", $output, $retval);
138 $this->error("Command %s failed with (%s)\n", $command, $retval);
139 if (isset($output) && !$this->args
->quiet
) {
140 printf("%s\n", implode("\n", $output));
148 * Construct a command from prefix common-name and suffix
149 * @param type $suffix
152 private function php($suffix) {
153 $cmd = $this->args
["common-name"] . $suffix;
154 if (isset($this->args
->prefix
)) {
155 $cmd = $this->args
->prefix
. "/bin/" . $cmd;
161 * Prepares, configures, builds and installs the extension
163 private function installPackage() {
165 $this->exec("phpize", $this->php("ize"));
166 $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ".
167 implode(" ", (array) $this->args
->configure
));
168 $this->exec("make", "make -sj3");
169 $this->exec("install", "make -s install", true);