8 * The extension packaging command executed by bin/pharext
10 class Packager
implements Command
13 * Command line arguments
14 * @var pharext\CliArgs
19 * Extension source directory
20 * @var pharext\SourceDir
27 public function __construct() {
28 $this->args
= new CliArgs([
29 ["h", "help", "Display this help",
30 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
31 ["v", "verbose", "More output",
32 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
33 ["q", "quiet", "Less output",
34 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
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 ["n", "name", "Extension name",
45 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
46 ["r", "release", "Extension release version",
47 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
48 ["z", "gzip", "Create additional PHAR compressed with gzip",
49 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
50 ["Z", "bzip", "Create additional PHAR compressed with bzip",
51 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
57 * @see \pharext\Command::run()
59 public function run($argc, array $argv) {
60 $prog = array_shift($argv);
61 foreach ($this->args
->parse(--$argc, $argv) as $error) {
62 $this->error("%s\n", $error);
65 if ($this->args
["help"]) {
66 $this->args
->help($prog);
70 if ($this->args
["source"]) {
71 if ($this->args
["pecl"]) {
72 $this->source
= new PeclSourceDir($this, $this->args
["source"]);
73 } elseif ($this->args
["git"]) {
74 $this->source
= new GitSourceDir($this, $this->args
["source"]);
76 $this->source
= new FilteredSourceDir($this, $this->args
["source"]);
80 foreach ($this->args
->validate() as $error) {
81 $this->error("%s\n", $error);
85 if (!$this->args
["quiet"]) {
86 $this->args
->help($prog);
91 $this->createPackage();
96 * @see \pharext\Command::getArgs()
98 public function getArgs() {
104 * @see \pharext\Command::info()
106 public function info($fmt) {
107 if (!$this->args
->quiet
) {
108 vprintf($fmt, array_slice(func_get_args(), 1));
114 * @see \pharext\Command::error()
116 public function error($fmt) {
117 if (!$this->args
->quiet
) {
118 vfprintf(STDERR
, "ERROR: $fmt", array_slice(func_get_args(), 1));
123 * Traverses all pharext source files to bundle
126 private function bundle() {
127 foreach (scandir(__DIR__
) as $entry) {
128 if (fnmatch("*.php", $entry)) {
129 yield
"pharext/$entry" => __DIR__
."/$entry";
135 * Creates the extension phar
137 private function createPackage() {
139 $pkgtemp = sys_get_temp_dir() ."/{$pkguniq}.phar";
140 $pkgdesc = "{$this->args->name}-{$this->args->release}";
142 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args
->verbose ?
"\n" : " ");
144 $package = new Phar($pkgtemp, 0, "ext.phar");
145 $package->startBuffering();
146 $package->buildFromIterator($this->source
, $this->source
->getBaseDir());
147 $package->buildFromIterator($this->bundle());
148 $package->addFile(__DIR__
."/../pharext_installer.php", "pharext_installer.php");
149 $package->setDefaultStub("pharext_installer.php");
150 $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub());
151 $package->stopBuffering();
153 chmod($pkgtemp, 0770);
154 if ($this->args
->verbose
) {
155 $this->info("Created executable phar %s\n", $pkgtemp);
159 if ($this->args
->gzip
) {
160 $this->info("Compressing with gzip ... ");
161 $package->compress(Phar
::GZ
);
164 if ($this->args
->bzip
) {
165 $this->info("Compressing with bzip ... ");
166 $package->compress(Phar
::BZ2
);
171 } catch (\Exception
$e) {
172 $this->error("%s\n", $e->getMessage());
176 foreach (glob($pkgtemp."*") as $pkgtemp) {
177 $pkgfile = str_replace($pkguniq, "{$pkgdesc}.ext", $pkgtemp);
178 $pkgname = $this->args
->dest
."/". basename($pkgfile);
179 $this->info("Finalizing %s ... ", $pkgname);
180 if (!rename($pkgtemp, $pkgname)) {
181 $this->error("%s\n", error_get_last()["message"]);