开发者

How to get list of declared functions with their data from php file?

I need to get list of functions with their contents (not only the function name) from php file. I tried to use regex but it has lots of limitations. it doesn't parse all types of functions. for example it fails if the function has if and for loop statements.

in details: I have around 100 include files. each file has number of declared functions. some files has functions duplicated in other file开发者_如何学Cs. so what I want is to get list of all functions from specific file then put this list inside an array then I will be using array unique in order to remove the duplicates. I read about tokenizer but I really have no idea how to make it grab the declared function with its data. all I have is this:

function get_defined_functions_in_file($file) 
{
    $source = file_get_contents($file);
    $tokens = token_get_all($source);

    $functions = array();
    $nextStringIsFunc = false;
    $inClass = false;
    $bracesCount = 0;

    foreach($tokens as $token) {
        switch($token[0]) {
            case T_CLASS:
                $inClass = true;
                break;
            case T_FUNCTION:
                if(!$inClass) $nextStringIsFunc = true;
                break;

            case T_STRING:
                if($nextStringIsFunc) {
                    $nextStringIsFunc = false;
                    $functions[] = $token[1];
                }
                break;

            // Anonymous functions
            case '(':
            case ';':
                $nextStringIsFunc = false;
                break;

            // Exclude Classes
            case '{':
                if($inClass) $bracesCount++;
                break;

            case '}':
                if($inClass) {
                    $bracesCount--;
                    if($bracesCount === 0) $inClass = false;
                }
                break;
        }
    }

    return $functions;
}

unfortunately, this function lists only the function names.. what I need is to list the whole declared function with its structure.. so any ideas?

thanks in advance..


If you got the function names from your get_defined_functions, consider using Reflection API for the remaining work.

Example:

include 'file-with-functions.php';
$reflector = new ReflectionFunction('foo'); // foo() being a valid function
$body = array_slice(
    file($reflector->getFileName()), // read in the file containing foo()
    $reflector->getStartLine(), // start to extract where foo() begins
    $reflector->getEndLine() - $reflector->getStartLine()); // offset

echo implode($body);

Like @nunthrey suggested, you can also use Zend_Reflection to get both: the functions in a file and their content. Example with Zend_Reflection:

$reflector = new Zend_Reflection_File('file-with-functions.php');
foreach($reflector->getFunctions() as $fn) {
    $function = new Zend_Reflection_Function($fn->name);
    echo $function->getContents();
}


Try Zend_Reflection http://framework.zend.com/manual/en/zend.reflection.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜