Skip to content

Instantly share code, notes, and snippets.

@ekojs
Last active January 6, 2023 18:45
Show Gist options
  • Save ekojs/df0e870bbef896a0416e9c2e16c5f5a2 to your computer and use it in GitHub Desktop.
Save ekojs/df0e870bbef896a0416e9c2e16c5f5a2 to your computer and use it in GitHub Desktop.
Create phar file
<?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();
}
<?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