Search code examples
phpfilenamesphar

Get filename from PharData Archive itself


My class creates PharData archives for me. The name of the archive isn't known to all functions of the class. Basically I have an outline like this:

public function doStuff(\PharData $bundle)
{
    $file = //someFile
    $bundle->addFile($file);

    $this->db->update($file is in $bundle->getName(????));
}

The only way to get access to the bundles name as far as i can tell is the getAlias() function of the $bundle variable. However this gives me the fully qualified filename and I would need to manually basename($bundle->getAlias()).

I'm really wondering if there's no way in the PharData class to get the actual archive filename itself.

I do NOT need the name of the files inside the archive, really all I need is to get access to the filename of the archive itself. I just don't know how reliant the getAlias() really is since it's name is quite confusing.


Solution

  • You could store own data with PharData::setMetadata() method. So you can store the filename in PHARs meta data and get it again at some later point with the PharData::getMetadata() method.

    Set it like this:

    $bundle->setMetaData(array('bundleFileName' => 'your_bundle.phar'));
    

    So you could then get it in your method like this:

    public function doStuff(\PharData $bundle)
    {
        $metaData = $bundle->getMetaData();
        $file     = //some File
    
        $bundle->addFile($file);
    
        $this->db->update($file is in $bundle->getMetadata['bundleFileName']);
    }
    

    But I think if you design your class properly you don't need this ugly workaround.