d8c7feb3ee4768c0f09fdd419cba191b916debc3
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
],
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 directory",
30 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
32 ["n", "common-name", "PHP common program name, e.g. php5",
33 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
35 ["c", "configure", "Additional extension configure flags",
36 CliArgs
::OPTIONAL|CliArgs
::MULTI|CliArgs
::REQARG
],
37 ["s", "sudo", "Installation might need increased privileges",
38 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::OPTARG
,
45 * @see \pharext\Command::run()
47 public function run($argc, array $argv) {
48 $prog = array_shift($argv);
49 foreach ($this->args
->parse(--$argc, $argv) as $error) {
50 $this->error("%s\n", $error);
53 if ($this->args
["help"]) {
54 $this->args
->help($prog);
58 foreach ($this->args
->validate() as $error) {
59 $this->error("%s\n", $error);
63 if (!$this->args
["quiet"]) {
64 $this->args
->help($prog);
69 $this->installPackage();
74 * @see \pharext\Command::getArgs()
76 public function getArgs() {
82 * @see \pharext\Command::info()
84 public function info($fmt) {
85 if (!$this->args
->quiet
) {
86 vprintf($fmt, array_slice(func_get_args(), 1));
92 * @see \pharext\Command::error()
94 public function error($fmt) {
95 if (!$this->args
->quiet
) {
96 vfprintf(STDERR
, "ERROR: $fmt", array_slice(func_get_args(), 1));
101 * Extract the phar to a temporary directory
103 private function extract() {
104 if (!$file = Phar
::running(false)) {
105 $this->error("Did your run the ext.phar?\n");
108 $temp = sys_get_temp_dir()."/".basename($file, ".ext.phar");
109 is_dir($temp) or mkdir($temp, 0750, true);
110 $phar = new Phar($file);
111 $phar->extractTo($temp, null, true);
116 * Execute a system command
117 * @param string $name pretty name
118 * @param string $command full command
119 * @param bool $sudo whether the command may need escalated privileges
121 private function exec($name, $command, $sudo = false) {
122 $this->info("Running %s ...%s", $this->args
->verbose ?
$command : $name, $this->args
->verbose ?
"\n" : " ");
123 if ($sudo && isset($this->args
->sudo
)) {
124 if ($proc = proc_open(sprintf($this->args
->sudo
, $command)." 2>&1", [STDIN
,STDOUT
,STDERR
], $pipes)) {
125 $retval = proc_close($proc);
129 } elseif ($this->args
->verbose
) {
130 passthru($command ." 2>&1", $retval);
132 exec($command ." 2>&1", $output, $retval);
135 $this->error("Command %s failed with (%s)\n", $command, $retval);
136 if (isset($output) && !$this->args
->quiet
) {
137 printf("%s\n", implode("\n", $output));
145 * Prepares, configures, builds and installs the extension
147 private function installPackage() {
149 $this->exec("phpize", "{$this->args->prefix}/bin/{$this->args->{'common-name'}}ize");
150 $this->exec("configure", "./configure --with-php-config={$this->args->prefix}/bin/{$this->args->{'common-name'}}-config ". implode(" ", (array) $this->args
->configure
));
151 $this->exec("make", "make -sj3");
152 $this->exec("install", "make -s install", true);