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 $phar->addFile(__DIR__ . '/run.php', 'run.php');
36 $phar->buildFromDirectory(__DIR__, '/src(.*)/');
37 $phar->buildFromDirectory(__DIR__, '/vendor(.*)\.php$/');
39 // Customize the stub to add the shebang
40 $stub = "#!/usr/bin/env php \n" . $defaultStub;
43 $phar->setStub($stub);
45 $phar->stopBuffering();
47 // plus - compressing it into gzip
48 $phar->compressFiles(Phar::GZ);
50 # Make the file executable
51 chmod(__DIR__ . "/{$pharFile}", 0775);
53 echo "$pharFile successfully created" . PHP_EOL;
54 } catch (Exception $e) {
55 echo "ERROR: " . $e->getMessage() . "\n";