Instantly share code, notes, and snippets.

Embed
What would you like to do?
Build a PHAR without hidden files
<?php
//Cleaning up any existing phar
if(file_exists('package.phar'))
{
unlink('package.phar');
}
//Where are my application sources
$dir = '../app/';
//Quickly get a recursive folder list
$folder = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
$items = array();
foreach($folder as $item)
{
//Get the filename
$filename = pathinfo($item->getPathName(), PATHINFO_BASENAME);
//Filter Unix hidden files by their leading dot. Add more filters in this condition.
if(substr($filename, 0, 1) != '.')
{
//Key is the relative path inside the Phar, value is the complete path of the file
$items[substr($item->getPathName(), strlen($dir))] = $item->getPathName();
}
}
//Shows the packaged files
print_r($items);
$phar = new Phar('package.phar');
//Populate the Phar
$phar->buildFromIterator(new ArrayIterator($items));
//Add a stub
$phar->setStub(file_get_contents('stub.php'));
echo PHP_EOL;
<?php
Phar::interceptFileFuncs();
//Listing the Phar's content
$phar = new Phar(__FILE__);
foreach (new RecursiveIteratorIterator($phar) as $iteration)
{
echo $iteration->getPathName().PHP_EOL;
}
__HALT_COMPILER();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment