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
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
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
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.