Lithiumはちゃんとショートタグを置換して出力してるよ!の確認

またもや自分の記事『まだLithiumには手を出さないほうがいいんでね?』で曖昧な記述があったので再検証した。
http://d.hatena.ne.jp/absun/20110207#1297018695


曖昧というか、ソースも出さずに言い切ってる部分があったのでそれの検証をしてみた。

Lithiumはviewでphpのショートタグ"変換してるとこは本当にあるのかありました。
それはlithium\libraries\lithium\template\view\Compiler.phpです!
viewをレンダリングする際にCompiler::templete()が呼ばれているようで、このCompilerクラスに色々書いてあります。

置換パターン正規表現が以下のメンバ変数に。

<?php
protected static $_processors = array(
//略
);
?>


これが置換処理部分。

<?php
public static function compile($string) {
//略
}
?>

で、template()メソッド中で

<?php
$compiled = static::compile(file_get_contents($file));
?>

と書いてあり、これで該当ファイルを処理してます。



本当に処理はここを通るのか?

ちっ、うっせーな。じゃあロードするファイル名と、変換結果を画面に出してみましょう。
上記のCompiler.phpの中で、変換処理を行った直後に表示とexitします。

<?php
$compiled = static::compile(file_get_contents($file));
echo $file . "\n";
var_dump($compiled);
exit;

?>


ビューファイルはHelloWorld的なコントローラでindex()をただ実行させたいのでapp/views/hello_world/index.html.php

Hello World!
<?= "unnko"; ?>

と書いておきます。


で、lithium/HelloWorld/にアクセス。
出力されたソースは以下の通りです。

D:/xampp/htdocs/lithium/app/views/hello_world/index.html.php
string(42) "Hello World!

<?php echo $h("unnko"); ?>

"


ドヤッ


$h()ってのはlithium\libraries\lithium\template\View.phpにある、ビューで呼び出せるhtmlエスケープ処理です。

<?php
$h = function($data) use (&$encoding) {
	return htmlspecialchars((string) $data, ENT_QUOTES, $encoding);
};
?>

そんな感じですね。