PHP8 Phar::mount

2024-02-21 14:39 更新

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL phar >= 2.0.0)

Phar::mount — 将外部路径或文件挂载到 phar 存档中的虚拟位置

说明

final public static Phar::mount(string $pharPath, string $externalPath): void

与 unix 文件系统的概念非常相似,即将外部设备挂载到 目录树,Phar::mount() 允许引用外部文件 和目录,就好像它们在存档中一样。这允许强大的 抽象,例如引用外部配置文件,就好像它们是 在存档中。

参数 

pharPath

phar 存档中要用作挂载路径位置的内部路径。 这必须是 phar 存档中的相对路径,并且不能已经存在。

externalPath

要在 phar 存档中挂载的外部文件或目录的路径或 URL

返回值 

没有回报。失败时引发 PharException。

错误/异常 

如果挂载路径出现任何问题,则引发 PharException。

示例 

示例 #1 A Phar::mount() example

以下示例演示如何访问外部配置文件,就好像它是 PHAR 存档中的路径。

首先,phar 存档中的代码:

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

接下来是用于挂载配置文件的外部代码:

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

另一种方法是将挂载代码放在 phar 存档的存根中。 下面是设置默认值的示例 配置文件(如果未指定用户配置):

<?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();
?>

...以及外部加载此 PHAR 存档的代码:

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


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号