Oihana PHP

phar

Table of Contents

Functions

assertPhar()  : void
Ensures that the `PharData` class and `phar` extension are available in the PHP environment.
getPharBasePath()  : string
Returns the base path of an archive, formatted for the `phar://` stream wrapper.
getPharCompressionType()  : int
Returns the corresponding Phar compression constant for a given compression type.
preservePharFilePermissions()  : void
Preserves file permissions from a Phar archive to the extracted files.

Functions

assertPhar()

Ensures that the `PharData` class and `phar` extension are available in the PHP environment.

assertPhar() : void

This function is typically used as a safeguard before attempting to work with .phar, .tar, .tar.gz, or .zip files using the PharData class.

Tags
throws
RuntimeException

If the PharData class does not exist or the phar extension is not enabled.

example
use function oihana\files\phar\assertPhar;

try
{
    assertPhar();
    $phar = new \PharData('/path/to/archive.tar');
    // proceed with Phar operations...
}
catch (\RuntimeException $e)
{
    echo "Phar support is not available: " . $e->getMessage();
}
author

Marc Alcaraz (ekameleon)

since
1.0.0

getPharBasePath()

Returns the base path of an archive, formatted for the `phar://` stream wrapper.

getPharBasePath(PharData $phar) : string

This function constructs a base URI by prefixing the archive's real path with phar://. This URI can be used to access or manipulate the internal files of the archive via PHP's stream wrapper.

It is particularly useful when extracting or analyzing archive contents by building full paths to the internal files (e.g., phar:///path/to/archive.tar/dir/file.txt).

Parameters
$phar : PharData

The archive's PharData instance.

Tags
throws
RuntimeException

If the archive file does not exist or is not readable.

example
use oihana\files\phar\assertPhar;
use oihana\files\phar\getPharBaseUri;

assertPhar();

$phar = new \PharData('/absolute/path/to/archive.tar');
$baseUri = getPharBaseUri($phar);

echo $baseUri; // Outputs: phar:///absolute/path/to/archive.tar

// Access an internal file:
$content = file_get_contents($baseUri . '/docs/readme.txt');
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The base phar:// URI to access the contents of the archive (e.g., phar:///absolute/path/to/archive.tar).

getPharCompressionType()

Returns the corresponding Phar compression constant for a given compression type.

getPharCompressionType(string $compression) : int

This function maps a compression type (e.g., gzip, bzip2, or none) to the appropriate Phar compression constant (e.g., Phar::GZ, Phar::BZ2, Phar::NONE).

It is useful when setting or detecting compression in Phar archives programmatically.

Parameters
$compression : string

The compression type to resolve. Must be one of the values defined in CompressionType.

Tags
throws
UnsupportedCompressionException

If the given compression type is not supported.

example
use oihana\files\phar\getPharCompressionType;
use oihana\files\enums\CompressionType;

$compression = CompressionType::GZIP;
$pharConstant = getPharCompressionType($compression);

echo $pharConstant; // Outputs: 4096 (Phar::GZ)
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
int

The Phar compression constant (Phar::GZ, Phar::BZ2, or Phar::NONE).

preservePharFilePermissions()

Preserves file permissions from a Phar archive to the extracted files.

preservePharFilePermissions(PharData $phar, string $outputPath) : void

This function iterates over the contents of a PharData archive and applies the original file permissions (as stored in the archive) to the corresponding extracted files in the specified output directory.

This is especially useful when extracting .tar or .tar.gz archives where file modes (e.g., executable bits) should be retained.

If a file's permissions cannot be set, the function logs a warning using error_log().

Parameters
$phar : PharData

The PharData archive instance.

$outputPath : string

The absolute path to the directory where files were extracted.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0
example
use PharData;
use oihana\files\phar\assertPhar;
use oihana\files\phar\preservePharFilePermissions;

assertPhar();

$phar = new PharData('/archives/app.tar');
$phar->extractTo('/var/www/app', true);

preservePharFilePermissions($phar, '/var/www/app');

        
On this page

Search results