]> BookStack Code Mirror - system-cli/blob - scripts/compile.php
f1773d1bfccba1e0c3d9bc1335437cb05245ec22
[system-cli] / scripts / compile.php
1 <?php
2
3 /**
4  * This file builds a phar archive to contain our CLI code.
5  * Attribution to https://blog.programster.org/creating-phar-files
6  * for the code in this file.
7  */
8
9 try {
10     $pharFile = 'app.phar';
11
12     // clean up
13     if (file_exists($pharFile)) {
14         unlink($pharFile);
15     }
16
17     if (file_exists($pharFile . '.gz')) {
18         unlink($pharFile . '.gz');
19     }
20
21     // create phar
22     $phar = new Phar($pharFile);
23
24     // start buffering. Mandatory to modify stub to add shebang
25     $phar->startBuffering();
26
27     // Create the default stub from main.php entrypoint
28     $defaultStub = $phar->createDefaultStub('run');
29
30     // Add the rest of the apps files
31     $phar->addFile(__DIR__ . '/run', 'run');
32     $phar->buildFromDirectory(__DIR__, '/vendor(.*)/');
33
34     // Customize the stub to add the shebang
35     $stub = "#!/usr/bin/env php \n" . $defaultStub;
36
37     // Add the stub
38     $phar->setStub($stub);
39
40     $phar->stopBuffering();
41
42     // plus - compressing it into gzip
43     $phar->compressFiles(Phar::GZ);
44
45     # Make the file executable
46     chmod(__DIR__ . "/{$pharFile}", 0770);
47
48     echo "$pharFile successfully created" . PHP_EOL;
49 } catch (Exception $e) {
50     echo $e->getMessage();
51 }