6 use pharext\Cli\Args
as CliArgs
;
7 use pharext\Cli\Command
as CliCommand
;
10 * The extension install command executed by the extension phar
12 class Installer
implements Command
17 * The temporary directory we should operate in
23 * The directory we came from
31 public function __construct() {
32 $this->args
= new CliArgs([
33 ["h", "help", "Display help",
34 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
35 ["v", "verbose", "More output",
36 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
37 ["q", "quiet", "Less output",
38 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
39 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
40 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
],
41 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
42 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
44 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
45 CliArgs
::OPTIONAL|CliArgs
::MULTI|CliArgs
::REQARG
],
46 ["s", "sudo", "Installation might need increased privileges",
47 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::OPTARG
,
49 ["i", "ini", "Activate in this php.ini instead of loaded default php.ini",
50 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
],
55 * Cleanup temp directory
57 public function __destruct() {
63 * @see \pharext\Command::run()
65 public function run($argc, array $argv) {
66 $this->cwd
= getcwd();
67 $this->tmp
= $this->tempname(basename(Phar
::running(false)));
69 $phar = new Phar(Phar
::running(false));
70 foreach ($phar as $entry) {
71 if (fnmatch("*.ext.phar*", $entry->getBaseName())) {
72 $temp = new Tempdir($entry->getBaseName());
73 $phar->extractTo($temp, $entry->getFilename(), true);
74 $phars[$temp] = new Phar($temp."/".$entry->getFilename());
77 $phars[$this->tmp
] = $phar;
79 foreach ($phars as $phar) {
80 if (isset($phar["pharext_install.php"])) {
81 $callable = include $phar["pharext_install.php"];
82 if (is_callable($callable)) {
83 $recv[] = $callable($this);
89 $prog = array_shift($argv);
90 foreach ($this->args
->parse(--$argc, $argv) as $error) {
94 if ($this->args
["help"]) {
100 foreach ($this->args
->validate() as $error) {
105 if (!$this->args
["quiet"]) {
108 foreach ($errs as $err) {
109 $this->error("%s\n", $err);
111 if (!$this->args
["quiet"]) {
118 foreach ($recv as $r) {
122 foreach ($phars as $temp => $phar) {
123 $this->installPackage($phar, $temp);
128 * Prepares, configures, builds and installs the extension
130 private function installPackage(Phar
$phar, $temp) {
131 $this->info("Installing %s ... \n", basename($phar->getAlias()));
133 $phar->extractTo($temp, null, true);
134 } catch (\Exception
$e) {
135 $this->error("%s\n", $e->getMessage());
146 $this->cleanup($temp);
152 private function build() {
155 $this->info("Runnin phpize ... ");
156 $cmd = new ExecCmd($this->php("ize"), $this->args
->verbose
);
161 $this->info("Running configure ... ");
162 $args = ["--with-php-config=". $this->php("-config")];
163 if ($this->args
->configure
) {
164 $args = array_merge($args, $this->args
->configure
);
166 $cmd = new ExecCmd("./configure", $this->args
->verbose
);
171 $this->info("Running make ... ");
172 $cmd = new ExecCmd("make", $this->args
->verbose
);
173 if ($this->args
->verbose
) {
176 $cmd->run(["-j3", "-s"]);
181 $this->info("Running make install ... ");
182 if (isset($this->args
->sudo
)) {
183 $cmd->setSu($this->args
->sudo
);
185 if ($this->args
->verbose
) {
186 $cmd->run(["install"]);
188 $cmd->run(["install", "-s"]);
192 } catch (\Exception
$e) {
193 $this->error("%s\n", $e->getMessage());
194 $this->error("%s\n", $cmd->getOutput());
199 * Perform any cleanups
201 private function cleanup($temp = null) {
207 $this->info("Cleaning up %s ...\n", $temp);
213 * Construct a command from prefix common-name and suffix
214 * @param type $suffix
217 private function php($suffix) {
218 $cmd = $this->args
["common-name"] . $suffix;
219 if (isset($this->args
->prefix
)) {
220 $cmd = $this->args
->prefix
. "/bin/" . $cmd;
226 * Activate extension in php.ini
228 private function activate() {
229 if ($this->args
->ini
) {
230 $files = [realpath($this->args
->ini
)];
232 $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
233 $files[] = php_ini_loaded_file();
236 $extension = basename(current(glob("modules/*.so")));
237 $pattern = preg_quote($extension);
239 foreach ($files as $index => $file) {
240 $temp = new Tempfile("phpini");
241 foreach (file($file) as $line) {
242 if (preg_match("/^\s*extension\s*=\s*[\"']?{$pattern}[\"']?\s*(;.*)?\$/", $line)) {
244 $this->info("Extension already activated\n");
247 fwrite($temp->getStream(), $line);
251 // not found, add extension line to the last process file
252 if (isset($temp, $file)) {
253 fprintf($temp->getStream(), "extension=%s\n", $extension);
254 $temp->closeStream();
256 $path = $temp->getPathname();
260 $this->info("Running INI owner transfer ... ");
261 $ugid = sprintf("%d:%d", $stat["uid"], $stat["gid"]);
262 $cmd = new ExecCmd("chown", $this->args
->verbose
);
263 if (isset($this->args
->sudo
)) {
264 $cmd->setSu($this->args
->sudo
);
266 $cmd->run([$ugid, $path]);
269 $this->info("Running INI permission transfer ... ");
270 $perm = decoct($stat["mode"] & 0777);
271 $cmd = new ExecCmd("chmod", $this->args
->verbose
);
272 if (isset($this->args
->sudo
)) {
273 $cmd->setSu($this->args
->sudo
);
275 $cmd->run([$perm, $path]);
278 $this->info("Running INI activation ... ");
279 $cmd = new ExecCmd("mv", $this->args
->verbose
);
280 if (isset($this->args
->sudo
)) {
281 $cmd->setSu($this->args
->sudo
);
283 $cmd->run([$path, $file]);
285 } catch (\Exception
$e) {
286 $this->error("%s\n", $e->getMessage());
287 $this->error("%s\n", $cmd->getOutput());