It appears that the $rewrites parameter is a callback not an array. The implementation seems to be more in tune with this note http://www.mail-archive.com/internals@lists.php.net/msg33627.html , although, I was unable to figure out when or if a parameter gets passed to that function. I implemented it as follows:
<?php
$phar_name = 'mysite.phar';
function phar_rewrites()
{
$r = $_SERVER['REQUEST_URI'];
if (file_exists("phar://$phar_name$r")) return $r;
else return 'index.php';
}
$phar = new Phar();
/*
* do stuff to set up phar
*/
$phar->webPhar($phar_name, 'index.php', '404.php', $mimes, 'phar_rewrites');
?>
The above implementation will also stop the phar from sending out a 301 redirect to the index file when you rewrite (in apache) all requests to the phar.
Phar::webPhar
(PHP >= 5.3.0, PECL phar >= 2.0.0)
Phar::webPhar — ウェブベースの phar で使用するフロントコントローラ
説明
$alias
[, string $index = "index.php"
[, string $f404
[, array $mimetypes
[, array $rewrites
]]]]] )Phar::mapPhar() は、ウェブベースの phar で使用します。 このメソッドは、 $_SERVER['REQUEST_URI'] をパースして ウェブサーバーからのリクエストを phar アーカイブ内のファイルに転送します。 要は、このメソッド自体がウェブサーバーをシミュレートするということです。 正しいファイルにリクエストを転送し、正しいヘッダを出力し、 必要に応じて PHP ファイルをパースします。 この強力なメソッドがあることで、 既存の PHP アプリケーションを phar アーカイブ化するのが容易になります。 Phar::mungServer() や Phar::interceptFileFuncs() と組み合わせて使用すると、任意のウェブアプリケーションをそのまま phar アーカイブ化することができます。
Phar::webPhar() をコールするのは、 phar アーカイブのスタブ内からのみとしましょう (スタブって何? という方は ここ をごらんください)。
パラメータ
-
alias -
phar:// URL でこのアーカイブを指す際に、 フルパスの代わりに使用するエイリアス。
-
index -
phar の中でディレクトリインデックスとなるファイルの場所。
-
f404 -
ファイルが見つからないときに実行するスクリプトの場所。 このスクリプトは 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',
);
?> -
rewrites -
URI と内部のファイルを関連付けた配列。apache の mod_rewrite をシミュレートしたものです。 たとえば、次のような配列を定義すると、
http://<host>/myphar.phar/myinfo がコールされた際に phar:///path/to/myphar.phar/myinfo.php に転送します。その際に GET/POST の内容はそのまま維持します。 これは mod_rewrite とまったく同じ挙動になるわけではありません。たとえば http://<host>/myphar.phar/myinfo/another にはマッチしません。<?php
array(
'myinfo' => 'myinfo.php'
);
?>
返り値
値を返しません。
エラー / 例外
出力したい内部ファイルのオープンに失敗した場合、
あるいはスタブ以外からコールした場合には
PharException をスローします。
無効な配列が
mimetypes あるいは rewrites
に渡された場合は
UnexpectedValueException をスローします。
例
例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 に横取りさせる
