有没有可用的PHP DocBlock解析器工具?

Bea*_*sen 15 php documentation parsing docblocks

我想为一些项目构建一些规模较小但高度定制的文档站点.PhpDocumentor非常棒,但它很重.我考虑过尝试调整模板,但是在花了几分钟的时间研究之后,我认为这将是太多的工作.

理想情况下,我希望看到一些东西,我可以传递一堆文件,并让它返回所有的文件,类和属性和方法,以及他们的元数据,以便我可以构建一些简单的模板基于数据.

是否有任何DocBlock解析器项目可以帮助我完成这项任务,还是我重新发明了那个轮子?

Gor*_*don 21

您可以使用Reflection API轻松完成此操作:

/**
 * This is an Example class
 */
class Example
{
    /**
     * This is an example function
     */
    public function fn() 
    {
        // void
    }
}

$reflector = new ReflectionClass('Example');

// to get the Class DocBlock
echo $reflector->getDocComment()

// to get the Method DocBlock
$reflector->getMethod('fn')->getDocComment();
Run Code Online (Sandbox Code Playgroud)

请参阅本教程:http://www.phpriot.com/articles/reflection-api

还有一个可以解析DocBlocks 的PEAR包.

  • 要使用反射,您是否必须以某种方式加载包含感兴趣的类的 PHP 脚本?对于任意长的 PHP 脚本列表,如何实现这一点? (2认同)

hak*_*kre 6

如果有人需要正则表达式(xdazz 建议尝试这个,而student310评论它适用于她/他的需要)

if (preg_match_all('/@(\w+)\s+(.*)\r?\n/m', $str, $matches)){
  $result = array_combine($matches[1], $matches[2]);
}
Run Code Online (Sandbox Code Playgroud)

示例(演示):

<?php
$str ='
/**    
 * @param   integer  $int  An integer
 * @return  boolean
 */
';
if (preg_match_all('/@(\w+)\s+(.*)\r?\n/m', $str, $matches)){
  $result = array_combine($matches[1], $matches[2]);
}

var_dump($result);
Run Code Online (Sandbox Code Playgroud)


Fur*_*gas 5

只是为了更新答案.您可能还想查看phpDocumentor2项目.我认为它的PHP DocBlock解析器可以很容易地作为独立解决方案提取.

  • DocBlox已经与phpDocumentor合并成为[phpDocumentor2](http://github.com/phpDocumentor/phpDocumentor2). (2认同)