0b133e357909f937b75d8244f8d3899ac4df7d45
8 * The extension packaging command executed by bin/pharext
10 class Packager
implements Command
15 * Extension source directory
16 * @var pharext\SourceDir
23 public function __construct() {
24 $this->args
= new CliArgs([
25 ["h", "help", "Display this help",
26 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
27 ["v", "verbose", "More output",
28 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
29 ["q", "quiet", "Less output",
30 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
31 ["n", "name", "Extension name",
32 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
33 ["r", "release", "Extension release version",
34 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
35 ["s", "source", "Extension source directory",
36 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
37 ["g", "git", "Use `git ls-files` instead of the standard ignore filter",
38 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
39 ["p", "pecl", "Use PECL package.xml instead of the standard ignore filter",
40 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
41 ["d", "dest", "Destination directory",
42 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
44 ["z", "gzip", "Create additional PHAR compressed with gzip",
45 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
46 ["Z", "bzip", "Create additional PHAR compressed with bzip",
47 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
53 * @see \pharext\Command::run()
55 public function run($argc, array $argv) {
57 $prog = array_shift($argv);
58 foreach ($this->args
->parse(--$argc, $argv) as $error) {
62 if ($this->args
["help"]) {
68 if ($this->args
["source"]) {
69 if ($this->args
["pecl"]) {
70 $this->source
= new PeclSourceDir($this, $this->args
["source"]);
71 } elseif ($this->args
["git"]) {
72 $this->source
= new GitSourceDir($this, $this->args
["source"]);
73 } elseif (realpath($this->args
["source"]."/pharext_package.php")) {
74 $this->source
= new PharextSourceDir($this, $this->args
["source"]);
76 $this->source
= new FilteredSourceDir($this, $this->args
["source"]);
80 foreach ($this->args
->validate() as $error) {
85 if (!$this->args
["quiet"]) {
88 foreach ($errs as $err) {
89 $this->error("%s\n", $err);
91 if (!$this->args
["quiet"]) {
97 $this->createPackage();
101 * Traverses all pharext source files to bundle
104 private function bundle() {
105 foreach (scandir(__DIR__
) as $entry) {
106 if (fnmatch("*.php", $entry)) {
107 yield
"pharext/$entry" => __DIR__
."/$entry";
113 * Creates the extension phar
115 private function createPackage() {
117 $pkgtemp = $this->tempname($pkguniq, "phar");
118 $pkgdesc = "{$this->args->name}-{$this->args->release}";
120 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args
->verbose ?
"\n" : " ");
122 $package = new Phar($pkgtemp, 0, "ext.phar");
123 $package->startBuffering();
124 $package->buildFromIterator($this->source
, $this->source
->getBaseDir());
125 $package->buildFromIterator($this->bundle());
126 $package->addFile(__DIR__
."/../pharext_installer.php", "pharext_installer.php");
127 $package->setDefaultStub("pharext_installer.php");
128 $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub());
129 $package->stopBuffering();
131 if (!chmod($pkgtemp, 0777)) {
133 } elseif ($this->args
->verbose
) {
134 $this->info("Created executable phar %s\n", $pkgtemp);
138 if ($this->args
->gzip
) {
139 $this->info("Compressing with gzip ... ");
141 $package->compress(Phar
::GZ
);
143 } catch (\Exception
$e) {
144 $this->error("%s\n", $e->getMessage());
147 if ($this->args
->bzip
) {
148 $this->info("Compressing with bzip ... ");
150 $package->compress(Phar
::BZ2
);
152 } catch (\Exception
$e) {
153 $this->error("%s\n", $e->getMessage());
158 } catch (\Exception
$e) {
159 $this->error("%s\n", $e->getMessage());
163 foreach (glob($pkgtemp."*") as $pkgtemp) {
164 $pkgfile = str_replace($pkguniq, "{$pkgdesc}.ext", $pkgtemp);
165 $pkgname = $this->args
->dest
."/". basename($pkgfile);
166 $this->info("Finalizing %s ... ", $pkgname);
167 if (!rename($pkgtemp, $pkgname)) {