support for zend_extension
[pharext/pharext] / src / pharext / Installer.php
1 <?php
2
3 namespace pharext;
4
5 use pharext\Cli\Args as CliArgs;
6 use pharext\Cli\Command as CliCommand;
7
8 use Phar;
9 use SplObjectStorage;
10
11 /**
12 * The extension install command executed by the extension phar
13 */
14 class Installer implements Command
15 {
16 use CliCommand;
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|CliArgs::HALT],
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 ["i", "ini", "Activate in this php.ini instead of loaded default php.ini",
40 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
41 [null, "signature", "Dump package signature",
42 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
43 [null, "license", "Show package license",
44 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
45 [null, "name", "Show package name",
46 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
47 [null, "release", "Show package release version",
48 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
49 [null, "version", "Show pharext version",
50 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
51 ]);
52 }
53
54 private function extract(Phar $phar) {
55 $this->debug("Extracting %s ...\n", basename($phar->getPath()));
56 return (new Task\Extract($phar))->run($this->args->verbose);
57 }
58
59 private function hooks(SplObjectStorage $phars) {
60 $hooks = [];
61 foreach ($phars as $phar) {
62 if (isset($phar["pharext_install.php"])) {
63 $callable = include $phar["pharext_install.php"];
64 if (is_callable($callable)) {
65 $hooks[] = $callable($this);
66 }
67 }
68 }
69 return $hooks;
70 }
71
72 /**
73 * @inheritdoc
74 * @see \pharext\Command::run()
75 */
76 public function run($argc, array $argv) {
77 $list = new SplObjectStorage();
78 $phar = new Phar(Phar::running(false));
79 $temp = $this->extract($phar);
80
81 foreach ($phar as $entry) {
82 $dep_file = $entry->getBaseName();
83 if (fnmatch("*.ext.phar*", $dep_file)) {
84 $dep_phar = new Phar("$temp/$dep_file");
85 $list[$dep_phar] = $this->extract($dep_phar);
86 }
87 }
88 /* the actual ext.phar at last */
89 $list[$phar] = $temp;
90
91 /* installer hooks */
92 $hook = $this->hooks($list);
93
94 /* standard arg stuff */
95 $errs = [];
96 $prog = array_shift($argv);
97 foreach ($this->args->parse(--$argc, $argv) as $error) {
98 $errs[] = $error;
99 }
100
101 if ($this->args["help"]) {
102 $this->header();
103 $this->help($prog);
104 exit;
105 }
106 try {
107 foreach (["signature", "name", "license", "release", "version"] as $opt) {
108 if ($this->args[$opt]) {
109 printf("%s\n", $this->metadata($opt));
110 exit;
111 }
112 }
113 } catch (\Exception $e) {
114 $this->error("%s\n", $e->getMessage());
115 exit(2);
116 }
117
118 foreach ($this->args->validate() as $error) {
119 $errs[] = $error;
120 }
121
122 if ($errs) {
123 if (!$this->args["quiet"]) {
124 $this->header();
125 }
126 foreach ($errs as $err) {
127 $this->error("%s\n", $err);
128 }
129 if (!$this->args["quiet"]) {
130 $this->help($prog);
131 }
132 exit(1);
133 }
134
135 /* post process hooks */
136 foreach ($hook as $callback) {
137 if (is_callable($callback)) {
138 $callback($this);
139 }
140 }
141
142 /* install packages */
143 foreach ($list as $phar) {
144 $this->info("Installing %s ...\n", basename($phar->getPath()));
145 $this->install($list[$phar]);
146 $this->activate($list[$phar]);
147 $this->cleanup($list[$phar]);
148 $this->info("Successfully installed %s!\n", basename($phar->getPath()));
149 }
150 }
151
152 /**
153 * Phpize + trinity
154 */
155 private function install($temp) {
156 try {
157 // phpize
158 $this->info("Running phpize ...\n");
159 $phpize = new Task\Phpize($temp, $this->args->prefix, $this->args->{"common-name"});
160 $phpize->run($this->args->verbose);
161
162 // configure
163 $this->info("Running configure ...\n");
164 $configure = new Task\Configure($temp, $this->args->configure, $this->args->prefix, $this->args{"common-name"});
165 $configure->run($this->args->verbose);
166
167 // make
168 $this->info("Running make ...\n");
169 $make = new Task\Make($temp);
170 $make->run($this->args->verbose);
171
172 // install
173 $this->info("Running make install ...\n");
174 $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
175 $install = new Task\Make($temp, ["install"], $sudo);
176 $install->run($this->args->verbose);
177
178 } catch (\Exception $e) {
179 $this->error("%s\n", $e->getMessage());
180 exit(2);
181 }
182 }
183
184 private function cleanup($temp) {
185 if (is_dir($temp)) {
186 $this->rm($temp);
187 } elseif (file_exists($temp)) {
188 unlink($temp);
189 }
190 }
191
192 private function activate($temp) {
193 if ($this->args->ini) {
194 $files = [realpath($this->args->ini)];
195 } else {
196 $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
197 $files[] = php_ini_loaded_file();
198 }
199
200 $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
201 $type = $this->metadata("type") ?: "php";
202
203 try {
204 $this->info("Running INI activation ...\n");
205 $activate = new Task\Activate($temp, $files, $type, $this->args->prefix, $this->args{"common-name"}, $sudo);
206 if (!$activate->run($this->args->verbose)) {
207 $this->info("Extension already activated ...\n");
208 }
209 } catch (\Exception $e) {
210 $this->error("%s\n", $e->getMessage());
211 exit(3);
212 }
213 }
214 }