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
],
44 private function extract(Phar
$phar) {
45 $this->debug("Extracting %s ...\n", basename($phar->getPath()));
46 return (new Task\
Extract($phar))->run($this->args
->verbose
);
49 private function hooks(SplObjectStorage
$phars) {
51 foreach ($phars as $phar) {
52 if (isset($phar["pharext_install.php"])) {
53 $callable = include $phar["pharext_install.php"];
54 if (is_callable($callable)) {
55 $hooks[] = $callable($this);
64 * @see \pharext\Command::run()
66 public function run($argc, array $argv) {
67 $list = new SplObjectStorage();
68 $phar = new Phar(Phar
::running(false));
69 $temp = $this->extract($phar);
71 foreach ($phar as $entry) {
72 $dep_file = $entry->getBaseName();
73 if (fnmatch("*.ext.phar*", $dep_file)) {
74 $dep_phar = new Phar("$temp/$dep_file");
75 $list[$dep_phar] = $this->extract($dep_phar);
78 /* the actual ext.phar at last */
82 $hook = $this->hooks($list);
84 /* standard arg stuff */
86 $prog = array_shift($argv);
87 foreach ($this->args
->parse(--$argc, $argv) as $error) {
91 if ($this->args
["help"]) {
97 foreach ($this->args
->validate() as $error) {
102 if (!$this->args
["quiet"]) {
105 foreach ($errs as $err) {
106 $this->error("%s\n", $err);
108 if (!$this->args
["quiet"]) {
114 /* post process hooks */
115 foreach ($hook as $callback) {
116 if (is_callable($callback)) {
121 /* install packages */
122 foreach ($list as $phar) {
123 $this->info("Installing %s ...\n", basename($phar->getPath()));
124 $this->install($list[$phar]);
125 $this->activate($list[$phar]);
126 $this->cleanup($list[$phar]);
127 $this->info("Successfully installed %s!\n", basename($phar->getPath()));
134 private function install($temp) {
137 $this->info("Running phpize ...\n");
138 $phpize = new Task\
Phpize($temp, $this->args
->prefix
, $this->args
->{"common-name"});
139 $phpize->run($this->args
->verbose
);
142 $this->info("Running configure ...\n");
143 $configure = new Task\
Configure($temp, $this->args
->configure
, $this->args
->prefix
, $this->args
{"common-name"});
144 $configure->run($this->args
->verbose
);
147 $this->info("Running make ...\n");
148 $make = new Task\
Make($temp);
149 $make->run($this->args
->verbose
);
152 $this->info("Running make install ...\n");
153 $sudo = isset($this->args
->sudo
) ?
$this->args
->sudo
: null;
154 $install = new Task\
Make($temp, ["install"], $sudo);
155 $install->run($this->args
->verbose
);
157 } catch (\Exception
$e) {
158 $this->error("%s\n", $e->getMessage());
163 private function cleanup($temp) {
166 } elseif (file_exists($temp)) {
171 private function activate($temp) {
172 if ($this->args
->ini
) {
173 $files = [realpath($this->args
->ini
)];
175 $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
176 $files[] = php_ini_loaded_file();
179 $sudo = isset($this->args
->sudo
) ?
$this->args
->sudo
: null;
182 $this->info("Running INI activation ...\n");
183 $activate = new Task\
Activate($temp, $files, $sudo);
184 if (!$activate->run($this->args
->verbose
)) {
185 $this->info("Extension already activated ...\n");
187 } catch (\Exception
$e) {
188 $this->error("%s\n", $e->getMessage());
189 $this->error("%s\n", $output);