refactor some commonly used code into a trait
[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 } else {
74 $this->source = new FilteredSourceDir($this, $this->args["source"]);
75 }
76 }
77
78 foreach ($this->args->validate() as $error) {
79 $errs[] = $error;
80 }
81
82 if ($errs) {
83 if (!$this->args["quiet"]) {
84 $this->header();
85 }
86 foreach ($errs as $err) {
87 $this->error("%s\n", $err);
88 }
89 if (!$this->args["quiet"]) {
90 $this->help($prog);
91 }
92 exit(1);
93 }
94
95 $this->createPackage();
96 }
97
98 /**
99 * @inheritdoc
100 * @see \pharext\Command::getArgs()
101 */
102 public function getArgs() {
103 return $this->args;
104 }
105
106 /**
107 * @inheritdoc
108 * @see \pharext\Command::info()
109 */
110 public function info($fmt) {
111 if (!$this->args->quiet) {
112 vprintf($fmt, array_slice(func_get_args(), 1));
113 }
114 }
115
116 /**
117 * @inheritdoc
118 * @see \pharext\Command::error()
119 */
120 public function error($fmt) {
121 if (!$this->args->quiet) {
122 vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1));
123 }
124 }
125
126 /**
127 * Traverses all pharext source files to bundle
128 * @return Generator
129 */
130 private function bundle() {
131 foreach (scandir(__DIR__) as $entry) {
132 if (fnmatch("*.php", $entry)) {
133 yield "pharext/$entry" => __DIR__."/$entry";
134 }
135 }
136 }
137
138 /**
139 * Creates the extension phar
140 */
141 private function createPackage() {
142 $pkguniq = uniqid();
143 $pkgtemp = sys_get_temp_dir() ."/{$pkguniq}.phar";
144 $pkgdesc = "{$this->args->name}-{$this->args->release}";
145
146 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args->verbose ? "\n" : " ");
147 try {
148 $package = new Phar($pkgtemp, 0, "ext.phar");
149 $package->startBuffering();
150 $package->buildFromIterator($this->source, $this->source->getBaseDir());
151 $package->buildFromIterator($this->bundle());
152 $package->addFile(__DIR__."/../pharext_installer.php", "pharext_installer.php");
153 $package->setDefaultStub("pharext_installer.php");
154 $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub());
155 $package->stopBuffering();
156
157 chmod($pkgtemp, 0770);
158 if ($this->args->verbose) {
159 $this->info("Created executable phar %s\n", $pkgtemp);
160 } else {
161 $this->info("OK\n");
162 }
163 if ($this->args->gzip) {
164 $this->info("Compressing with gzip ... ");
165 try {
166 $package->compress(Phar::GZ);
167 $this->info("OK\n");
168 } catch (\Exception $e) {
169 $this->error("%s\n", $e->getMessage());
170 }
171 }
172 if ($this->args->bzip) {
173 $this->info("Compressing with bzip ... ");
174 try {
175 $package->compress(Phar::BZ2);
176 $this->info("OK\n");
177 } catch (\Exception $e) {
178 $this->error("%s\n", $e->getMessage());
179 }
180 }
181
182 unset($package);
183 } catch (\Exception $e) {
184 $this->error("%s\n", $e->getMessage());
185 exit(4);
186 }
187
188 foreach (glob($pkgtemp."*") as $pkgtemp) {
189 $pkgfile = str_replace($pkguniq, "{$pkgdesc}.ext", $pkgtemp);
190 $pkgname = $this->args->dest ."/". basename($pkgfile);
191 $this->info("Finalizing %s ... ", $pkgname);
192 if (!rename($pkgtemp, $pkgname)) {
193 $this->error("%s\n", error_get_last()["message"]);
194 exit(5);
195 }
196 $this->info("OK\n");
197 }
198 }
199 }