1. 使用composer安装.因为是不支持composer的框架,所以暂时放弃了该方法.
  2. 使用pear安装. 在安装pear的时候 php go-pear.phar 总是无限循环在安装步骤中,即使设置php-cli路径也是失败.
  3. 官网下载phpDocumentor.phar. https://www.phpdoc.org/

image.png

将下载好的文件放到php.exe所在的目录中,然后就可以通过命令去生成文档了`

php phpDocumentor.phar -d "要生成文档的路径" -t "生成的文档存放路径"
php phpDocumentor.phar -d Pro/ -t docs/api

-d后面跟文件夹路径,如果是单个文件,可以用-f。-t表示目标target,后面跟API文档生成的文件夹。如果需要指定模板,可以在代码后面追加--template。

php phpDocumentor.phar -d Pro/ -t docs/api --template="clean" 

可以通过template:list查看所支持的内置模板

php phpDocumentor.phar template:list 

可以直接在template后面接自定义模板

php phpDocumentor.phar -d Pro/ -t docs/api --template="data/template/my_template"

使用

php phpDocumentor.phar -f "D:\Visual-NMP-x64\www\xinhu0905\webmain\task\api\test.php" -t "D:\document"

会报错.因为cli模式运行使用的是7.2版本的PHP.
image.png

于是,更改phar包.

$phar = new Phar('PharFile.phar'); $phar->extractTo('PharFile');

运行即可,但是更改完后,再压缩报错,无法解决,于是放弃:

$phar = new Phar('test.phar');
// $phar->extractTo('fmt');
//指定压缩的目录,第二个参数为指定压缩的文件类型正则
$phar->buildFromDirectory(__DIR__ . '/phars/', '/.php$/');
//压缩方式 Phar::GZ  PHAR::BZ2
$phar->compressFiles(Phar::GZ);
//设置默认的启动文件,也就是入口文件
$phar->setStub($phar->createDefaultStub('index.php'));

打包之前要把php.ini中的配置项phar.readonly 设置成Off

注意事项

打包成phar后如果脚本里有使用  dir 这些常量的话要把 phar:// 替换掉,要不然file_get_content mkdir 等一些函数会报错找不到路径,但是require_once include等是可以用的

define('ROOT_PATH', str_replace('phar://', '', dirname(__dir__)));

或着用下面来取当前所在目录

define('ROOT_PATH', realpath('.'));

最后只能妥协使用5.6版本的PHP运行该文件:

 D:/Visual-NMP-x64/Bin/PHP/php-5.6.36-nts-x64/php.exe phpDocumentor.phar -f "D:\Visual-NMP-x64\www\xinhu0905\webmain\task\api\test.php" -t "D:\document"

生成页面:
image.png

参考文档:
phpDocumentor
PHPDocumentor的代码注释规范说明
PHP代码打包---phar
php打包phar文件
phpDocumentor使用教程【安装PHPDocumentor】
后续尝试使用apidoc,也是一种比较好替代.


嘟嘟胖胖啊
73 声望3 粉丝

哼哼哈哈