5401b4c269f84f7f2190c60b372b1caeb84e82b8
[pharext/pharext] / src / pharext / Packager.php
1 <?php
2
3 namespace pharext;
4
5 use Phar;
6
7 /**
8 * The extension packaging command executed by bin/pharext
9 */
10 class Packager implements Command
11 {
12 use CliCommand;
13
14 /**
15 * Extension source directory
16 * @var pharext\SourceDir
17 */
18 private $source;
19
20 /**
21 * Create the command
22 */
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,
43 "."],
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],
48 ]);
49 }
50
51 /**
52 * @inheritdoc
53 * @see \pharext\Command::run()
54 */
55 public function run($argc, array $argv) {
56 $errs = [];
57 $prog = array_shift($argv);
58 foreach ($this->args->parse(--$argc, $argv) as $error) {
59 $errs[] = $error;
60 }
61
62 if ($this->args["help"]) {
63 $this->header();
64 $this->help($prog);
65 exit;
66 }
67
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"]);
75 } else {
76 $this->source = new FilteredSourceDir($this, $this->args["source"]);
77 }
78 }
79
80 foreach ($this->args->validate() as $error) {
81 $errs[] = $error;
82 }
83
84 if ($errs) {
85 if (!$this->args["quiet"]) {
86 $this->header();
87 }
88 foreach ($errs as $err) {
89 $this->error("%s\n", $err);
90 }
91 if (!$this->args["quiet"]) {
92 $this->help($prog);
93 }
94 exit(1);
95 }
96
97 $this->createPackage();
98 }
99
100 /**
101 * @inheritdoc
102 * @see \pharext\Command::getArgs()
103 */
104 public function getArgs() {
105 return $this->args;
106 }
107
108 /**
109 * @inheritdoc
110 * @see \pharext\Command::info()
111 */
112 public function info($fmt) {
113 if (!$this->args->quiet) {
114 vprintf($fmt, array_slice(func_get_args(), 1));
115 }
116 }
117
118 /**
119 * @inheritdoc
120 * @see \pharext\Command::error()
121 */
122 public function error($fmt) {
123 if (!$this->args->quiet) {
124 vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1));
125 }
126 }
127
128 /**
129 * Traverses all pharext source files to bundle
130 * @return Generator
131 */
132 private function bundle() {
133 foreach (scandir(__DIR__) as $entry) {
134 if (fnmatch("*.php", $entry)) {
135 yield "pharext/$entry" => __DIR__."/$entry";
136 }
137 }
138 }
139
140 /**
141 * Creates the extension phar
142 */
143 private function createPackage() {
144 $pkguniq = uniqid();
145 $pkgtemp = sys_get_temp_dir() ."/{$pkguniq}.phar";
146 $pkgdesc = "{$this->args->name}-{$this->args->release}";
147
148 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args->verbose ? "\n" : " ");
149 try {
150 $package = new Phar($pkgtemp, 0, "ext.phar");
151 $package->startBuffering();
152 $package->buildFromIterator($this->source, $this->source->getBaseDir());
153 $package->buildFromIterator($this->bundle());
154 $package->addFile(__DIR__."/../pharext_installer.php", "pharext_installer.php");
155 $package->setDefaultStub("pharext_installer.php");
156 $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub());
157 $package->stopBuffering();
158
159 chmod($pkgtemp, 0770);
160 if ($this->args->verbose) {
161 $this->info("Created executable phar %s\n", $pkgtemp);
162 } else {
163 $this->info("OK\n");
164 }
165 if ($this->args->gzip) {
166 $this->info("Compressing with gzip ... ");
167 try {
168 $package->compress(Phar::GZ);
169 $this->info("OK\n");
170 } catch (\Exception $e) {
171 $this->error("%s\n", $e->getMessage());
172 }
173 }
174 if ($this->args->bzip) {
175 $this->info("Compressing with bzip ... ");
176 try {
177 $package->compress(Phar::BZ2);
178 $this->info("OK\n");
179 } catch (\Exception $e) {
180 $this->error("%s\n", $e->getMessage());
181 }
182 }
183
184 unset($package);
185 } catch (\Exception $e) {
186 $this->error("%s\n", $e->getMessage());
187 exit(4);
188 }
189
190 foreach (glob($pkgtemp."*") as $pkgtemp) {
191 $pkgfile = str_replace($pkguniq, "{$pkgdesc}.ext", $pkgtemp);
192 $pkgname = $this->args->dest ."/". basename($pkgfile);
193 $this->info("Finalizing %s ... ", $pkgname);
194 if (!rename($pkgtemp, $pkgname)) {
195 $this->error("%s\n", error_get_last()["message"]);
196 exit(5);
197 }
198 $this->info("OK\n");
199 }
200 }
201 }