CakeFest 2024: The Official CakePHP Conference

Phar::webPhar

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

Phar::webPharブラウザからのリクエストを、phar アーカイブ内部のファイルに転送する

説明

final public static Phar::webPhar(
    ?string $alias = null,
    ?string $index = null,
    ?string $fileNotFoundScript = null,
    array $mimeTypes = [],
    ?callable $rewrite = null
): void

Phar::webPhar() は、Phar::mapPhar() を、 ウェブベースの phar で使うものです。 このメソッドは、 $_SERVER['REQUEST_URI'] をパースして ウェブブラウザからのリクエストを phar アーカイブ内のファイルに転送します。 このメソッド自体がウェブサーバーをシミュレートします。 正しいファイルにリクエストを転送し、正しいヘッダを出力し、 必要に応じて PHP ファイルをパースします。 Phar::mungServer()Phar::interceptFileFuncs() と組み合わせて使用すると、任意のウェブアプリケーションをそのまま phar アーカイブ化することができます。

Phar::webPhar() をコールするのは、 phar アーカイブのスタブ内からのみとしましょう (スタブって何? という方は ここ をごらんください)。

パラメータ

alias

phar:// URL でこのアーカイブを指す際に、 フルパスの代わりに使用するエイリアス。

index

phar の中でディレクトリインデックスとなるファイルの場所。

fileNotFoundScript

ファイルが見つからないときに実行するスクリプトの場所。 このスクリプトは HTTP 404 ヘッダを返さなければなりません。

mimeTypes

ファイルの拡張子と MIME タイプを関連付けた配列。 デフォルトのマッピングで十分な場合は、空の配列を渡します。 デフォルトで、これらの関連が定義されています。

<?php
$mimes
= array(
'phps' => Phar::PHPS, // 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' => Phar::PHP, // PHP としてパースします
'inc' => Phar::PHP, // 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',
);
?>

rewrite

書き換え関数は、唯一のパラメータとして文字列を受け取り、string あるいは false を返さないといけません。

fast-cgi あるいは cgi を使っている場合、この関数に渡されるパラメータは $_SERVER['PATH_INFO'] の値になります。 それ以外の場合、この関数に渡されるパラメータは $_SERVER['REQUEST_URI'] の値になります。

文字列を返した場合は、内部的なファイルパスとして扱います。false を返した場合は、 webPhar() が HTTP 403 を送信します。

戻り値

値を返しません。

エラー / 例外

出力したい内部ファイルのオープンに失敗した場合、 あるいはスタブ以外からコールした場合には PharException をスローします。 無効な配列を mimeTypes に渡したり、 無効なコールバックを rewrite に渡したりした場合は UnexpectedValueException をスローします。

変更履歴

バージョン 説明
8.0.0 fileNotFoundScriptrewrite は、nullable になりました。

例1 Phar::webPhar() の例

この例で作成した phar は、/myphar.phar/index.php/myphar.phar をブラウズしたときには Hello World を表示し、 /myphar.phar/index.phps をブラウズしたときには index.phps のソースを表示します。

<?php
// phar アーカイブを作成します
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) {
// ここでエラー処理をします
}
?>

参考

  • Phar::mungServer() - 最大 4 つまでの $_SERVER 変数を実行時に変更させる
  • Phar::interceptFileFuncs() - fopen、file_get_contents、opendir などの stat 関連の関数をすべて phar に横取りさせる

add a note

User Contributed Notes 1 note

up
1
James
11 years ago
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".
To Top