PECL::inclued
プロファイル系の拡張モジュールが出てきてました。まだα版です。「includeのtypo?」と思いましたが、そうでもないみたいです;-)
ということで、早速インストールして試してみました。インストールはpeclコマンド一発で。。。あ。preferred_stateはalphaに設定済みです。
Allows you trace through and dump the hierarchy of file inclusions and class inheritance at runtime
$ sudo pecl install inclued
php.iniの設定は以下の通りで、inclued.enabledをOnにしないとプロファイルデータが出力されないようです。
extension=inclued.so inclued.enabled = On inclued.dumpdir = /tmp/
この状態で任意のスクリプトを実行すると、inclued.dumpdirで指定したディレクトリに「inclued.xxxxx.x」のようなファイルが出力されます。ファイルの内容は、以下のような配列をシリアライズしたものです。
array(4) {
["request"]=>
array(3) {
["_COOKIE"]=>
array(0) {
}
["SCRIPT_FILENAME"]=>
string(11) "sample2.php"
["REQUEST_TIME"]=>
int(1204625558)
}
["includes"]=>
array(15) {
[0]=>
array(6) {
["operation"]=>
string(12) "require_once"
["op_type"]=>
int(16)
["filename"]=>
string(28) "Services/MixiAPI/Factory.php"
["opened_path"]=>
string(53) "/usr/local/lib/php5/pear/Services/MixiAPI/Factory.php"
["fromfile"]=>
string(51) "/home/shimooka/public_html/pecl/inclued/sample2.php"
["fromline"]=>
int(3)
}
[1]=>
array(6) {
:
}
}
["inheritance"]=>
array(3) {
[0]=>
array(5) {
["operation"]=>
string(13) "declare_class"
["filename"]=>
string(57) "/usr/local/lib/php5/pear/Services/MixiAPI/AbstractAPI.php"
["line"]=>
int(39)
["name"]=>
string(28) "services_mixiapi_abstractapi"
["mangled"]=>
string(96) "services_mixiapi_abstractapi/usr/local/lib/php5/pear/Services/MixiAPI/AbstractAPI.php0xb7f44d4a"
}
[1]=>
array(5) {
:
}
}
["classes"]=>
array(15) {
[0]=>
array(4) {
["name"]=>
string(24) "Services_MixiAPI_Factory"
["mangled_name"]=>
string(24) "services_mixiapi_factory"
["filename"]=>
string(53) "/usr/local/lib/php5/pear/Services/MixiAPI/Factory.php"
["line"]=>
int(38)
}
[1]=>
array(4) {
:
}
}これを見る限り、取得できるデータは
- リクエストの情報
- includeされたファイル
- クラスの継承関係
- 定義されているクラス
のようです。また、追加されるinclued_get_data関数を使うと、シリアライズされていないデータが取得できます。
ついでに、得られたデータからincludeされたファイルを図に起こすスクリプトを書いてみました。
<?php require_once 'Image/GraphViz.php'; $graph = new Image_GraphViz(); function replacePath($path) { $replaces = array('/home/shimooka/public_html/pecl/inclued/' => '', '/usr/local/lib/php5/pear' => '$PEAR'); $ret = $path; foreach ($replaces as $from => $to) { $ret = str_replace($from, $to, $ret); } return $ret; } $dump = unserialize(file_get_contents($argv[1])); foreach ($dump['includes'] as $data) { $fromfile = replacePath($data['fromfile']); $opened_path = replacePath($data['opened_path']); $graph->addNode($fromfile); $graph->addNode($opened_path); $graph->addEdge( array($fromfile => $opened_path), array('label' => $data['operation']) ); } $graph->image();
シリアライズデータのファイル名を引数として、このスクリプトファイルを実行すると、GraphVizでdotファイルを出力します。実際にServices_MixiAPIを使うサンプルを実行して作成されたincluedファイルを使って、図にしてみました。svg形式ですので、Firefoxなどsvg対応のviewerでどうぞ。
今後こういったツールが出てくるんでしょうかね。。。