Skip to content

Instantly share code, notes, and snippets.

@dseguy
Last active June 15, 2018 08:09
<?php
shell_exec('rm -rf folder');
unlink('iterator.phar');
unlink('addfile.phar');
mkdir('folder', 0755);
mkdir('folder/sub', 0755);
file_put_contents('folder/sub/a.txt', 'a');
file_put_contents('folder/sub/b.txt', 'b');
file_put_contents('folder/sub/c.txt', 'c');
file_put_contents('test', <<<'PHP'
<?php
print_r(__DIR__);
var_dump(scandir(__DIR__.'/folder/sub'));
?>
PHP
);
// create with buildFromIterator
$phar = new Phar('iterator.phar', 0, 'iterator.phar');
$phar->addFile('test', 'test');
$phar->buildFromIterator(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(__DIR__.'/folder')), __DIR__);
$stub = <<<'PHP'
<?php
Phar::mapPhar();
include 'phar://iterator.phar/test';
__HALT_COMPILER();
PHP;
$phar->setStub($stub);
print "Built ".filesize('iterator.phar')."o in iterator.phar\n";
// create with buildFromIterator
$phar = new Phar('addfile.phar', 0, 'addfile.phar');
$phar->addFile('test', 'test');
foreach( new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__.'/folder')) as $file => $c) {
if (basename($file) === '.') {continue; }
if (basename($file) === '..') {continue; }
$phar->addFile($file, str_replace(__DIR__, '', $file));
}
$stub = <<<'PHP'
<?php
Phar::mapPhar();
include 'phar://addfile.phar/test';
__HALT_COMPILER();
PHP;
$phar->setStub($stub);
print "Built ".filesize('addfile.phar')."o in addfile.phar\n";
?>
@dseguy
Copy link
Author

dseguy commented Jun 14, 2018

To run :
mkdir issuephar
cd issuephar
php test.php // (this gist).
php iterator.phar (errors)
php addfile.phar (displays folder content)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment