db9013faa4a8f72876a5d2274c411ee3129f18be
[pharext/pharext] / build / create-phar.php
1 <?php
2
3 /**
4 * Creates bin/pharext, invoked through the Makefile
5 */
6
7 $pkgname = __DIR__."/../bin/pharext";
8 $tmpname = __DIR__."/pharext.phar";
9
10 if (file_exists($tmpname)) {
11 if (!unlink($tmpname)) {
12 fprintf(STDERR, "%s\n", error_get_last()["message"]);
13 exit(3);
14 }
15 }
16
17 $package = new \Phar($tmpname, 0, "pharext.phar");
18
19 if (getenv("SIGN")) {
20 shell_exec("stty -echo");
21 printf("Password: ");
22 $password = fgets(STDIN, 1024);
23 printf("\n");
24 shell_exec("stty echo");
25 if (substr($password, -1) == "\n") {
26 $password = substr($password, 0, -1);
27 }
28
29 $pkey = openssl_pkey_get_private("file://".__DIR__."/pharext.key", $password);
30 if (!is_resource($pkey)) {
31 $this->error("Could not load private key %s/pharext.key", __DIR__);
32 exit(3);
33 }
34 if (!openssl_pkey_export($pkey, $key)) {
35 $this->error(null);
36 exit(3);
37 }
38
39 $package->setSignatureAlgorithm(Phar::OPENSSL, $key);
40 }
41
42 $package->buildFromDirectory(dirname(__DIR__)."/src", "/^.*\.php$/");
43 $package->setDefaultStub("pharext_packager.php");
44 $package->setStub("#!/usr/bin/php -dphar.readonly=0\n".$package->getStub());
45 unset($package);
46
47 if (!rename($tmpname, $pkgname)) {
48 fprintf(STDERR, "%s\n", error_get_last()["message"]);
49 exit(4);
50 }