Skip to content

Instantly share code, notes, and snippets.

@simonjodet
Created June 18, 2011 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonjodet/1032868 to your computer and use it in GitHub Desktop.
Save simonjodet/1032868 to your computer and use it in GitHub Desktop.
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