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 /* source needs to be evaluated before CliArgs validation,
99 * so e.g. name and version can be overriden and CliArgs
100 * does not complain about missing arguments
102 if ($this->args
["source"]) {
103 $source = $this->localize($this->args
["source"]);
104 if ($this->args
["pecl"]) {
105 $this->source
= new SourceDir\
Pecl($this, $source);
106 } elseif ($this->args
["git"]) {
107 $this->source
= new SourceDir\
Git($this, $source);
109 $this->source
= new SourceDir\
Pharext($this, $source);
112 } catch (\Exception
$e) {
113 $errs[] = $e->getMessage();
116 foreach ($this->args
->validate() as $error) {
121 if (!$this->args
["quiet"]) {
122 if (!headers_sent()) {
123 /* only display header, if we didn't generate any output yet */
127 foreach ($errs as $err) {
128 $this->error("%s\n", $err);
131 if (!$this->args
["quiet"]) {
137 $this->createPackage();
141 * Dump program signature
142 * @param string $prog
143 * @return int exit code
145 function signature($prog) {
147 $sig = (new Phar(Phar
::running(false)))->getSignature();
148 printf("%s signature of %s\n%s", $sig["hash_type"], $prog,
149 chunk_split($sig["hash"], 64, "\n"));
151 } catch (\Exception
$e) {
152 $this->error("%s\n", $e->getMessage());
158 * Download remote source
159 * @param string $source
160 * @return string local source
162 private function download($source) {
163 if ($this->args
["git"]) {
164 $this->info("Cloning %s ... ", $source);
165 $local = new Tempdir("gitclone");
166 $cmd = new ExecCmd("git", $this->args
->verbose
);
167 $cmd->run(["clone", $source, $local]);
168 if (!$this->args
->verbose
) {
172 $this->info("Fetching remote source %s ... ", $source);
173 if (!$remote = fopen($source, "r")) {
177 $local = new Tempfile("remote");
178 if (!stream_copy_to_stream($remote, $local->getStream())) {
182 $local->closeStream();
186 $this->cleanup
[] = $local;
187 return $local->getPathname();
191 * Extract local archive
192 * @param stirng $source
193 * @return string extracted directory
195 private function extract($source) {
196 $dest = new Tempdir("local");
197 if ($this->args
->verbose
) {
198 $this->info("Extracting to %s ... ", $dest);
200 $archive = new PharData($source);
201 $archive->extractTo($dest);
203 $this->cleanup
[] = $dest;
208 * Localize a possibly remote source
209 * @param string $source
210 * @return string local source directory
212 private function localize($source) {
213 if (!stream_is_local($source)) {
214 $source = $this->download($source);
216 if (!is_dir($source)) {
217 $source = $this->extract($source);
218 if ($this->args
["pecl"]) {
219 $this->info("Sanitizing PECL dir ... ");
220 $dirs = glob("$source/*", GLOB_ONLYDIR
);
221 $files = array_diff(glob("$source/*"), $dirs);
222 $source = current($dirs);
223 foreach ($files as $file) {
224 rename($file, "$source/" . basename($file));
233 * Traverses all pharext source files to bundle
236 private function bundle() {
237 $rdi = new \
RecursiveDirectoryIterator(__DIR__
);
238 $rii = new \
RecursiveIteratorIterator($rdi);
239 for ($rii->rewind(); $rii->valid(); $rii->next()) {
240 yield
"pharext/". $rii->getSubPathname() => $rii->key();
246 * Ask for password on the console
247 * @param string $prompt
248 * @return string password
250 private function askpass($prompt = "Password:") {
251 system("stty -echo", $retval);
253 $this->error("Could not disable echo on the terminal\n");
255 printf("%s ", $prompt);
256 $pass = fgets(STDIN
, 1024);
258 if (substr($pass, -1) == "\n") {
259 $pass = substr($pass, 0, -1);
265 * Creates the extension phar
267 private function createPackage() {
269 $pkgtemp = $this->tempname($pkguniq, "phar");
270 $pkgdesc = "{$this->args->name}-{$this->args->release}";
272 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args
->verbose ?
"\n" : " ");
274 $package = new Phar($pkgtemp);
276 if ($this->args
->sign
) {
277 $this->info("\nUsing private key to sign phar ... \n");
278 $privkey = new Openssl\
PrivateKey(realpath($this->args
->sign
), $this->askpass());
279 $privkey->sign($package);
282 $package->startBuffering();
283 $package->buildFromIterator($this->source
, $this->source
->getBaseDir());
284 $package->buildFromIterator($this->bundle(__DIR__
));
285 $package->addFile(__DIR__
."/../pharext_installer.php", "pharext_installer.php");
286 $package->setDefaultStub("pharext_installer.php");
287 $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub());
288 $package->stopBuffering();
290 if (!chmod($pkgtemp, 0777)) {
292 } elseif ($this->args
->verbose
) {
293 $this->info("Created executable phar %s\n", $pkgtemp);
297 if ($this->args
->gzip
) {
298 $this->info("Compressing with gzip ... ");
300 $package->compress(Phar
::GZ
)
301 ->setDefaultStub("pharext_installer.php");
303 } catch (\Exception
$e) {
304 $this->error("%s\n", $e->getMessage());
307 if ($this->args
->bzip
) {
308 $this->info("Compressing with bzip ... ");
310 $package->compress(Phar
::BZ2
)
311 ->setDefaultStub("pharext_installer.php");
313 } catch (\Exception
$e) {
314 $this->error("%s\n", $e->getMessage());
319 } catch (\Exception
$e) {
320 $this->error("%s\n", $e->getMessage());
324 foreach (glob($pkgtemp."*") as $pkgtemp) {
325 $pkgfile = str_replace($pkguniq, "{$pkgdesc}.ext", $pkgtemp);
326 $pkgname = $this->args
->dest
."/". basename($pkgfile);
327 $this->info("Finalizing %s ... ", $pkgname);
328 if (!rename($pkgtemp, $pkgname)) {
333 if ($this->args
->sign
&& isset($privkey)) {
334 $keyname = $this->args
->dest
."/". basename($pkgfile) . ".pubkey";
335 $this->info("Public Key %s ... ", $keyname);
337 $privkey->exportPublicKey($keyname);
339 } catch (\Exception
$e) {
340 $this->error("%s", $e->getMessage());