fix warning
[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 /**
13 * Command line arguments
14 * @var pharext\CliArgs
15 */
16 private $args;
17
18 /**
19 * Create the command
20 */
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 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,
33 "php"],
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,
38 "sudo -S %s"]
39 ]);
40 }
41
42 /**
43 * @inheritdoc
44 * @see \pharext\Command::run()
45 */
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);
50 }
51
52 if ($this->args["help"]) {
53 $this->args->help($prog);
54 exit;
55 }
56
57 foreach ($this->args->validate() as $error) {
58 $this->error("%s\n", $error);
59 }
60
61 if (isset($error)) {
62 if (!$this->args["quiet"]) {
63 $this->args->help($prog);
64 }
65 exit(1);
66 }
67
68 $this->installPackage();
69 }
70
71 /**
72 * @inheritdoc
73 * @see \pharext\Command::getArgs()
74 */
75 public function getArgs() {
76 return $this->args;
77 }
78
79 /**
80 * @inheritdoc
81 * @see \pharext\Command::info()
82 */
83 public function info($fmt) {
84 if (!$this->args->quiet) {
85 vprintf($fmt, array_slice(func_get_args(), 1));
86 }
87 }
88
89 /**
90 * @inheritdoc
91 * @see \pharext\Command::error()
92 */
93 public function error($fmt) {
94 if (!$this->args->quiet) {
95 vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1));
96 }
97 }
98
99 /**
100 * Extract the phar to a temporary directory
101 */
102 private function extract() {
103 if (!$file = Phar::running(false)) {
104 $this->error("Did your run the ext.phar?\n");
105 exit(3);
106 }
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);
111 chdir($temp);
112 }
113
114 /**
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
119 */
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);
125 } else {
126 $retval = -1;
127 }
128 } elseif ($this->args->verbose) {
129 passthru($command ." 2>&1", $retval);
130 } else {
131 exec($command ." 2>&1", $output, $retval);
132 }
133 if ($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));
137 }
138 exit(2);
139 }
140 $this->info("OK\n");
141 }
142
143 /**
144 * Construct a command from prefix common-name and suffix
145 * @param type $suffix
146 * @return string
147 */
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;
152 }
153 return $cmd;
154 }
155
156 /**
157 * Prepares, configures, builds and installs the extension
158 */
159 private function installPackage() {
160 $this->extract();
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);
166 }
167 }