8 * The extension install command executed by the extension phar
10 class Installer
implements Command
13 * Command line arguments
14 * @var pharext\CliArgs
21 public function __construct() {
22 $this->args
= new CliArgs([
23 ["h", "help", "Display help",
24 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
25 ["v", "verbose", "More output",
26 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
27 ["q", "quiet", "Less output",
28 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
29 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
30 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
],
31 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
32 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
34 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
35 CliArgs
::OPTIONAL|CliArgs
::MULTI|CliArgs
::REQARG
],
36 ["s", "sudo", "Installation might need increased privileges",
37 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::OPTARG
,
44 * @see \pharext\Command::run()
46 public function run($argc, array $argv) {
47 $prog = array_shift($argv);
48 foreach ($this->args
->parse(--$argc, $argv) as $error) {
49 $this->error("%s\n", $error);
52 if ($this->args
["help"]) {
53 $this->args
->help($prog);
57 foreach ($this->args
->validate() as $error) {
58 $this->error("%s\n", $error);
62 if (!$this->args
["quiet"]) {
63 $this->args
->help($prog);
68 $this->installPackage();
73 * @see \pharext\Command::getArgs()
75 public function getArgs() {
81 * @see \pharext\Command::info()
83 public function info($fmt) {
84 if (!$this->args
->quiet
) {
85 vprintf($fmt, array_slice(func_get_args(), 1));
91 * @see \pharext\Command::error()
93 public function error($fmt) {
94 if (!$this->args
->quiet
) {
95 vfprintf(STDERR
, "ERROR: $fmt", array_slice(func_get_args(), 1));
100 * Extract the phar to a temporary directory
102 private function extract() {
103 if (!$file = Phar
::running(false)) {
104 $this->error("Did your run the ext.phar?\n");
107 $temp = sys_get_temp_dir()."/".basename($file, ".ext.phar");
108 is_dir($temp) or mkdir($temp, 0750, true);
109 $phar = new Phar($file);
110 $phar->extractTo($temp, null, true);
115 * Execute a system command
116 * @param string $name pretty name
117 * @param string $command full command
118 * @param bool $sudo whether the command may need escalated privileges
120 private function exec($name, $command, $sudo = false) {
121 $this->info("Running %s ...%s", $this->args
->verbose ?
$command : $name, $this->args
->verbose ?
"\n" : " ");
122 if ($sudo && isset($this->args
->sudo
)) {
123 if (($proc = proc_open(sprintf($this->args
->sudo
, $command)." 2>&1", [STDIN
,STDOUT
,STDERR
], $pipes))) {
124 $retval = proc_close($proc);
128 } elseif ($this->args
->verbose
) {
129 passthru($command ." 2>&1", $retval);
131 exec($command ." 2>&1", $output, $retval);
134 $this->error("Command %s failed with (%s)\n", $command, $retval);
135 if (isset($output) && !$this->args
->quiet
) {
136 printf("%s\n", implode("\n", $output));
144 * Construct a command from prefix common-name and suffix
145 * @param type $suffix
148 private function php($suffix) {
149 $cmd = $this->args
["common-name"] . $suffix;
150 if (isset($this->args
->prefix
)) {
151 $cmd = $this->args
->prefix
. "/bin/" . $cmd;
157 * Prepares, configures, builds and installs the extension
159 private function installPackage() {
161 $this->exec("phpize", $this->php("ize"));
162 $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ".
163 implode(" ", (array) $this->args
->configure
));
164 $this->exec("make", "make -sj3");
165 $this->exec("install", "make -s install", true);