Implement packager and installer hooks
[pharext/pharext] / src / pharext / Installer.php
1 <?php
2
3 namespace pharext;
4
5 use Phar;
6
7 /**
8 * The extension install command executed by the extension phar
9 */
10 class Installer implements Command
11 {
12 use CliCommand;
13
14 /**
15 * Create the command
16 */
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,
29 "php"],
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,
34 "sudo -S %s"]
35 ]);
36 }
37
38 /**
39 * @inheritdoc
40 * @see \pharext\Command::run()
41 */
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);
47 }
48 }
49
50 $errs = [];
51 $prog = array_shift($argv);
52 foreach ($this->args->parse(--$argc, $argv) as $error) {
53 $errs[] = $error;
54 }
55
56 if ($this->args["help"]) {
57 $this->header();
58 $this->help($prog);
59 exit;
60 }
61
62 foreach ($this->args->validate() as $error) {
63 $errs[] = $error;
64 }
65
66 if ($errs) {
67 if (!$this->args["quiet"]) {
68 $this->header();
69 }
70 foreach ($errs as $err) {
71 $this->error("%s\n", $err);
72 }
73 if (!$this->args["quiet"]) {
74 $this->help($prog);
75 }
76 exit(1);
77 }
78
79 if (isset($recv)) {
80 $recv($this);
81 }
82
83 $this->installPackage();
84 }
85
86 /**
87 * @inheritdoc
88 * @see \pharext\Command::getArgs()
89 */
90 public function getArgs() {
91 return $this->args;
92 }
93
94 /**
95 * @inheritdoc
96 * @see \pharext\Command::info()
97 */
98 public function info($fmt) {
99 if (!$this->args->quiet) {
100 vprintf($fmt, array_slice(func_get_args(), 1));
101 }
102 }
103
104 /**
105 * @inheritdoc
106 * @see \pharext\Command::error()
107 */
108 public function error($fmt) {
109 if (!$this->args->quiet) {
110 vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1));
111 }
112 }
113
114 /**
115 * Extract the phar to a temporary directory
116 */
117 private function extract() {
118 if (!$file = Phar::running(false)) {
119 $this->error("Did your run the ext.phar?\n");
120 exit(3);
121 }
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);
126 chdir($temp);
127 }
128
129 /**
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
134 */
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);
140 } else {
141 $retval = -1;
142 }
143 } elseif ($this->args->verbose) {
144 passthru($command ." 2>&1", $retval);
145 } else {
146 exec($command ." 2>&1", $output, $retval);
147 }
148 if ($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));
152 }
153 exit(2);
154 }
155 $this->info("OK\n");
156 }
157
158 /**
159 * Construct a command from prefix common-name and suffix
160 * @param type $suffix
161 * @return string
162 */
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;
167 }
168 return $cmd;
169 }
170
171 /**
172 * Prepares, configures, builds and installs the extension
173 */
174 private function installPackage() {
175 $this->extract();
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);
181 }
182 }