TAGS :Viewed: 5 - Published at: a few seconds ago

[ How to create a Phar archive without index ]

I'm trying to make a Phar archive with one of my lib. The lib is just a bunch of classes organized into folders and subfolders. No index.php at all here , just a static Config class to call to initiate the autoloader.

Anyway , i built a archive like this :

$phar = new Phar(__DIR__ . '/lis.phar',0,'lib.phar');
$phar->buildFromDirectory(__DIR__ . '/class','/\.php$');
$phar->stopBuffering();

After that i'm trying to use the phar like this :

require('lib.phar');
Config::register(); // Config is in the phar

But i get the following error :

Warning: include(phar://D:\wamp\www_test\phar\lib.phar/index.php) [function.include]: failed to open stream: phar error: "index.php" is not a file in phar "D:/wamp/www/_test/phar/lib.phar" in D:\wamp\www_test\phar\lib.phar on line 9

How can i make a phar archive without any index.php file inside it ? In fact i just need the archive to be a container for my files , no need to auto execute anything.

Thanks

Answer 1


First of all, i think you have to startBuffering() before stopBuffering(). And I might think that buildFromDirectory does this internally for you. You don't need to do stopBuffering() for "sealing" the archive. Its ready "on the fly".

So second: You can watch the defaultStub (which is used in your code implicity) like this:

$phar->setDefaultStub();
var_dump($phar->getStub());

Its a littly bit cryptic, but you will figure it out. It does check for phar stream wrapper support (in 5.3) and if not it extracts the contents to temp file and then executes the Phar::START constant File - which is by default "index.php". And of course it does Phar::interceptFileFuncs() and sets the include path, which makes the phar working "magic". But your question sounds like you only need an archive for your libs. So you're better off with using the "PharData" class. Haven't tried it yet, but the documentation says so.