Last active
January 6, 2023 18:45
-
-
Save ekojs/df0e870bbef896a0416e9c2e16c5f5a2 to your computer and use it in GitHub Desktop.
Create phar file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// this compile.php file above of html folder | |
try { | |
$pharFile = 'app.phar'; | |
// clean up | |
if (file_exists($pharFile)){ | |
unlink($pharFile); | |
} | |
if (file_exists($pharFile . '.gz')){ | |
unlink($pharFile . '.gz'); | |
} | |
// create phar | |
$phar = new Phar($pharFile); | |
// start buffering. Mandatory to modify stub to add shebang | |
$phar->startBuffering(); | |
// Create the default stub from main.php entrypoint inside html | |
$defaultStub = $phar->createDefaultStub('main.php'); | |
// Add the rest of the apps files | |
$phar->buildFromDirectory(__DIR__ . '/html'); | |
// Customize the stub to add the shebang | |
$stub = "#!/usr/bin/env php \n" . $defaultStub; | |
// Add the stub | |
$phar->setStub($stub); | |
$phar->stopBuffering(); | |
// plus - compressing it into gzip | |
$phar->compressFiles(Phar::GZ); | |
# Make the file executable | |
chmod(__DIR__ . "/{$pharFile}", 0770); | |
echo "$pharFile successfully created" . PHP_EOL; | |
} catch (Exception $e){ | |
echo $e->getMessage(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$phar = new Phar('app.phar'); | |
$phar2 = $phar->convertToExecutable (Phar::TAR,Phar::NONE); // Convert to an uncompressed tar archive | |
$phar2->extractTo('out/'); // Extract all files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment