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
22 private $cleanup = [];
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
,
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
,
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
],
65 function __destruct() {
66 foreach ($this->cleanup
as $cleanup) {
71 private function extract(Phar
$phar) {
72 $temp = (new Task\
Extract($phar))->run($this->verbosity());
73 $this->cleanup
[] = new Task\
Cleanup($temp);
77 private function hooks(SplObjectStorage
$phars) {
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());
91 private function load() {
92 $list = new SplObjectStorage();
93 $phar = new Phar(Phar
::running(false));
94 $temp = $this->extract($phar);
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);
104 /* the actual ext.phar at last */
105 $list[$phar] = $temp;
111 * @see \pharext\Command::run()
113 public function run($argc, array $argv) {
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
);
124 /* standard arg stuff */
126 $prog = array_shift($argv);
127 foreach ($this->args
->parse(--$argc, $argv) as $error) {
131 if ($this->args
["help"]) {
137 foreach (["signature", "name", "date", "license", "release", "version"] as $opt) {
138 if ($this->args
[$opt]) {
139 printf("%s\n", $this->metadata($opt));
143 } catch (\Exception
$e) {
144 $this->error("%s\n", $e->getMessage());
148 foreach ($this->args
->validate() as $error) {
153 if (!$this->args
["quiet"]) {
156 foreach ($errs as $err) {
157 $this->error("%s\n", $err);
159 if (!$this->args
["quiet"]) {
166 /* post process hooks */
167 foreach ($hook as $sdir) {
168 $sdir->setArgs($this->args
);
170 } catch (\Exception
$e) {
171 $this->error("%s\n", $e->getMessage());
175 /* install packages */
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()));
183 } catch (\Exception
$e) {
184 $this->error("%s\n", $e->getMessage());
185 exit(self
::EINSTALL
);
192 private function install($temp) {
194 $phpize = new Task\
Phpize($temp, $this->args
->prefix
, $this->args
->{"common-name"});
195 $phpize->run($this->verbosity());
198 $configure = new Task\
Configure($temp, $this->args
->configure
, $this->args
->prefix
, $this->args
{"common-name"});
199 $configure->run($this->verbosity());
202 $make = new Task\
Make($temp);
203 $make->run($this->verbosity());
206 $sudo = isset($this->args
->sudo
) ?
$this->args
->sudo
: null;
207 $install = new Task\
Make($temp, ["install"], $sudo);
208 $install->run($this->verbosity());
211 private function activate($temp) {
212 if ($this->args
->ini
) {
213 $files = [realpath($this->args
->ini
)];
215 $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
216 $files[] = php_ini_loaded_file();
219 $sudo = isset($this->args
->sudo
) ?
$this->args
->sudo
: null;
220 $type = $this->metadata("type") ?
: "extension";
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");