Phar::startBuffering
(PECL phar:1.0.0-1.2.1)
Phar::startBuffering — Start buffering Phar write operations, do not modify the Phar object on disk
Описание
Although technically unnecessary, the startBuffering() method can provide a significant performance boost when creating or modifying a Phar archive with a large number of files. Ordinarily, every time a file within a Phar archive is created or modified in any way, the entire Phar archive will be recreated with the changes. In this way, the archive will be up-to-date with the activity performed on it.
However, this can be unnecessary when simply creating a new Phar archive, when it would make more sense to write the entire archive out at once. Similarly, it is often necessary to make a series of changes and to ensure that they all are possible before making any changes on disk, similar to the relational database concept of transactions. the startBuffering()/stopBuffering() pair of methods is provided for this purpose.
Phar write buffering is per-archive, buffering active for the foo.phar Phar archive does not affect changes to the bar.phar Phar archive.
Примеры
Пример #1 A Phar::startBuffering() example
<?php
// make sure it doesn't exist
@unlink('brandnewphar.phar');
try {
$p = new Phar(dirname(__FILE__) . '/brandnewphar.phar', 0, 'brandnewphar.phar');
} catch (Exception $e) {
echo 'Could not create phar:', $e;
}
echo 'The new phar has ' . $p->count() . " entries\n";
$p->startBuffering();
$p['file.txt'] = 'hi';
$p['file2.txt'] = 'there';
$p['file2.txt']->setCompressedGZ();
$p['file3.txt'] = 'babyface';
$p['file3.txt']->setMetaData(42);
$p->setStub("<?php
function __autoload($class)
{
include 'phar://myphar.phar/' . str_replace('_', '/', $class) . '.php';
}
Phar::mapPhar('myphar.phar');
include 'phar://myphar.phar/startup.php';
__HALT_COMPILER();");
$p->stopBuffering();
?>
- Add an empty directory to the phar archive
- Add a file from the filesystem to the phar archive
- Add a file from the filesystem to the phar archive
- Returns the api version
- Construct a phar archive from the files within a directory.
- Construct a phar archive from an iterator.
- Returns whether phar extension supports compression using either zlib or bzip2
- Returns whether phar extension supports writing and creating phars
- Compresses the entire Phar archive using Gzip or Bzip2 compression
- Compresses all files in the current Phar archive using Bzip2 compression
- Compresses all files in the current Phar archive using Gzip compression
- Compresses all files in the current Phar archive
- Construct a Phar archive object
- Convert a phar archive to a non-executable tar or zip file
- Convert a phar archive to another executable phar archive file format
- Copy a file internal to the phar archive to another new file within the phar
- Returns the number of entries (files) in the Phar archive
- Create a phar-file format specific stub
- Decompresses the entire Phar archive
- Decompresses all files in the current Phar archive
- Deletes the global metadata of the phar
- Delete a file within a phar archive
- Extract the contents of a phar archive to a directory
- Returns phar archive meta-data
- Return whether phar was modified
- Return MD5/SHA1/SHA256/SHA512/OpenSSL signature of a Phar archive
- Return the PHP loader or bootstrap stub of a Phar archive
- Return array of supported compression algorithms
- Return array of supported signature types
- Return version info of Phar archive
- Returns whether phar has global meta-data
- instructs phar to intercept fopen, file_get_contents, opendir, and all of the stat-related functions
- Used to determine whether Phar write operations are being buffered, or are flushing directly to disk
- Returns Phar::GZ or PHAR::BZ2 if the entire phar archive is compressed (.tar.gz/tar.bz and so on)
- Returns true if the phar archive is based on the tar/phar/zip file format depending on the parameter
- Returns whether the given filename is a valid phar filename
- Returns true if the phar archive can be modified
- Loads any phar archive with an alias
- Reads the currently executed file (a phar) and registers its manifest
- Mount an external path or file to a virtual location within the phar archive
- Defines a list of up to 4 $_SERVER variables that should be modified for execution
- determines whether a file exists in the phar
- Gets a PharFileInfo object for a specific file
- set the contents of an internal file to those of an external file
- remove a file from a phar
- Returns the full path on disk or full phar URL to the currently executing Phar archive
- Set the alias for the Phar archive
- Used to set the PHP loader or bootstrap stub of a Phar archive to the default loader
- Sets phar archive meta-data
- set the signature algorithm for a phar and apply it.
- Used to set the PHP loader or bootstrap stub of a Phar archive
- Start buffering Phar write operations, do not modify the Phar object on disk
- Stop buffering write requests to the Phar archive, and save changes to disk
- Uncompresses all files in the current Phar archive
- Completely remove a phar archive from disk and from memory
- mapPhar for web-based phars. front controller for web applications
Коментарии
To setthe ini key phar.readonly to 0, from a unix shell script, this is possible on the fly it is possible see below.
The example given on this page has some issues with quoting: (See conversion to simple quotes)
#!/usr/bin/php -d phar.readonly=0
<?php
print(ini_get('phar.readonly')); // Must return 0
// make sure it doesn't exist
@unlink('brandnewphar.phar');
try {
$p = new Phar(dirname(__FILE__) . '/brandnewphar.phar', 0, 'brandnewphar.phar');
} catch (Exception $e) {
echo 'Could not create phar:', $e;
}
echo 'The new phar has ' . $p->count() . " entries\n";
$p->startBuffering();
$p['file.txt'] = 'hi';
$p['file2.txt'] = 'there';
$p['file2.txt']->compress(Phar::GZ);
$p['file3.txt'] = 'babyface';
$p['file3.txt']->setMetadata(42);
$p->setStub('<?php
function __autoload($class)
{
include "phar://myphar.phar/" . str_replace("_", "/", $class) . ".php";
}
Phar::mapPhar("myphar.phar");
include "phar://myphar.phar/startup.php";
__HALT_COMPILER();');
$p->stopBuffering();
// Test
$m = file_get_contents("phar://brandnewphar.phar/file2.txt");
$m = explode("\n",$m);
var_dump($m);
/* Output:
* there
**/