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');
22 $phar = new Phar($pharFile);
24 // start buffering. Mandatory to modify stub to add shebang
25 $phar->startBuffering();
27 // Create the default stub from main.php entrypoint
28 $defaultStub = $phar->createDefaultStub('run');
30 // Add the rest of the apps files
31 $phar->addFile(__DIR__ . '/run', 'run');
32 $phar->buildFromDirectory(__DIR__, '/vendor(.*)/');
34 // Customize the stub to add the shebang
35 $stub = "#!/usr/bin/env php \n" . $defaultStub;
38 $phar->setStub($stub);
40 $phar->stopBuffering();
42 // plus - compressing it into gzip
43 $phar->compressFiles(Phar::GZ);
45 # Make the file executable
46 chmod(__DIR__ . "/{$pharFile}", 0770);
48 echo "$pharFile successfully created" . PHP_EOL;
49 } catch (Exception $e) {
50 echo $e->getMessage();