1
0
mirror of https://github.com/php/doc-en.git synced 2026-03-23 23:32:18 +01:00
Files
archived-doc-en/reference/phar/Phar/mount.xml
2022-12-13 09:21:59 +01:00

160 lines
4.6 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="phar.mount" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>Phar::mount</refname>
<refpurpose>Mount an external path or file to a virtual location within the phar archive</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis role="Phar">
<modifier>final</modifier> <modifier>public</modifier> <modifier>static</modifier> <type>void</type><methodname>Phar::mount</methodname>
<methodparam><type>string</type><parameter>pharPath</parameter></methodparam>
<methodparam><type>string</type><parameter>externalPath</parameter></methodparam>
</methodsynopsis>
<para>
Much like the unix file system concept of mounting external devices to paths within the
directory tree, <function>Phar::mount</function> 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.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>pharPath</parameter></term>
<listitem>
<para>
The internal path within the phar archive to use as the mounted path location.
This must be a relative path within the phar archive, and must not already exist.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>externalPath</parameter></term>
<listitem>
<para>
A path or URL to an external file or directory to mount within the phar archive
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
No return. <classname>PharException</classname> is thrown on failure.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
Throws <classname>PharException</classname> if any problems occur mounting the path.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>A <function>Phar::mount</function> example</title>
<para>
The following example shows accessing an external configuration file as if it were
a path within a phar archive.
</para>
<para>
First, the code inside of a phar archive:
</para>
<programlisting role="php">
<![CDATA[
<?php
$configuration = simplexml_load_string(file_get_contents(
Phar::running(false) . '/config.xml'));
?>
]]>
</programlisting>
<para>
Next the external code used to mount the configuration file:
</para>
<programlisting role="php">
<![CDATA[
<?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';
?>
]]>
</programlisting>
<para>
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:
</para>
<programlisting role="php">
<![CDATA[
<?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();
?>
]]>
</programlisting>
<para>
...and the code externally to load this phar archive:
</para>
<programlisting role="php">
<![CDATA[
<?php
define('EXTERNAL_CONFIG', '/home/example/config.xml');
// now run the application
include '/path/to/archive.phar';
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->