Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get phar archive meta-data inside of executed phar?

Tags:

php

metadata

phar

I have packaged PHP application into executable Phar archive and have there inside one Class with method which should access executed Phar archive meta-data.

I could get meta-data as shown below however that seems odd that I load same Phar inside the Phar archive executed in order to get it's meta-data.

So is there right way how to get executed Phar's meta-data? Perhaps read it and defined that inside the Phar's Stub or something.

<?php
namespace MyPhar;
use \Phar;

class InsideThePhar {
    public function getPharMetaData() {
        $phar_self = new Phar(Phar::running(false));
        $metadata = $phar_self->getMetadata();
        var_dump($metadata);
        exit();
    }
}
like image 272
mkungla Avatar asked Feb 11 '16 16:02

mkungla


People also ask

How do I extract a Phar file?

To unpack the PHAR archive, you need to click on the area below or drag the * . phar file into it with the mouse. As a result of unpacking, you will receive a ZIP archive. To package the PHAR archive, you need to click on the area below or drag the * .

What do I do with a Phar file?

The most common usage for a phar archive is to distribute a complete application in a single file. For instance, the PEAR Installer that is bundled with PHP versions is distributed as a phar archive. To use a phar archive distributed in this way, the archive can be executed on the command-line or via a web server.


1 Answers

PHP keeps a cache of already loaded phar files, see http://git.php.net/?p=php-src.git;a=blob;f=ext/phar/phar.c;h=65ebce0f0856fc5a90a62d32dd0bb5c00627706f;hb=HEAD#l96

The cache is used when opening phar files, so it is not as expensive as opening a totally different phar file.


And no, as of PHP 7.2 there is no better way to get the meta data of the currently "running" phar file.

like image 178
cweiske Avatar answered Oct 19 '22 23:10

cweiske