f27c38a0a8b0f7e623b71cf514dc1f9b0b6850e5
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"]) {
69 if ($this->args
["source"]) {
70 if ($this->args
["pecl"]) {
71 $this->source
= new PeclSourceDir($this, $this->args
["source"]);
72 } elseif ($this->args
["git"]) {
73 $this->source
= new GitSourceDir($this, $this->args
["source"]);
74 } elseif (realpath($this->args
["source"]."/pharext_package.php")) {
75 $this->source
= new PharextSourceDir($this, $this->args
["source"]);
77 $this->source
= new FilteredSourceDir($this, $this->args
["source"]);
80 } catch (\Exception
$e) {
81 $errs[] = $e->getMessage();
84 foreach ($this->args
->validate() as $error) {
89 if (!$this->args
["quiet"]) {
92 foreach ($errs as $err) {
93 $this->error("%s\n", $err);
96 if (!$this->args
["quiet"]) {
102 $this->createPackage();
106 * Traverses all pharext source files to bundle
109 private function bundle() {
110 foreach (scandir(__DIR__
) as $entry) {
111 if (fnmatch("*.php", $entry)) {
112 yield
"pharext/$entry" => __DIR__
."/$entry";
118 * Creates the extension phar
120 private function createPackage() {
122 $pkgtemp = $this->tempname($pkguniq, "phar");
123 $pkgdesc = "{$this->args->name}-{$this->args->release}";
125 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args
->verbose ?
"\n" : " ");
127 $package = new Phar($pkgtemp);
128 $package->startBuffering();
129 $package->buildFromIterator($this->source
, $this->source
->getBaseDir());
130 $package->buildFromIterator($this->bundle());
131 $package->addFile(__DIR__
."/../pharext_installer.php", "pharext_installer.php");
132 $package->setDefaultStub("pharext_installer.php");
133 $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub());
134 $package->stopBuffering();
136 if (!chmod($pkgtemp, 0777)) {
138 } elseif ($this->args
->verbose
) {
139 $this->info("Created executable phar %s\n", $pkgtemp);
143 if ($this->args
->gzip
) {
144 $this->info("Compressing with gzip ... ");
146 $package->compress(Phar
::GZ
);
148 } catch (\Exception
$e) {
149 $this->error("%s\n", $e->getMessage());
152 if ($this->args
->bzip
) {
153 $this->info("Compressing with bzip ... ");
155 $package->compress(Phar
::BZ2
);
157 } catch (\Exception
$e) {
158 $this->error("%s\n", $e->getMessage());
163 } catch (\Exception
$e) {
164 $this->error("%s\n", $e->getMessage());
168 foreach (glob($pkgtemp."*") as $pkgtemp) {
169 $pkgfile = str_replace($pkguniq, "{$pkgdesc}.ext", $pkgtemp);
170 $pkgname = $this->args
->dest
."/". basename($pkgfile);
171 $this->info("Finalizing %s ... ", $pkgname);
172 if (!rename($pkgtemp, $pkgname)) {