5 use pharext\Cli\Args
as CliArgs
;
6 use pharext\Cli\Command
as CliCommand
;
12 * The extension install command executed by the extension phar
14 class Installer
implements Command
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
,
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
,
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
],
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
);
61 private function hooks(SplObjectStorage
$phars) {
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);
76 * @see \pharext\Command::run()
78 public function run($argc, array $argv) {
80 $list = new SplObjectStorage();
81 $phar = new Phar(Phar
::running(false));
82 $temp = $this->extract($phar);
84 foreach ($phar as $entry) {
85 $dep_file = $entry->getBaseName();
86 if (fnmatch("*.ext.phar*", $dep_file)) {
87 $dep_phar = new Phar("$temp/$dep_file");
88 $list[$dep_phar] = $this->extract($dep_phar);
91 /* the actual ext.phar at last */
95 $hook = $this->hooks($list);
96 } catch (\Exception
$e) {
97 $this->error("%s\n", $e->getMessage());
101 /* standard arg stuff */
103 $prog = array_shift($argv);
104 foreach ($this->args
->parse(--$argc, $argv) as $error) {
108 if ($this->args
["help"]) {
114 foreach (["signature", "name", "date", "license", "release", "version"] as $opt) {
115 if ($this->args
[$opt]) {
116 printf("%s\n", $this->metadata($opt));
120 } catch (\Exception
$e) {
121 $this->error("%s\n", $e->getMessage());
125 foreach ($this->args
->validate() as $error) {
130 if (!$this->args
["quiet"]) {
133 foreach ($errs as $err) {
134 $this->error("%s\n", $err);
136 if (!$this->args
["quiet"]) {
143 /* post process hooks */
144 foreach ($hook as $callback) {
145 if (is_callable($callback)) {
149 } catch (\Exception
$e) {
150 $this->error("%s\n", $e->getMessage());
154 /* install packages */
156 foreach ($list as $phar) {
157 $this->info("Installing %s ...\n", basename($phar->getPath()));
158 $this->install($list[$phar]);
159 $this->activate($list[$phar]);
160 $this->cleanup($list[$phar]);
161 $this->info("Successfully installed %s!\n", basename($phar->getPath()));
163 } catch (\Exception
$e) {
164 $this->error("%s\n", $e->getMessage());
165 exit(self
::EINSTALL
);
172 private function install($temp) {
174 $this->info("Running phpize ...\n");
175 $phpize = new Task\
Phpize($temp, $this->args
->prefix
, $this->args
->{"common-name"});
176 $phpize->run($this->args
->verbose
);
179 $this->info("Running configure ...\n");
180 $configure = new Task\
Configure($temp, $this->args
->configure
, $this->args
->prefix
, $this->args
{"common-name"});
181 $configure->run($this->args
->verbose
);
184 $this->info("Running make ...\n");
185 $make = new Task\
Make($temp);
186 $make->run($this->args
->verbose
);
189 $this->info("Running make install ...\n");
190 $sudo = isset($this->args
->sudo
) ?
$this->args
->sudo
: null;
191 $install = new Task\
Make($temp, ["install"], $sudo);
192 $install->run($this->args
->verbose
);
195 private function cleanup($temp) {
198 } elseif (file_exists($temp)) {
203 private function activate($temp) {
204 if ($this->args
->ini
) {
205 $files = [realpath($this->args
->ini
)];
207 $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
208 $files[] = php_ini_loaded_file();
211 $sudo = isset($this->args
->sudo
) ?
$this->args
->sudo
: null;
212 $type = $this->metadata("type") ?
: "extension";
214 $this->info("Running INI activation ...\n");
215 $activate = new Task\
Activate($temp, $files, $type, $this->args
->prefix
, $this->args
{"common-name"}, $sudo);
216 if (!$activate->run($this->args
->verbose
)) {
217 $this->info("Extension already activated ...\n");