refactor some commonly used code into a trait
[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 $errs = [];
44 $prog = array_shift($argv);
45 foreach ($this->args->parse(--$argc, $argv) as $error) {
46 $errs[] = $error;
47 }
48
49 if ($this->args["help"]) {
50 $this->header();
51 $this->help($prog);
52 exit;
53 }
54
55 foreach ($this->args->validate() as $error) {
56 $errs[] = $error;
57 }
58
59 if ($errs) {
60 if (!$this->args["quiet"]) {
61 $this->header();
62 }
63 foreach ($errs as $err) {
64 $this->error("%s\n", $err);
65 }
66 if (!$this->args["quiet"]) {
67 $this->help($prog);
68 }
69 exit(1);
70 }
71
72 $this->installPackage();
73 }
74
75 /**
76 * @inheritdoc
77 * @see \pharext\Command::getArgs()
78 */
79 public function getArgs() {
80 return $this->args;
81 }
82
83 /**
84 * @inheritdoc
85 * @see \pharext\Command::info()
86 */
87 public function info($fmt) {
88 if (!$this->args->quiet) {
89 vprintf($fmt, array_slice(func_get_args(), 1));
90 }
91 }
92
93 /**
94 * @inheritdoc
95 * @see \pharext\Command::error()
96 */
97 public function error($fmt) {
98 if (!$this->args->quiet) {
99 vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1));
100 }
101 }
102
103 /**
104 * Extract the phar to a temporary directory
105 */
106 private function extract() {
107 if (!$file = Phar::running(false)) {
108 $this->error("Did your run the ext.phar?\n");
109 exit(3);
110 }
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);
115 chdir($temp);
116 }
117
118 /**
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
123 */
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);
129 } else {
130 $retval = -1;
131 }
132 } elseif ($this->args->verbose) {
133 passthru($command ." 2>&1", $retval);
134 } else {
135 exec($command ." 2>&1", $output, $retval);
136 }
137 if ($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));
141 }
142 exit(2);
143 }
144 $this->info("OK\n");
145 }
146
147 /**
148 * Construct a command from prefix common-name and suffix
149 * @param type $suffix
150 * @return string
151 */
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;
156 }
157 return $cmd;
158 }
159
160 /**
161 * Prepares, configures, builds and installs the extension
162 */
163 private function installPackage() {
164 $this->extract();
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);
170 }
171 }