support git clones and (PECL) package archives as sources
[pharext/pharext] / src / pharext / Installer.php
1 <?php
2
3 namespace pharext;
4
5 use Phar;
6 use pharext\Cli\Args as CliArgs;
7 use pharext\Cli\Command as CliCommand;
8
9 /**
10 * The extension install command executed by the extension phar
11 */
12 class Installer implements Command
13 {
14 use CliCommand;
15
16 /**
17 * The temporary directory we should operate in
18 * @var string
19 */
20 private $tmp;
21
22 /**
23 * The directory we came from
24 * @var string
25 */
26 private $cwd;
27
28 /**
29 * Create the command
30 */
31 public function __construct() {
32 $this->args = new CliArgs([
33 ["h", "help", "Display help",
34 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
35 ["v", "verbose", "More output",
36 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
37 ["q", "quiet", "Less output",
38 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
39 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
40 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
41 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
42 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
43 "php"],
44 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
45 CliArgs::OPTIONAL|CliArgs::MULTI|CliArgs::REQARG],
46 ["s", "sudo", "Installation might need increased privileges",
47 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::OPTARG,
48 "sudo -S %s"],
49 ["i", "ini", "Activate in this php.ini instead of loaded default php.ini",
50 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
51 ]);
52 }
53
54 /**
55 * Cleanup temp directory
56 */
57 public function __destruct() {
58 $this->cleanup();
59 }
60
61 /**
62 * @inheritdoc
63 * @see \pharext\Command::run()
64 */
65 public function run($argc, array $argv) {
66 $this->cwd = getcwd();
67 $this->tmp = $this->tempname(basename(Phar::running(false)));
68
69 $phar = new Phar(Phar::running(false));
70 foreach ($phar as $entry) {
71 if (fnmatch("*.ext.phar*", $entry->getBaseName())) {
72 $temp = $this->newtemp($entry->getBaseName());
73 $phar->extractTo($temp, $entry->getFilename(), true);
74 $phars[$temp] = new Phar($temp."/".$entry->getFilename());
75 }
76 }
77 $phars[$this->tmp] = $phar;
78
79 foreach ($phars as $phar) {
80 if (isset($phar["pharext_install.php"])) {
81 $callable = include $phar["pharext_install.php"];
82 if (is_callable($callable)) {
83 $recv[] = $callable($this);
84 }
85 }
86 }
87
88 $errs = [];
89 $prog = array_shift($argv);
90 foreach ($this->args->parse(--$argc, $argv) as $error) {
91 $errs[] = $error;
92 }
93
94 if ($this->args["help"]) {
95 $this->header();
96 $this->help($prog);
97 exit;
98 }
99
100 foreach ($this->args->validate() as $error) {
101 $errs[] = $error;
102 }
103
104 if ($errs) {
105 if (!$this->args["quiet"]) {
106 $this->header();
107 }
108 foreach ($errs as $err) {
109 $this->error("%s\n", $err);
110 }
111 if (!$this->args["quiet"]) {
112 $this->help($prog);
113 }
114 exit(1);
115 }
116
117 if (isset($recv)) {
118 foreach ($recv as $r) {
119 $r($this);
120 }
121 }
122 foreach ($phars as $temp => $phar) {
123 $this->installPackage($phar, $temp);
124 }
125 }
126
127 /**
128 * Prepares, configures, builds and installs the extension
129 */
130 private function installPackage(Phar $phar, $temp) {
131 $this->info("Installing %s ... \n", basename($phar->getAlias()));
132 try {
133 $phar->extractTo($temp, null, true);
134 } catch (\Exception $e) {
135 $this->error("%s\n", $e->getMessage());
136 exit(3);
137 }
138
139 if (!chdir($temp)) {
140 $this->error(null);
141 exit(4);
142 }
143
144 // phpize
145 $this->exec("phpize", $this->php("ize"));
146
147 // configure
148 $args = ["--with-php-config=". $this->php("-config")];
149 if ($this->args->configure) {
150 $args = array_merge($args, $this->args->configure);
151 }
152 $this->exec("configure", "./configure", $args);
153
154 // make
155 if ($this->args->verbose) {
156 $this->exec("make", "make", ["-j3"]);
157 } else {
158 $this->exec("make", "make", ["-j3", "-s"]);
159 }
160
161 // install
162 if ($this->args->verbose) {
163 $this->exec("install", "make", ["install"], true);
164 } else {
165 $this->exec("install", "make", ["install", "-s"], true);
166 }
167
168 // activate
169 $this->activate();
170
171 // cleanup
172 $this->cleanup($temp);
173 }
174
175 /**
176 * Perform any cleanups
177 */
178 private function cleanup($temp = null) {
179 if (!isset($temp)) {
180 $temp = $this->tmp;
181 }
182 if (is_dir($temp)) {
183 chdir($this->cwd);
184 $this->info("Cleaning up %s ...\n", $temp);
185 $this->rm($temp);
186 }
187 }
188
189 /**
190 * Construct a command from prefix common-name and suffix
191 * @param type $suffix
192 * @return string
193 */
194 private function php($suffix) {
195 $cmd = $this->args["common-name"] . $suffix;
196 if (isset($this->args->prefix)) {
197 $cmd = $this->args->prefix . "/bin/" . $cmd;
198 }
199 return $cmd;
200 }
201
202 /**
203 * Activate extension in php.ini
204 */
205 private function activate() {
206 if ($this->args->ini) {
207 $files = [realpath($this->args->ini)];
208 } else {
209 $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
210 $files[] = php_ini_loaded_file();
211 }
212
213 $extension = basename(current(glob("modules/*.so")));
214 $pattern = preg_quote($extension);
215
216 foreach ($files as $index => $file) {
217 $temp = new Tempfile("phpini");
218 foreach (file($file) as $line) {
219 if (preg_match("/^\s*extension\s*=\s*[\"']?{$pattern}[\"']?\s*(;.*)?\$/", $line)) {
220 // already there
221 $this->info("Extension already activated\n");
222 return;
223 }
224 fwrite($temp->getStream(), $line);
225 }
226 }
227
228 // not found, add extension line to the last process file
229 if (isset($temp, $file)) {
230 fprintf($temp->getStream(), "extension=%s\n", $extension);
231 $temp->closeStream();
232
233 $path = $temp->getPathname();
234 $stat = stat($file);
235
236 $ugid = sprintf("%d:%d", $stat["uid"], $stat["gid"]);
237 $this->exec("INI owner transfer", "chown", [$ugid, $path], true);
238
239 $perm = decoct($stat["mode"] & 0777);
240 $this->exec("INI permission transfer", "chmod", [$perm, $path], true);
241
242 $this->exec("INI activation", "mv", [$path, $file], true);
243 }
244 }
245 }