martinsik/php-doc-parser

PHP 文档解析器,具有 CLI 接口,输出为 JSON + Markdown。

2.0.2 2015-11-16 10:43 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:42:48 UTC


README

Build Status

此包从 php.net 下载 gzip 压缩的文档,解析它并将所有找到的函数以 JSON 格式(带 Markdown 语法)输出。它附带 CLI 接口,便于使用。

安装

martinsik/php-doc-parser 添加到您的 composer.json 依赖中

"require": {
    ...
    "martinsik/php-doc-parser": "~2.0"
}

然后运行 composer.phar install

使用方法

作为 CLI 脚本

Composer 将 doc-parser 文件添加到您的目录中(默认为 vendor/bin)。运行它并按照屏幕上的说明操作。

$ vendor/bin/doc-parser

结果默认保存到 output 目录。这将创建以下文件(文件名由选定的语言和镜像生成)

  • en_php_net.json - 包含所有解析函数及其数据的非常大的关联数组。请参见下文 示例输出
  • en_php_net.list.json - 所有函数名称的小写列表。
  • en_php_net.examples.json(可选)- 如果您选择导出示例,它将它们放入单独的文件中。

要查看完整选项列表,请运行

$ vendor/bin/doc-parser help parser:run

作为第三方包

创建 DocParser\Package 类的实例以设置您要解析的语言和镜像,然后它将为您下载并解压文档。然后提供您想要解析的 DocParser\Parser 目录中的文件,它将返回一个包含所有数据的 DocParser\ParserResult 对象。

use DocParser\Package;
use DocParser\Parser;

$package = new Package('en', 'php.net');
$tmpFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $package->getOrigFilename();
$package->download($tmpFile);
$unpackedDir = $package->unpack();

$result = $parser->processDir($unpackedDir, Parser::EXPORT_EXAMPLES);
// you can parse just a single file with: $parser->processFile('file.html');

foreach ($result->getResult() as $funcName => $funcData) {
    // Note that all function names used as keys are lowercase.
    // Proper function names are in parameter lists (see [sample bellow](https://github.com/martinsik/php-doc-parser#sample-output)).
    // eg.: $funcData['params'][0]['name']
    
    // Get all examples for this function.
    // $result->getExamples($funcName);
    
    // If you used Parser::IMPORT_EXAMPLES then examples are right in $funcData.
    // With Parser::SKIP_EXAMPLES they're not parsed at all.
}

// Remove all temporary files
$package->cleanup();

示例输出

这是 DateTime::setDateen_php_net.json 中深处的样子。

{
    "abs": { ... },
    "array_pop": { ... },
    ...
    "datetime::add": { ... },
    "datetime::setdate": {
        "desc": "Sets the date.",
        "long_desc": "Resets the current date of the DateTime object to a different date.",
        "ver": "PHP 5 >= 5.2.0",
        "ret_desc": "Returns the DateTime object for method chaining or FALSE on failure.",
        "seealso": [
            "DateTime::setISODate",
            "DateTime::setTime"
        ],
        "filename": "datetime.setdate",
        "params": [
            {
                "list": [
                    {
                        "type": "int",
                        "var": "$year",
                        "beh": "required",
                        "desc": "Year of the date."
                    },
                    {
                        "type": "int",
                        "var": "$month",
                        "beh": "required",
                        "desc": "Month of the date."
                    },
                    {
                        "type": "int",
                        "var": "$day",
                        "beh": "required",
                        "desc": "Day of the date."
                    }
                ],
                "name": "DateTime::setDate",
                "ret_type": "DateTime"
            },
            {
                "list": [
                    {
                        "type": "DateTime",
                        "var": "$object",
                        "beh": "required",
                        "desc": "Procedural style only: A DateTime object returned by date\\_create(). The function modifies this object."
                    },
                    {
                        "type": "int",
                        "var": "$year",
                        "beh": "required",
                        "desc": "Year of the date."
                    },
                    {
                        "type": "int",
                        "var": "$month",
                        "beh": "required",
                        "desc": "Month of the date."
                    },
                    {
                        "type": "int",
                        "var": "$day",
                        "beh": "required",
                        "desc": "Day of the date."
                    }
                ],
                "name": "date_date_set",
                "ret_type": "DateTime"
            }
        ],
        "examples": [
            {
                "title": "DateTime::setDate() example",
                "source": "$date = new DateTime();\n$date->setDate(2001, 2, 3);\necho $date->format('Y-m-d');",
                "output": "2001-02-03"
            },
            {
                "title": "Values exceeding ranges are added to their parent values",
                "source": "$date = new DateTime();\n\n$date->setDate(2001, 2, 28);\necho $date->format('Y-m-d') . \"\\n\";\n\n$date->setDate(2001, 2, 29);\necho $date->format('Y-m-d') . \"\\n\";\n\n$date->setDate(2001, 14, 3);\necho $date->format('Y-m-d') . \"\\n\";",
                "output": "2001-02-28\n2001-03-01\n2002-02-03"
            }
        ]
    },
    "date_date_set": "DateTime::setDate",
    "datedime::createfromformat": { ... },
    "date_create_from_format": "DateTime::createFromFormat",
    ...
    "strpos": { ... }
    "tempnam": { ... }
    ...
}

注意,此函数有两个不同的定义,即 DateTime::setDatedate_date_set,它们分别接受不同的参数。为了能够搜索这两个函数,为此函数提供了两个键,其中第二个键 date_date_set 只是对第一个键的引用。此外,所有键都是小写的。

为什么?

我使用此脚本来生成名为 PHP Ninja Manual 的 Google Chrome 扩展程序的 "数据库"。

顺便说一句,还有一个官方的 PHP 文档生成器,用于 IDE,但在我开始开发我的扩展时它还不存在。我不知道它现在的功能是什么,但也许值得一试。

已知限制

  • 没有 PHP 语句(for、if、while 等)
  • 它无法识别类中类似于 mysqli 的面向对象或过程式风格。

测试

此包使用 Behat 进行测试。运行测试

$ bin/behat

许可证

PHP 文档解析器(此包)受 MIT 许可证许可。

PHP 文档页面(php.net/docs.php)受 Creative Commons Attribution 3.0 许可证 许可。