correctly bail out on non-existing INI file
[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 * Cleanups
20 * @var array
21 */
22 private $cleanup = [];
23
24 /**
25 * Create the command
26 */
27 public function __construct() {
28 $this->args = new CliArgs([
29 ["h", "help", "Display help",
30 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
31 ["v", "verbose", "More output",
32 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
33 ["q", "quiet", "Less output",
34 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
35 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
36 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
37 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
38 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
39 "php"],
40 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
41 CliArgs::OPTIONAL|CliArgs::MULTI|CliArgs::REQARG],
42 ["s", "sudo", "Installation might need increased privileges",
43 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::OPTARG,
44 "sudo -S %s"],
45 ["i", "ini", "Activate in this php.ini instead of loaded default php.ini",
46 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
47 [null, "signature", "Show package signature",
48 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
49 [null, "license", "Show package license",
50 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
51 [null, "name", "Show package name",
52 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
53 [null, "date", "Show package release date",
54 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
55 [null, "release", "Show package release version",
56 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
57 [null, "version", "Show pharext version",
58 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
59 ]);
60 }
61
62 /**
63 * Perform cleaniup
64 */
65 function __destruct() {
66 foreach ($this->cleanup as $cleanup) {
67 $cleanup->run();
68 }
69 }
70
71 private function extract(Phar $phar) {
72 $temp = (new Task\Extract($phar))->run($this->verbosity());
73 $this->cleanup[] = new Task\Cleanup($temp);
74 return $temp;
75 }
76
77 private function hooks(SplObjectStorage $phars) {
78 $hook = [];
79 foreach ($phars as $phar) {
80 if (isset($phar["pharext_package.php"])) {
81 $sdir = include $phar["pharext_package.php"];
82 if ($sdir instanceof SourceDir) {
83 $this->args->compile($sdir->getArgs());
84 $hook[] = $sdir;
85 }
86 }
87 }
88 return $hook;
89 }
90
91 private function load() {
92 $list = new SplObjectStorage();
93 $phar = new Phar(Phar::running(false));
94 $temp = $this->extract($phar);
95
96 foreach ($phar as $entry) {
97 $dep_file = $entry->getBaseName();
98 if (fnmatch("*.ext.phar*", $dep_file)) {
99 $dep_phar = new Phar("$temp/$dep_file");
100 $list[$dep_phar] = $this->extract($dep_phar);
101 }
102 }
103
104 /* the actual ext.phar at last */
105 $list[$phar] = $temp;
106 return $list;
107 }
108
109 /**
110 * @inheritdoc
111 * @see \pharext\Command::run()
112 */
113 public function run($argc, array $argv) {
114 try {
115 /* load the phar(s) */
116 $list = $this->load();
117 /* installer hooks */
118 $hook = $this->hooks($list);
119 } catch (\Exception $e) {
120 $this->error("%s\n", $e->getMessage());
121 exit(self::EEXTRACT);
122 }
123
124 /* standard arg stuff */
125 $errs = [];
126 $prog = array_shift($argv);
127 foreach ($this->args->parse(--$argc, $argv) as $error) {
128 $errs[] = $error;
129 }
130
131 if ($this->args["help"]) {
132 $this->header();
133 $this->help($prog);
134 exit;
135 }
136 try {
137 foreach (["signature", "name", "date", "license", "release", "version"] as $opt) {
138 if ($this->args[$opt]) {
139 printf("%s\n", $this->metadata($opt));
140 exit;
141 }
142 }
143 } catch (\Exception $e) {
144 $this->error("%s\n", $e->getMessage());
145 exit(self::EARGS);
146 }
147
148 foreach ($this->args->validate() as $error) {
149 $errs[] = $error;
150 }
151
152 if ($errs) {
153 if (!$this->args["quiet"]) {
154 $this->header();
155 }
156 foreach ($errs as $err) {
157 $this->error("%s\n", $err);
158 }
159 if (!$this->args["quiet"]) {
160 $this->help($prog);
161 }
162 exit(self::EARGS);
163 }
164
165 try {
166 /* post process hooks */
167 foreach ($hook as $sdir) {
168 $sdir->setArgs($this->args);
169 }
170 } catch (\Exception $e) {
171 $this->error("%s\n", $e->getMessage());
172 exit(self::EARGS);
173 }
174
175 /* install packages */
176 try {
177 foreach ($list as $phar) {
178 $this->info("Installing %s ...\n", basename($phar->getPath()));
179 $this->install($list[$phar]);
180 $this->activate($list[$phar]);
181 $this->info("Successfully installed %s!\n", basename($phar->getPath()));
182 }
183 } catch (\Exception $e) {
184 $this->error("%s\n", $e->getMessage());
185 exit(self::EINSTALL);
186 }
187 }
188
189 /**
190 * Phpize + trinity
191 */
192 private function install($temp) {
193 // phpize
194 $phpize = new Task\Phpize($temp, $this->args->prefix, $this->args->{"common-name"});
195 $phpize->run($this->verbosity());
196
197 // configure
198 $configure = new Task\Configure($temp, $this->args->configure, $this->args->prefix, $this->args{"common-name"});
199 $configure->run($this->verbosity());
200
201 // make
202 $make = new Task\Make($temp);
203 $make->run($this->verbosity());
204
205 // install
206 $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
207 $install = new Task\Make($temp, ["install"], $sudo);
208 $install->run($this->verbosity());
209 }
210
211 private function activate($temp) {
212 if ($this->args->ini) {
213 $files = [$this->args->ini];
214 } else {
215 $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
216 $files[] = php_ini_loaded_file();
217 }
218
219 $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
220 $type = $this->metadata("type") ?: "extension";
221
222 $activate = new Task\Activate($temp, $files, $type, $this->args->prefix, $this->args{"common-name"}, $sudo);
223 if (!$activate->run($this->verbosity())) {
224 $this->info("Extension already activated ...\n");
225 }
226 }
227 }