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) {
43 if (($hook = stream_resolve_include_path("pharext_install.php"))) {
44 $callable = include $hook;
45 if (is_callable($callable)) {
46 $recv = $callable($this);
51 $prog = array_shift($argv);
52 foreach ($this->args
->parse(--$argc, $argv) as $error) {
56 if ($this->args
["help"]) {
62 foreach ($this->args
->validate() as $error) {
67 if (!$this->args
["quiet"]) {
70 foreach ($errs as $err) {
71 $this->error("%s\n", $err);
73 if (!$this->args
["quiet"]) {
83 $this->installPackage();
88 * @see \pharext\Command::getArgs()
90 public function getArgs() {
96 * @see \pharext\Command::info()
98 public function info($fmt) {
99 if (!$this->args
->quiet
) {
100 vprintf($fmt, array_slice(func_get_args(), 1));
106 * @see \pharext\Command::error()
108 public function error($fmt) {
109 if (!$this->args
->quiet
) {
110 vfprintf(STDERR
, "ERROR: $fmt", array_slice(func_get_args(), 1));
115 * Extract the phar to a temporary directory
117 private function extract() {
118 if (!$file = Phar
::running(false)) {
119 $this->error("Did your run the ext.phar?\n");
122 $temp = sys_get_temp_dir()."/".basename($file, ".ext.phar");
123 is_dir($temp) or mkdir($temp, 0750, true);
124 $phar = new Phar($file);
125 $phar->extractTo($temp, null, true);
130 * Execute a system command
131 * @param string $name pretty name
132 * @param string $command full command
133 * @param bool $sudo whether the command may need escalated privileges
135 private function exec($name, $command, $sudo = false) {
136 $this->info("Running %s ...%s", $this->args
->verbose ?
$command : $name, $this->args
->verbose ?
"\n" : " ");
137 if ($sudo && isset($this->args
->sudo
)) {
138 if (($proc = proc_open(sprintf($this->args
->sudo
, $command)." 2>&1", [STDIN
,STDOUT
,STDERR
], $pipes))) {
139 $retval = proc_close($proc);
143 } elseif ($this->args
->verbose
) {
144 passthru($command ." 2>&1", $retval);
146 exec($command ." 2>&1", $output, $retval);
149 $this->error("Command %s failed with (%s)\n", $command, $retval);
150 if (isset($output) && !$this->args
->quiet
) {
151 printf("%s\n", implode("\n", $output));
159 * Construct a command from prefix common-name and suffix
160 * @param type $suffix
163 private function php($suffix) {
164 $cmd = $this->args
["common-name"] . $suffix;
165 if (isset($this->args
->prefix
)) {
166 $cmd = $this->args
->prefix
. "/bin/" . $cmd;
172 * Prepares, configures, builds and installs the extension
174 private function installPackage() {
176 $this->exec("phpize", $this->php("ize"));
177 $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ".
178 implode(" ", (array) $this->args
->configure
));
179 $this->exec("make", "make -sj3");
180 $this->exec("install", "make -s install", true);