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