Phar::webPhar
(No version information available, might be only in CVS)
Phar::webPhar — mapPhar for web-based phars. front controller for web applications
Описание
Phar::mapPhar() for web-based phars. This method parses $_SERVER['REQUEST_URI'] and routes a request from a web browser to an internal file within the phar archive. In essence, it simulates a web server, routing requests to the correct file, echoing the correct headers and parsing PHP files as needed. This powerful method is part of what makes it easy to convert an existing PHP application into a phar archive. Combined with Phar::mungServer() and Phar::interceptFileFuncs(), any web application can be used unmodified from a phar archive.
Phar::webPhar() should only be called from the stub of a phar archive (see here for more information on what a stub is).
Список параметров
- alias
-
The alias that can be used in phar:// URLs to refer to this archive, rather than its full path.
- index
-
The location within the phar of the directory index, defaults to index.php.
- f404
-
The location of the script to run when a file is not found. This script should output the proper HTTP 404 headers.
- mimetypes
-
An array mapping additional file extensions to MIME type. By default, these extensions are mapped to these mime types:
$mimes = array(
'phps' => 2, // pass to highlight_file()
'c' => 'text/plain',
'cc' => 'text/plain',
'cpp' => 'text/plain',
'c++' => 'text/plain',
'dtd' => 'text/plain',
'h' => 'text/plain',
'log' => 'text/plain',
'rng' => 'text/plain',
'txt' => 'text/plain',
'xsd' => 'text/plain',
'php' => 1, // parse as PHP
'inc' => 1, // parse as PHP
'avi' => 'video/avi',
'bmp' => 'image/bmp',
'css' => 'text/css',
'gif' => 'image/gif',
'htm' => 'text/html',
'html' => 'text/html',
'htmls' => 'text/html',
'ico' => 'image/x-ico',
'jpe' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'js' => 'application/x-javascript',
'midi' => 'audio/midi',
'mid' => 'audio/midi',
'mod' => 'audio/mod',
'mov' => 'movie/quicktime',
'mp3' => 'audio/mp3',
'mpg' => 'video/mpeg',
'mpeg' => 'video/mpeg',
'pdf' => 'application/pdf',
'png' => 'image/png',
'swf' => 'application/shockwave-flash',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'wav' => 'audio/wav',
'xbm' => 'image/xbm',
'xml' => 'text/xml',
); - rewrites
-
An array mapping URI to internal file, simulating mod_rewrite of apache. For example:
array(
'myinfo' => 'myinfo.php'
);
Возвращаемые значения
No return values
Errors/Exceptions
Throws PharException when unable to open the internal file to output, if Phar::webPhar() is used from within an extracted Phar archive, or if called from a non-stub. If an invalid array value is passed into mimetypes or to rewrites , then UnexpectedValueException is thrown.
Примеры
Пример #1 A Phar::webPhar() example
With the example below, the created phar will display Hello World if one browses to /myphar.phar/index.php or to /myphar.phar, and will display the source of index.phps if one browses to /myphar.phar/index.phps.
<?php
// creating the phar archive:
try {
$phar = new Phar('myphar.phar');
$phar['index.php'] = '<?php echo "Hello World"; ?>';
$phar['index.phps'] = '<?php echo "Hello World"; ?>';
$phar->setStub('<?php
Phar::webPhar();
__HALT_COMPILER(); ?>');
} catch (Exception $e) {
// handle error here
}
?>
- 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
Коментарии
It seems that calling Phar::webPhar() from inside a function is a bad idea. Doing so will cause global variables in included files to not be global. For instance, do NOT try this:
<?php
$phar = new Phar('test.phar.php');
$phar['test.php'] = '<?php
$FOO = "globals work";
function test() {
global $FOO;
echo "test: $FOO\n";
}
test();
?>';
$phar->setStub('<?php
function _bootstrap() {
Phar::webPhar();
}
_bootstrap();
__HALT_COMPILER(); ?>');
?>
The output will be "test:", not "test: globals work".