9 * The extension install command executed by the extension phar
11 class Installer
implements Command
19 private $cleanup = [];
24 public function __construct() {
25 $this->args
= new Cli\
Args([
26 ["h", "help", "Display help",
27 Cli\Args
::OPTIONAL|Cli\Args
::SINGLE|Cli\Args
::NOARG|Cli\Args
::HALT
],
28 ["v", "verbose", "More output",
29 Cli\Args
::OPTIONAL|Cli\Args
::SINGLE|Cli\Args
::NOARG
],
30 ["q", "quiet", "Less output",
31 Cli\Args
::OPTIONAL|Cli\Args
::SINGLE|Cli\Args
::NOARG
],
32 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
33 Cli\Args
::OPTIONAL|Cli\Args
::SINGLE|Cli\Args
::REQARG
],
34 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
35 Cli\Args
::OPTIONAL|Cli\Args
::SINGLE|Cli\Args
::REQARG
,
37 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
38 Cli\Args
::OPTIONAL|Cli\Args
::MULTI|Cli\Args
::REQARG
],
39 ["s", "sudo", "Installation might need increased privileges",
40 Cli\Args
::OPTIONAL|Cli\Args
::SINGLE|Cli\Args
::OPTARG
,
42 ["i", "ini", "Activate in this php.ini instead of loaded default php.ini",
43 Cli\Args
::OPTIONAL|Cli\Args
::SINGLE|Cli\Args
::REQARG
],
44 [null, "signature", "Show package signature",
45 Cli\Args
::OPTIONAL|Cli\Args
::SINGLE|Cli\Args
::NOARG|Cli\Args
::HALT
],
46 [null, "license", "Show package license",
47 Cli\Args
::OPTIONAL|Cli\Args
::SINGLE|Cli\Args
::NOARG|Cli\Args
::HALT
],
48 [null, "name", "Show package name",
49 Cli\Args
::OPTIONAL|Cli\Args
::SINGLE|Cli\Args
::NOARG|Cli\Args
::HALT
],
50 [null, "date", "Show package release date",
51 Cli\Args
::OPTIONAL|Cli\Args
::SINGLE|Cli\Args
::NOARG|Cli\Args
::HALT
],
52 [null, "release", "Show package release version",
53 Cli\Args
::OPTIONAL|Cli\Args
::SINGLE|Cli\Args
::NOARG|Cli\Args
::HALT
],
54 [null, "version", "Show pharext version",
55 Cli\Args
::OPTIONAL|Cli\Args
::SINGLE|Cli\Args
::NOARG|Cli\Args
::HALT
],
62 function __destruct() {
63 foreach ($this->cleanup
as $cleanup) {
68 private function extract($phar) {
69 $temp = (new Task\
Extract($phar))->run($this->verbosity());
70 $this->cleanup
[] = new Task\
Cleanup($temp);
74 private function hooks(SplObjectStorage
$phars) {
76 foreach ($phars as $phar) {
77 if (isset($phar["pharext_package.php"])) {
78 $sdir = include $phar["pharext_package.php"];
79 if ($sdir instanceof SourceDir
) {
80 $this->args
->compile($sdir->getArgs());
88 private function load() {
89 $list = new SplObjectStorage();
90 $phar = extension_loaded("Phar")
91 ?
new Phar(Phar
::running(false))
92 : new Archive(PHAREXT_PHAR
);
93 $temp = $this->extract($phar);
95 foreach ($phar as $entry) {
96 $dep_file = $entry->getBaseName();
97 if (fnmatch("*.ext.phar*", $dep_file)) {
98 $dep_phar = extension_loaded("Phar")
99 ?
new Phar("$temp/$dep_file")
100 : new Archive("$temp/$dep_file");
101 $list[$dep_phar] = $this->extract($dep_phar);
105 /* the actual ext.phar at last */
106 $list[$phar] = $temp;
112 * @see \pharext\Command::run()
114 public function run($argc, array $argv) {
116 /* load the phar(s) */
117 $list = $this->load();
118 /* installer hooks */
119 $hook = $this->hooks($list);
120 } catch (\Exception
$e) {
121 $this->error("%s\n", $e->getMessage());
122 exit(self
::EEXTRACT
);
125 /* standard arg stuff */
127 $prog = array_shift($argv);
128 foreach ($this->args
->parse(--$argc, $argv) as $error) {
132 if ($this->args
["help"]) {
138 foreach (["signature", "name", "date", "license", "release", "version"] as $opt) {
139 if ($this->args
[$opt]) {
140 printf("%s\n", $this->metadata($opt));
144 } catch (\Exception
$e) {
145 $this->error("%s\n", $e->getMessage());
149 foreach ($this->args
->validate() as $error) {
154 if (!$this->args
["quiet"]) {
157 foreach ($errs as $err) {
158 $this->error("%s\n", $err);
160 if (!$this->args
["quiet"]) {
167 /* post process hooks */
168 foreach ($hook as $sdir) {
169 $sdir->setArgs($this->args
);
171 } catch (\Exception
$e) {
172 $this->error("%s\n", $e->getMessage());
176 /* install packages */
178 foreach ($list as $phar) {
179 $this->info("Installing %s ...\n", basename($phar->getPath()));
180 $this->install($list[$phar]);
181 $this->activate($list[$phar]);
182 $this->info("Successfully installed %s!\n", basename($phar->getPath()));
184 } catch (\Exception
$e) {
185 $this->error("%s\n", $e->getMessage());
186 exit(self
::EINSTALL
);
193 private function install($temp) {
195 $phpize = new Task\
Phpize($temp, $this->args
->prefix
, $this->args
->{"common-name"});
196 $phpize->run($this->verbosity());
199 $configure = new Task\
Configure($temp, $this->args
->configure
, $this->args
->prefix
, $this->args
->{"common-name"});
200 $configure->run($this->verbosity());
203 $make = new Task\
Make($temp);
204 $make->run($this->verbosity());
207 $sudo = isset($this->args
->sudo
) ?
$this->args
->sudo
: null;
208 $install = new Task\
Make($temp, ["install"], $sudo);
209 $install->run($this->verbosity());
212 private function activate($temp) {
213 if ($this->args
->ini
) {
214 $files = [$this->args
->ini
];
216 $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
217 $files[] = php_ini_loaded_file();
220 $sudo = isset($this->args
->sudo
) ?
$this->args
->sudo
: null;
221 $type = $this->metadata("type") ?
: "extension";
223 $activate = new Task\
Activate($temp, $files, $type, $this->args
->prefix
, $this->args
["common-name"], $sudo);
224 if (!$activate->run($this->verbosity())) {
225 $this->info("Extension already activated ...\n");