mkungla
mkungla

Reputation: 3538

How to get phar archive meta-data inside of executed 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();
    }
}

Upvotes: 7

Views: 367

Answers (1)

cweiske
cweiske

Reputation: 31078

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.

Upvotes: 1

Related Questions