Ship your dependencies as phars inside the phar
[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 try {
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"]);
76 } else {
77 $this->source = new FilteredSourceDir($this, $this->args["source"]);
78 }
79 }
80 } catch (\Exception $e) {
81 $errs[] = $e->getMessage();
82 }
83
84 foreach ($this->args->validate() as $error) {
85 $errs[] = $error;
86 }
87
88 if ($errs) {
89 if (!$this->args["quiet"]) {
90 $this->header();
91 }
92 foreach ($errs as $err) {
93 $this->error("%s\n", $err);
94 }
95 printf("\n");
96 if (!$this->args["quiet"]) {
97 $this->help($prog);
98 }
99 exit(1);
100 }
101
102 $this->createPackage();
103 }
104
105 /**
106 * Traverses all pharext source files to bundle
107 * @return Generator
108 */
109 private function bundle() {
110 foreach (scandir(__DIR__) as $entry) {
111 if (fnmatch("*.php", $entry)) {
112 yield "pharext/$entry" => __DIR__."/$entry";
113 }
114 }
115 }
116
117 /**
118 * Creates the extension phar
119 */
120 private function createPackage() {
121 $pkguniq = uniqid();
122 $pkgtemp = $this->tempname($pkguniq, "phar");
123 $pkgdesc = "{$this->args->name}-{$this->args->release}";
124
125 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args->verbose ? "\n" : " ");
126 try {
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();
135
136 if (!chmod($pkgtemp, 0777)) {
137 $this->error(null);
138 } elseif ($this->args->verbose) {
139 $this->info("Created executable phar %s\n", $pkgtemp);
140 } else {
141 $this->info("OK\n");
142 }
143 if ($this->args->gzip) {
144 $this->info("Compressing with gzip ... ");
145 try {
146 $package->compress(Phar::GZ);
147 $this->info("OK\n");
148 } catch (\Exception $e) {
149 $this->error("%s\n", $e->getMessage());
150 }
151 }
152 if ($this->args->bzip) {
153 $this->info("Compressing with bzip ... ");
154 try {
155 $package->compress(Phar::BZ2);
156 $this->info("OK\n");
157 } catch (\Exception $e) {
158 $this->error("%s\n", $e->getMessage());
159 }
160 }
161
162 unset($package);
163 } catch (\Exception $e) {
164 $this->error("%s\n", $e->getMessage());
165 exit(4);
166 }
167
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)) {
173 $this->error(null);
174 exit(5);
175 }
176 $this->info("OK\n");
177 }
178 }
179 }