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", "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
],
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
);
59 private function hooks(SplObjectStorage
$phars) {
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);
74 * @see \pharext\Command::run()
76 public function run($argc, array $argv) {
77 $list = new SplObjectStorage();
78 $phar = new Phar(Phar
::running(false));
79 $temp = $this->extract($phar);
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);
88 /* the actual ext.phar at last */
92 $hook = $this->hooks($list);
94 /* standard arg stuff */
96 $prog = array_shift($argv);
97 foreach ($this->args
->parse(--$argc, $argv) as $error) {
101 if ($this->args
["help"]) {
107 foreach (["signature", "name", "license", "release", "version"] as $opt) {
108 if ($this->args
[$opt]) {
109 printf("%s\n", $this->metadata($opt));
113 } catch (\Exception
$e) {
114 $this->error("%s\n", $e->getMessage());
118 foreach ($this->args
->validate() as $error) {
123 if (!$this->args
["quiet"]) {
126 foreach ($errs as $err) {
127 $this->error("%s\n", $err);
129 if (!$this->args
["quiet"]) {
135 /* post process hooks */
136 foreach ($hook as $callback) {
137 if (is_callable($callback)) {
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()));
155 private function install($temp) {
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
);
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
);
168 $this->info("Running make ...\n");
169 $make = new Task\
Make($temp);
170 $make->run($this->args
->verbose
);
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
);
178 } catch (\Exception
$e) {
179 $this->error("%s\n", $e->getMessage());
184 private function cleanup($temp) {
187 } elseif (file_exists($temp)) {
192 private function activate($temp) {
193 if ($this->args
->ini
) {
194 $files = [realpath($this->args
->ini
)];
196 $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
197 $files[] = php_ini_loaded_file();
200 $sudo = isset($this->args
->sudo
) ?
$this->args
->sudo
: null;
201 $type = $this->metadata("type") ?
: "php";
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");
209 } catch (\Exception
$e) {
210 $this->error("%s\n", $e->getMessage());