7 use pharext\Cli\Args
as CliArgs
;
8 use pharext\Cli\Command
as CliCommand
;
11 * The extension packaging command executed by bin/pharext
13 class Packager
implements Command
18 * Extension source directory
19 * @var pharext\SourceDir
27 private $cleanup = [];
32 public function __construct() {
33 $this->args
= new CliArgs([
34 ["h", "help", "Display this help",
35 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
36 ["v", "verbose", "More output",
37 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
38 ["q", "quiet", "Less output",
39 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
40 ["n", "name", "Extension name",
41 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
42 ["r", "release", "Extension release version",
43 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
44 ["s", "source", "Extension source directory",
45 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
46 ["g", "git", "Use `git ls-tree` to determine file list",
47 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
48 ["p", "pecl", "Use PECL package.xml to determine file list, name and release",
49 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
50 ["d", "dest", "Destination directory",
51 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
53 ["z", "gzip", "Create additional PHAR compressed with gzip",
54 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
55 ["Z", "bzip", "Create additional PHAR compressed with bzip",
56 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
57 ["S", "sign", "Sign the PHAR with a private key",
58 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
],
59 [null, "signature", "Dump signature",
60 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
67 function __destruct() {
68 foreach ($this->cleanup
as $cleanup) {
69 if (is_dir($cleanup)) {
79 * @see \pharext\Command::run()
81 public function run($argc, array $argv) {
83 $prog = array_shift($argv);
84 foreach ($this->args
->parse(--$argc, $argv) as $error) {
88 if ($this->args
["help"]) {
93 if ($this->args
["signature"]) {
94 exit($this->signature($prog));
98 if ($this->args
["source"]) {
99 $source = $this->localize($this->args
["source"]);
100 if ($this->args
["pecl"]) {
101 $this->source
= new SourceDir\
Pecl($this, $source);
102 } elseif ($this->args
["git"]) {
103 $this->source
= new SourceDir\
Git($this, $source);
105 $this->source
= new SourceDir\
Pharext($this, $source);
108 } catch (\Exception
$e) {
109 $errs[] = $e->getMessage();
112 foreach ($this->args
->validate() as $error) {
117 if (!$this->args
["quiet"]) {
120 foreach ($errs as $err) {
121 $this->error("%s\n", $err);
124 if (!$this->args
["quiet"]) {
130 $this->createPackage();
134 * Dump program signature
135 * @param string $prog
136 * @return int exit code
138 function signature($prog) {
140 $sig = (new Phar(Phar
::running(false)))->getSignature();
141 printf("%s signature of %s\n%s", $sig["hash_type"], $prog,
142 chunk_split($sig["hash"], 64, "\n"));
144 } catch (\Exception
$e) {
145 $this->error("%s\n", $e->getMessage());
151 * Download remote source
152 * @param string $source
153 * @return string local source
155 private function download($source) {
156 if ($this->args
["git"]) {
157 $local = $this->newtemp("gitclone");
158 $this->exec("git clone", "git", ["clone", $source, $local]);
161 $this->info("Fetching remote source %s ... ", $source);
162 if (!$remote = fopen($source, "r")) {
166 $local = new Tempfile("remote");
167 if (!stream_copy_to_stream($remote, $local->getStream())) {
171 $local->closeStream();
172 $source = $local->getPathname();
176 $this->cleanup
[] = $local;
181 * Extract local archive
182 * @param stirng $source
183 * @return string extracted directory
185 private function extract($source) {
186 $dest = $this->newtemp("local");
187 $this->info("Extracting to %s ... ", $dest);
188 $archive = new PharData($source);
189 $archive->extractTo($dest);
191 $this->cleanup
[] = $dest;
196 * Localize a possibly remote source
197 * @param string $source
198 * @return string local source directory
200 private function localize($source) {
201 if (!stream_is_local($source)) {
202 $source = $this->download($source);
204 if (!is_dir($source)) {
205 $source = $this->extract($source);
206 if ($this->args
["pecl"]) {
207 $this->info("Sanitizing PECL dir ... ");
208 $dirs = glob("$source/*", GLOB_ONLYDIR
);
209 $files = array_diff(glob("$source/*"), $dirs);
210 $source = current($dirs);
211 foreach ($files as $file) {
212 rename($file, "$source/" . basename($file));
221 * Traverses all pharext source files to bundle
224 private function bundle() {
225 $rdi = new \
RecursiveDirectoryIterator(__DIR__
);
226 $rii = new \
RecursiveIteratorIterator($rdi);
227 for ($rii->rewind(); $rii->valid(); $rii->next()) {
228 yield
"pharext/". $rii->getSubPathname() => $rii->key();
234 * Ask for password on the console
235 * @param string $prompt
236 * @return string password
238 private function askpass($prompt = "Password:") {
239 system("stty -echo", $retval);
241 $this->error("Could not disable echo on the terminal\n");
243 printf("%s ", $prompt);
244 $pass = fgets(STDIN
, 1024);
246 if (substr($pass, -1) == "\n") {
247 $pass = substr($pass, 0, -1);
253 * Creates the extension phar
255 private function createPackage() {
257 $pkgtemp = $this->tempname($pkguniq, "phar");
258 $pkgdesc = "{$this->args->name}-{$this->args->release}";
260 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args
->verbose ?
"\n" : " ");
262 $package = new Phar($pkgtemp);
264 if ($this->args
->sign
) {
265 $this->info("\nUsing private key to sign phar ... \n");
266 $privkey = new Openssl\
PrivateKey(realpath($this->args
->sign
), $this->askpass());
267 $privkey->sign($package);
270 $package->startBuffering();
271 $package->buildFromIterator($this->source
, $this->source
->getBaseDir());
272 $package->buildFromIterator($this->bundle(__DIR__
));
273 $package->addFile(__DIR__
."/../pharext_installer.php", "pharext_installer.php");
274 $package->setDefaultStub("pharext_installer.php");
275 $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub());
276 $package->stopBuffering();
278 if (!chmod($pkgtemp, 0777)) {
280 } elseif ($this->args
->verbose
) {
281 $this->info("Created executable phar %s\n", $pkgtemp);
285 if ($this->args
->gzip
) {
286 $this->info("Compressing with gzip ... ");
288 $package->compress(Phar
::GZ
)
289 ->setDefaultStub("pharext_installer.php");
291 } catch (\Exception
$e) {
292 $this->error("%s\n", $e->getMessage());
295 if ($this->args
->bzip
) {
296 $this->info("Compressing with bzip ... ");
298 $package->compress(Phar
::BZ2
)
299 ->setDefaultStub("pharext_installer.php");
301 } catch (\Exception
$e) {
302 $this->error("%s\n", $e->getMessage());
307 } catch (\Exception
$e) {
308 $this->error("%s\n", $e->getMessage());
312 foreach (glob($pkgtemp."*") as $pkgtemp) {
313 $pkgfile = str_replace($pkguniq, "{$pkgdesc}.ext", $pkgtemp);
314 $pkgname = $this->args
->dest
."/". basename($pkgfile);
315 $this->info("Finalizing %s ... ", $pkgname);
316 if (!rename($pkgtemp, $pkgname)) {
321 if ($this->args
->sign
&& isset($privkey)) {
322 $keyname = $this->args
->dest
."/". basename($pkgfile) . ".pubkey";
323 $this->info("Public Key %s ... ", $keyname);
325 $privkey->exportPublicKey($keyname);
327 } catch (\Exception
$e) {
328 $this->error("%s", $e->getMessage());