PHP Doku:: Mount an external path or file to a virtual location within the phar archive - phar.mount.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzErweiterungen zur Datenkompression und ArchivierungPharThe Phar classPhar::mount

Ein Service von Reinhard Neidl - Webprogrammierung.

The Phar class

<<Phar::mapPhar

Phar::mungServer>>

Phar::mount

(PHP >= 5.3.0, PECL phar >= 2.0.0)

Phar::mountMount an external path or file to a virtual location within the phar archive

Beschreibung

void Phar::mount ( string $pharpath , string $externalpath )

Much like the unix file system concept of mounting external devices to paths within the directory tree, Phar::mount() allows referring to external files and directories as if they were inside of an archive. This allows powerful abstraction such as referring to external configuration files as if they were inside the archive.

Parameter-Liste

pharpath

The internal path within the phar archive to use as the mounted path location. If executed within a phar archive, this may be a relative path, otherwise this must be a full phar URL.

externalpath

A path or URL to an external file or directory to mount within the phar archive

Rückgabewerte

No return. PharException is thrown on failure.

Fehler/Exceptions

Throws PharException if any problems occur mounting the path.

Beispiele

Beispiel #1 A Phar::mount() example

The following example shows accessing an external configuration file as if it were a path within a phar archive.

First, the code inside of a phar archive:

<?php
$configuration 
simplexml_load_string(file_get_contents(
    
Phar::running(false) . '/config.xml'));
?>

Next the external code used to mount the configuration file:

<?php
// first set up the association between the abstract config.xml
// and the actual one on disk
Phar::mount('phar:///path/to/archive.phar/config.xml''/home/example/config.xml');
// now run the application
include '/path/to/archive.phar';
?>

Another method is to put the mounting code inside the stub of the phar archive. Here is an example of setting up a default configuration file if no user configuration is specified:

<?php
// first set up the association between the abstract config.xml
// and the actual one on disk
if (defined('EXTERNAL_CONFIG')) {
    
Phar::mount('config.xml'EXTERNAL_CONFIG);
    if (
file_exists(__DIR__ '/extra_config.xml')) {
        
Phar::mount('extra.xml'__DIR__ '/extra_config.xml');
    }
} else {
    
Phar::mount('config.xml''phar://' __FILE__ '/default_config.xml');
    
Phar::mount('extra.xml''phar://' __FILE__ '/default_extra.xml');
}
// now run the application
include 'phar://' __FILE__ '/index.php';
__HALT_COMPILER();
?>

...and the code externally to load this phar archive:

<?php
define
('EXTERNAL_CONFIG''/home/example/config.xml');
// now run the application
include '/path/to/archive.phar';
?>


Ein BenutzerBeitrag:
- Beiträge aktualisieren...
espendiller at gmx dot de
9.06.2010 12:20
i tested phar to generate a one file drupal installation.
as every cms, drupal put all uploaded files in a seperate folder (i.e. /sites).

i think the mount options here expected a file, i didnt get it to work on mounting a folder. is there any possibility to do that?

to generate a phar out of drupal7 with sqlite database

build.php
$phar = new Phar('drupal7.phar');
$phar->buildFromDirectory(dirname(__FILE__) . '/drupal-7.0-alpha5');
$phar->setStub("<?php
Phar
::interceptFileFuncs();
Phar::mount('sites/default/settings.php', __DIR__ . '/sites/default/settings.php');
Phar::mount('database.db', __DIR__ . '/database.db');
Phar::webPhar();
__HALT_COMPILER();
?>"
);

to call the phar iam using a .htacces file:
RewriteCond %{REQUEST_URI} !^/drupal.phar/
RewriteCond %{REQUEST_URI} !build.php
RewriteRule ^(.*)$ /drupal.phar/$1



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)