init
[pharext/pharext] / src / pharext / Installer.php
1 <?php
2
3 namespace pharext;
4
5 use Phar;
6
7 /**
8 * The extension install command executed by the extension phar
9 */
10 class Installer implements Command
11 {
12 /**
13 * Command line arguments
14 * @var pharext\CliArgs
15 */
16 private $args;
17
18 /**
19 * Create the command
20 */
21 public function __construct() {
22 $this->args = new CliArgs([
23 ["h", "help", "Display help",
24 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
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 directory",
30 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
31 "/usr"],
32 ["n", "common-name", "PHP common program name, e.g. php5",
33 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
34 "php"],
35 ["c", "configure", "Additional extension configure flags",
36 CliArgs::OPTIONAL|CliArgs::MULTI|CliArgs::REQARG],
37 ["s", "sudo", "Installation might need increased privileges",
38 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::OPTARG,
39 "sudo -S %s"]
40 ]);
41 }
42
43 /**
44 * @inheritdoc
45 * @see \pharext\Command::run()
46 */
47 public function run($argc, array $argv) {
48 $prog = array_shift($argv);
49 foreach ($this->args->parse(--$argc, $argv) as $error) {
50 $this->error("%s\n", $error);
51 }
52
53 if ($this->args["help"]) {
54 $this->args->help($prog);
55 exit;
56 }
57
58 foreach ($this->args->validate() as $error) {
59 $this->error("%s\n", $error);
60 }
61
62 if (isset($error)) {
63 if (!$this->args["quiet"]) {
64 $this->args->help($prog);
65 }
66 exit(1);
67 }
68
69 $this->installPackage();
70 }
71
72 /**
73 * @inheritdoc
74 * @see \pharext\Command::getArgs()
75 */
76 public function getArgs() {
77 return $this->args;
78 }
79
80 /**
81 * @inheritdoc
82 * @see \pharext\Command::info()
83 */
84 public function info($fmt) {
85 if (!$this->args->quiet) {
86 vprintf($fmt, array_slice(func_get_args(), 1));
87 }
88 }
89
90 /**
91 * @inheritdoc
92 * @see \pharext\Command::error()
93 */
94 public function error($fmt) {
95 if (!$this->args->quiet) {
96 vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1));
97 }
98 }
99
100 /**
101 * Extract the phar to a temporary directory
102 */
103 private function extract() {
104 if (!$file = Phar::running(false)) {
105 $this->error("Did your run the ext.phar?\n");
106 exit(3);
107 }
108 $temp = sys_get_temp_dir()."/".basename($file, ".ext.phar");
109 is_dir($temp) or mkdir($temp, 0750, true);
110 $phar = new Phar($file);
111 $phar->extractTo($temp, null, true);
112 chdir($temp);
113 }
114
115 /**
116 * Execute a system command
117 * @param string $name pretty name
118 * @param string $command full command
119 * @param bool $sudo whether the command may need escalated privileges
120 */
121 private function exec($name, $command, $sudo = false) {
122 $this->info("Running %s ...%s", $this->args->verbose ? $command : $name, $this->args->verbose ? "\n" : " ");
123 if ($sudo && isset($this->args->sudo)) {
124 if ($proc = proc_open(sprintf($this->args->sudo, $command)." 2>&1", [STDIN,STDOUT,STDERR], $pipes)) {
125 $retval = proc_close($proc);
126 } else {
127 $retval = -1;
128 }
129 } elseif ($this->args->verbose) {
130 passthru($command ." 2>&1", $retval);
131 } else {
132 exec($command ." 2>&1", $output, $retval);
133 }
134 if ($retval) {
135 $this->error("Command %s failed with (%s)\n", $command, $retval);
136 if (isset($output) && !$this->args->quiet) {
137 printf("%s\n", implode("\n", $output));
138 }
139 exit(2);
140 }
141 $this->info("OK\n");
142 }
143
144 /**
145 * Prepares, configures, builds and installs the extension
146 */
147 private function installPackage() {
148 $this->extract();
149 $this->exec("phpize", "{$this->args->prefix}/bin/{$this->args->{'common-name'}}ize");
150 $this->exec("configure", "./configure --with-php-config={$this->args->prefix}/bin/{$this->args->{'common-name'}}-config ". implode(" ", (array) $this->args->configure));
151 $this->exec("make", "make -sj3");
152 $this->exec("install", "make -s install", true);
153 }
154 }