6 use pharext\Cli\Args
as CliArgs
;
7 use pharext\Cli\Command
as CliCommand
;
10 * The extension packaging command executed by bin/pharext
12 class Packager
implements Command
17 * Extension source directory
18 * @var pharext\SourceDir
25 public function __construct() {
26 $this->args
= new CliArgs([
27 ["h", "help", "Display this help",
28 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
29 ["v", "verbose", "More output",
30 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
31 ["q", "quiet", "Less output",
32 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
33 ["n", "name", "Extension name",
34 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
35 ["r", "release", "Extension release version",
36 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
37 ["s", "source", "Extension source directory",
38 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
39 ["g", "git", "Use `git ls-tree` to determine file list",
40 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
41 ["p", "pecl", "Use PECL package.xml to determine file list, name and release",
42 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
43 ["d", "dest", "Destination directory",
44 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
46 ["z", "gzip", "Create additional PHAR compressed with gzip",
47 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
48 ["Z", "bzip", "Create additional PHAR compressed with bzip",
49 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
50 ["S", "sign", "Sign the PHAR with a private key",
51 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
],
52 [null, "signature", "Dump signature",
53 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
59 * @see \pharext\Command::run()
61 public function run($argc, array $argv) {
63 $prog = array_shift($argv);
64 foreach ($this->args
->parse(--$argc, $argv) as $error) {
68 if ($this->args
["help"]) {
73 if ($this->args
["signature"]) {
74 exit($this->signature($prog));
78 if ($this->args
["source"]) {
79 if ($this->args
["pecl"]) {
80 $this->source
= new SourceDir\
Pecl($this, $this->args
["source"]);
81 } elseif ($this->args
["git"]) {
82 $this->source
= new SourceDir\
Git($this, $this->args
["source"]);
84 $this->source
= new SourceDir\
Pharext($this, $this->args
["source"]);
87 } catch (\Exception
$e) {
88 $errs[] = $e->getMessage();
91 foreach ($this->args
->validate() as $error) {
96 if (!$this->args
["quiet"]) {
99 foreach ($errs as $err) {
100 $this->error("%s\n", $err);
103 if (!$this->args
["quiet"]) {
109 $this->createPackage();
112 function signature($prog) {
114 $sig = (new Phar(Phar
::running(false)))->getSignature();
115 printf("%s signature of %s\n%s", $sig["hash_type"], $prog,
116 chunk_split($sig["hash"], 64, "\n"));
118 } catch (\Exception
$e) {
119 $this->error("%s\n", $e->getMessage());
125 * Traverses all pharext source files to bundle
128 private function bundle() {
129 $rdi = new \
RecursiveDirectoryIterator(__DIR__
);
130 $rii = new \
RecursiveIteratorIterator($rdi);
131 for ($rii->rewind(); $rii->valid(); $rii->next()) {
132 yield
"pharext/". $rii->getSubPathname() => $rii->key();
137 private function askpass($prompt = "Password:") {
138 system("stty -echo", $retval);
140 $this->error("Could not disable echo on the terminal\n");
142 printf("%s ", $prompt);
143 $pass = fgets(STDIN
, 1024);
145 if (substr($pass, -1) == "\n") {
146 $pass = substr($pass, 0, -1);
152 * Creates the extension phar
154 private function createPackage() {
156 $pkgtemp = $this->tempname($pkguniq, "phar");
157 $pkgdesc = "{$this->args->name}-{$this->args->release}";
159 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args
->verbose ?
"\n" : " ");
161 $package = new Phar($pkgtemp);
163 if ($this->args
->sign
) {
164 $this->info("\nUsing private key to sign phar ... \n");
165 $privkey = new Openssl\
PrivateKey(realpath($this->args
->sign
), $this->askpass());
166 $privkey->sign($package);
169 $package->startBuffering();
170 $package->buildFromIterator($this->source
, $this->source
->getBaseDir());
171 $package->buildFromIterator($this->bundle(__DIR__
));
172 $package->addFile(__DIR__
."/../pharext_installer.php", "pharext_installer.php");
173 $package->setDefaultStub("pharext_installer.php");
174 $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub());
175 $package->stopBuffering();
177 if (!chmod($pkgtemp, 0777)) {
179 } elseif ($this->args
->verbose
) {
180 $this->info("Created executable phar %s\n", $pkgtemp);
184 if ($this->args
->gzip
) {
185 $this->info("Compressing with gzip ... ");
187 $package->compress(Phar
::GZ
)
188 ->setDefaultStub("pharext_installer.php");
190 } catch (\Exception
$e) {
191 $this->error("%s\n", $e->getMessage());
194 if ($this->args
->bzip
) {
195 $this->info("Compressing with bzip ... ");
197 $package->compress(Phar
::BZ2
)
198 ->setDefaultStub("pharext_installer.php");
200 } catch (\Exception
$e) {
201 $this->error("%s\n", $e->getMessage());
206 } catch (\Exception
$e) {
207 $this->error("%s\n", $e->getMessage());
211 foreach (glob($pkgtemp."*") as $pkgtemp) {
212 $pkgfile = str_replace($pkguniq, "{$pkgdesc}.ext", $pkgtemp);
213 $pkgname = $this->args
->dest
."/". basename($pkgfile);
214 $this->info("Finalizing %s ... ", $pkgname);
215 if (!rename($pkgtemp, $pkgname)) {
220 if ($this->args
->sign
&& isset($privkey)) {
221 $keyname = $this->args
->dest
."/". basename($pkgfile) . ".pubkey";
222 $this->info("Public Key %s ... ", $keyname);
224 $privkey->exportPublicKey($keyname);
226 } catch (\Exception
$e) {
227 $this->error("%s", $e->getMessage());