2014-04-15 13 views
0

目前我正在解析PHP代碼,並將生存建立一個包含/需求和功能樹:通過包含/需求樹我的意思是一棵樹,其中很明顯哪些文件包含其他文件 - 在這裏我必須留意循環,但它是可行的。比方說,我有一個文件a.php,其中又包括b.php和c.php,c.php還包括d.php,樹應該以顯示層次結構的方式構建。PHP - 建設包含/需求樹

如果也可以對函數完成這項工作,那麼一個函數調用第二個函數並調用第三個函數也是很好的。

我的問題是這樣一個工具或腳本是否已經可用,因爲我不需要任何幻想,但另一方面,我不想重新發明輪子。

回答

0

我在想,也許你可以使用像REGEX這樣的東西來解析PHP文件(不知道是否有像DOMDocument for PHP這樣會使它更容易)獲取任何包含或需要的內容。像這樣:

function make_tree($php_file){ 
    //specify output file 
    $output = "" 

    //set counter 
    $counter = 1; 

    //make REGEX or other parsing array of 
    //all the include, include_once, 
    //require, and require_once elements 

    foreach($regex_array[] as $url){ 
     $output .= $counter . ": Page included/required " . $url . "."; 

     //run make_tree on subpage 
     make_tree($url); 

     //increment counter 
     $counter = $counter + 1; 
    } 

    //output the output 
    return $output; 
} 

這是非常粗糙,可能需要一些修訂,但它似乎應該工作。輸出示例:

1: Page included/required first_require.php.  //required by your first document 
1: Page included/required req1.php    //required by first_require.php 
2: Page included/required req2.php    //required by first_require.php 
1: Page included/required penguin.php   //required by req2.php 
3: Page included/required req3.php    //required by first_require.php 

有跡象表明,也許可以固定嵌套數組的一些重大問題,但我不知道如何實現它現在。

0

功能get_included_files()中內置的PHP似乎對這種情況最有用。示例:

$ included_files = get_included_files();

foreach($ included_files as $ filename){ echo「$ filename \ n」; }