PHP Velho Oeste 2024

inclued をアプリケーションに組み込む例

この例では、inclued を既存のアプリケーションに組み込んで結果を見る方法を説明します。

例1 PHP アプリケーション自身 (関数) 内のデータの取得

<?php
// inclued の情報を保存するファイル
$fp fopen('/tmp/wp.ser''w');
if (
$fp) {
    
$clue inclued_get_data();
    if (
$clue) {
        
fwrite($fpserialize($clue));
    }
    
fclose($fp);
}
?>

データが取得できたら、それを何らかの形式で図にしてみたいと思われることでしょう。 inclued 拡張モジュールには gengraph.php という PHP ファイルが組み込まれています。 このファイルは、» graphviz ライブラリで使える dot ファイルを作成します。 しかし、これは必須ではありません。

例2 gengraph.php の使用例

この例は、inclued のデータを示す画像 inclued.png を作成します。

# まず dot ファイルを作成します
$ php gengraph.php -i /tmp/wp.ser -o wp.dot

# そして画像を作成します
$ dot -Tpng -o inclued.png wp.dot

例3 inclued のダンプデータの一覧

inclued.dumpdir を使うと、ファイル (include 情報) がリクエストのたびに出力されます。 それらの一覧を取得して unserialize() する例です。

<?php
$path 
ini_get('inclued.dumpdir');
if (
$path && is_dir($path)) {

    echo 
"Path: $path"PHP_EOL;

    
$inclues = new GlobIterator($path DIRECTORY_SEPARATOR 'inclued*');

    if (
$inclues->count() === 0) {
        echo 
'No clues today'PHP_EOL;
        exit;
    }

    foreach (
$inclues as $inclue) {

        echo 
'Inclued file: '$inclue->getFilename(), PHP_EOL;

        
$data file_get_contents($inclue->getPathname());
        if (
$data) {
            
$inc unserialize($data);
            echo 
' -- filename: '$inc['request']['SCRIPT_FILENAME'], PHP_EOL;
            echo 
' -- number of includes: 'count($inc['includes']), PHP_EOL;
        }
        echo 
PHP_EOL;
    }
} else {
    echo 
'I am totally clueless today.'PHP_EOL;
}
?>

上の例の出力は、 たとえば以下のようになります。

PATH: /tmp/inclued
Inclued file: inclued.56521.1
 -- filename: /Users/philip/test.php
 -- number of includes: 1

Inclued file: inclued.56563.1
 -- filename: /tmp/none.php
 -- number of includes: 0

Inclued file: inclued.56636.1
 -- filename: /tmp/three.php
 -- number of includes: 3

add a note add a note

User Contributed Notes 3 notes

up
1
g dot kuizinas at anuary dot com
11 years ago
If you install the module and execute it via CLI it will not produce any errors when not enabled. It took me quite a bit of time until I realised that by default the module is disabled, http://www.php.net/manual/en/inclued.configuration.php.
up
1
gtisza at gmail dot com
11 years ago
Note that inclued does not work correctly when xdebug is enabled (the includes array remains empty; consequently, graphing the classes works but graphing the files does not). Bug report: https://bugs.php.net/bug.php?id=59419
up
0
admin at eexit dot net
13 years ago
The new gengraph.php has « -t » parameter set to « includes » by default. Don't forget to specify it to « classes » for most of all projects.

Since PHP 5.3, namespace operators are stripped by Graphiz when the graph is generated. Add the following tips to escape namespace operators:

<?php
// Around line 254
else /* classes */
{
   
$filemap = array();

    foreach(
$data["classes"] as $k => $v) {
       
$class = $v;

   
// Here is the tip to remove namespaces
       
$class['name'] = str_replace('\\', '\\\\', $class['name']);
       
$class['mangled_name'] = str_replace('\\', '\\\\', $class['mangled_name']);

// ...
?>

By the way, if you are looking for an easy way to use inclued, take a look at my library: http://www.eexit.net/projects/inclued.html
To Top