15

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

理想情况下,我希望看到可以传递一堆文件并让它返回所有文件、类、属性和方法以及它们的元数据的东西,这样我就可以构建一些基于数据。

是否有任何仅限 DocBlock 解析器的项目可以帮助我完成这项任务,还是我被困在重新发明那个轮子上?

4

4 回答 4

23

您可以使用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();

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

还有一个PEAR 包可以解析 DocBlocks。

于 2010-03-27T22:23:22.507 回答
6

如果有人需要正则表达式(xdazz 建议尝试这个,student310评论说它可以满足她/他的需要)

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

示例(演示

<?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);
于 2012-09-02T14:41:17.033 回答
5

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

于 2011-10-14T18:12:15.603 回答
3

正如furgas指出的那样,我多年来一直将phpDocumentor作为一个独立项目使用,它运行良好。

<?php
$class = new ReflectionClass('MyClass');
$phpdoc = new \phpDocumentor\Reflection\DocBlock($class);

var_dump($phpdoc->getShortDescription());
var_dump($phpdoc->getLongDescription()->getContents());
var_dump($phpdoc->getTags());
var_dump($phpdoc->hasTag('author'));
var_dump($phpdoc->hasTag('copyright'));
于 2014-09-01T10:41:13.250 回答