1 <?php declare(strict_types=1);
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.
10 $pharFile = 'app.phar';
13 if (file_exists($pharFile)) {
17 if (file_exists($pharFile . '.gz')) {
18 unlink($pharFile . '.gz');
21 if (is_dir(__DIR__ . '/vendor/phpunit')) {
22 throw new Exception("You should only compile when dev dependencies are NOT installed");
26 $phar = new Phar($pharFile);
28 // start buffering. Mandatory to modify stub to add shebang
29 $phar->startBuffering();
31 // Create the default stub from main.php entrypoint
32 $defaultStub = $phar->createDefaultStub('run.php');
34 // Add the rest of the apps files
35 $escapedDir = preg_quote(__DIR__, '/');
36 $phar->addFile(__DIR__ . '/run.php', 'run.php');
37 $phar->buildFromDirectory(__DIR__, "/{$escapedDir}\\/src(.*)\.php$/");
38 $phar->buildFromDirectory(__DIR__, "/{$escapedDir}\\/vendor(.*)\.php$/");
40 // Customize the stub to add the shebang
41 $stub = "#!/usr/bin/env php \n" . $defaultStub;
44 $phar->setStub($stub);
46 $phar->stopBuffering();
48 // plus - compressing it into gzip
49 $phar->compressFiles(Phar::GZ);
51 # Make the file executable
52 chmod(__DIR__ . "/{$pharFile}", 0775);
53 if (filesize(__DIR__ . "/{$pharFile}") > 500000) {
54 throw new Exception("Phar size unusually large. Check extra files are not included by mistake.");
57 echo "$pharFile successfully created" . PHP_EOL;
58 } catch (Exception $e) {
59 echo "ERROR: " . $e->getMessage() . "\n";