4379bb6ee093c78ed63bfa19776ffbf0e49707ba
[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 /**
13 * Command line arguments
14 * @var pharext\CliArgs
15 */
16 private $args;
17
18 /**
19 * Extension source directory
20 * @var pharext\SourceDir
21 */
22 private $source;
23
24 /**
25 * Create the command
26 */
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,
43 "."],
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],
52 ]);
53 }
54
55 /**
56 * @inheritdoc
57 * @see \pharext\Command::run()
58 */
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);
63 }
64
65 if ($this->args["help"]) {
66 $this->args->help($prog);
67 exit;
68 }
69
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"]);
75 } else {
76 $this->source = new FilteredSourceDir($this, $this->args["source"]);
77 }
78 }
79
80 foreach ($this->args->validate() as $error) {
81 $this->error("%s\n", $error);
82 }
83
84 if (isset($error)) {
85 if (!$this->args["quiet"]) {
86 $this->args->help($prog);
87 }
88 exit(1);
89 }
90
91 $this->createPackage();
92 }
93
94 /**
95 * @inheritdoc
96 * @see \pharext\Command::getArgs()
97 */
98 public function getArgs() {
99 return $this->args;
100 }
101
102 /**
103 * @inheritdoc
104 * @see \pharext\Command::info()
105 */
106 public function info($fmt) {
107 if (!$this->args->quiet) {
108 vprintf($fmt, array_slice(func_get_args(), 1));
109 }
110 }
111
112 /**
113 * @inheritdoc
114 * @see \pharext\Command::error()
115 */
116 public function error($fmt) {
117 if (!$this->args->quiet) {
118 vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1));
119 }
120 }
121
122 /**
123 * Traverses all pharext source files to bundle
124 * @return Generator
125 */
126 private function bundle() {
127 foreach (scandir(__DIR__) as $entry) {
128 if (fnmatch("*.php", $entry)) {
129 yield "pharext/$entry" => __DIR__."/$entry";
130 }
131 }
132 }
133
134 /**
135 * Creates the extension phar
136 */
137 private function createPackage() {
138 $pkguniq = uniqid();
139 $pkgtemp = sys_get_temp_dir() ."/{$pkguniq}.phar";
140 $pkgdesc = "{$this->args->name}-{$this->args->release}";
141
142 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args->verbose ? "\n" : " ");
143 try {
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();
152
153 chmod($pkgtemp, 0770);
154 if ($this->args->verbose) {
155 $this->info("Created executable phar %s\n", $pkgtemp);
156 } else {
157 $this->info("OK\n");
158 }
159 if ($this->args->gzip) {
160 $this->info("Compressing with gzip ... ");
161 $package->compress(Phar::GZ);
162 $this->info("OK\n");
163 }
164 if ($this->args->bzip) {
165 $this->info("Compressing with bzip ... ");
166 $package->compress(Phar::BZ2);
167 $this->info("OK\n");
168 }
169
170 unset($package);
171 } catch (\Exception $e) {
172 $this->error("%s\n", $e->getMessage());
173 exit(4);
174 }
175
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"]);
182 exit(5);
183 }
184 $this->info("OK\n");
185 }
186 }
187 }