本文主要介绍怎样使用PhpDocumentor
phpDocumentor是什么 phpDocumentor(以下用phpdoc代替),是一个能为php文件自动生成API文档的自动化工具。
phpdoc的目标
“Provide PHP Developers with all the tools and resources necessary to write effective and comprehensive documentation with as little effort possible.”
让php开发者用最少的精力开发出有效全面的文档
第一组phpdoc文档 对php源文件 在php源文件中我们要写入一系列的代码块(DocBlock),来让phpdoc识别怎么样,并生成文档
哪些元素可以被写入文档
Function 函数
Constant 常量
Class 类
Interface 借口
Trait 特征
Class constant 类常量
Property 属性
Method 方法
代码块怎么用 代码块长啥样 以/**
开始,以*/
结束,在开始和结束之间每一行都以*
开头
1 2 3 4 5 6 7 8 9 10 <?php /** * This is a DocBlock. */ function associatedFunction () { }
代码块内容 代码快被分为三个部分:Summary,Description,Tags,每一部分都是可选的,但是要写Description必须要有Summary。
有时被称为一个简短的描述,简要介绍相关元素的功能。
Summary遇到以下任意一种方式,就被视为结束:
1.遇到‘.’
+一个回车符
2.遇到连续的两个回车符
有时也被称为长描述,可以提供更多的信息。即Summary的详细说明。
Description遇到以下任意一种方式,就被视为结束:
1.遇到第一个tag
标签
2.遇到块标签截止符‘*/’
即标签,提供简短统一的标签符来诠释某些特征与方法(后面会有详细介绍)
每一个标签都是以@
符开始,且一行只能有一个标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <?php function myFunction($myArgument) { }
根据代码快内容生成phpdoc文档 在终端下执行phpdoc命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 ken@ken-K42JY:~$ cd '/var/www/info' ken@ken-K42JY:/var/www/info$ ls -l total 8 -rwxrwxrwx 1 root root 438 Dec 14 17:01 info.php -rwxrwxrwx 1 ken ken 771 Dec 14 17:01 info.php~ ken@ken-K42JY:/var/www/info$ phpdoc -f info.php -t ./demo/ //将info.php文件生成phpdoc文档 Collecting files .. OK Initializing parser .. OK Parsing files Parsing /var/www/info/info.php No summary was found for this file Storing cache in "/var/www/info/demo" .. OK Load cache .. 0.001 sPreparing template "clean" .. 0.022 s Preparing 17 transformations .. 0.000 s Build "elements" index .. 0.000 sReplace textual FQCNs with object aliases .. 0.000 sResolve @link and @see tags in descriptions .. 0.000 s Enriches inline example tags with their sources .. 0.000 s Build "packages" index .. 0.001 sBuild "namespaces" index and add namespaces to "elements" .. 0.000 sCollect all markers embedded in tags .. 0.000 sTransform analyzed project into artifacts .. Applying 17 transformations Initialize writer "phpDocumentor\Plugin\Core\Transformer\Writer\FileIo" Initialize writer "phpDocumentor\Plugin\Twig\Writer\Twig" Initialize writer "phpDocumentor\Plugin\Graphs\Writer\Graph" Execute transformation using writer "FileIo" Execute transformation using writer "FileIo" Execute transformation using writer "FileIo" Execute transformation using writer "FileIo" Execute transformation using writer "FileIo" Execute transformation using writer "twig" Execute transformation using writer "twig" Execute transformation using writer "twig" Execute transformation using writer "twig" Execute transformation using writer "twig" Execute transformation using writer "twig" Execute transformation using writer "twig" Execute transformation using writer "twig" Execute transformation using writer "twig" Execute transformation using writer "twig" Execute transformation using writer "twig" Execute transformation using writer "Graph" Unable to find the `dot` command of the GraphViz package. Is GraphViz correctly installed and present in your path ? 0.075 s Analyze results and write report to log .. 0.000 sken@ken-K42JY:/var /www/info$
phpdoc –h 会得到一个详细的参数表,其中几个重要的参数如下:
1 2 3 4 5 6 7 8 -f 要进行分析的文件名,多个文件用逗号隔开 -d 要分析的目录,多个目录用逗号分割 -t 生成的文档的存放路径 -o 输出的文档格式,结构为输出格式:转换器名:模板目录。 --template=responsive-twig 将模板换成responsive-twig,自带的模板:用`phpdoc template:list` 指令查看
生成的文档如下图所示:
文档目录如下图所示:
其中index.html是入口文件
No summary was found for this file 我们注意到上一段代码中的第11行报了一个错误No summary was found for this file
这是因为
Does the top of your file have two such docblocks? The first docblock is expected to be the one for the file itself, and a second docblock should pair up with the first documentable code element that appears after it. However, if you only have one docblock at the top of the file, it will get paired up with the first code element found, thus the “file itself” will seem to be missing its docblock. That is what that error is supposed to indicate.
故我们这样写我们的代码块:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 <?php /** * 文档自身的Summary. * * 文档自身的Description */ /** * A summary informing the user what the associated element does.第一个函数的summary * * A *description*, that can span multiple lines, to go _in-depth_ into the details of this element * and to provide some background information or textual references. * * @param string $myArgument With a *description* of this argument, these may also * span multiple lines. * * @return void */ function myFunction ($myArgument) { }
标记用途描述
标签
用途
描述
@abstract
用于描述抽象类的变量和方法
@access
public, private or protected
文档的访问、使用权限. @access private 表明这个文档是被保护的
@author
author name author@email
描述当前元素的作者
@copyright
名称 时间
文档版权信息
@deprecated
version
文档中被废除的方法
@deprec
同@deprecated
@example
/path/to/example
文档的外部保存的示例文件的位置
@exception
文档中方法抛出的异常,也可参照 @throws
@ignorel
忽略文档中指定的关键字
@internal
开发团队内部信息
@link
URL
类似于license 但还可以通过link找到文档中的更多个详细的信息
@name
变量别名
为某个变量指定别名
@magic
phpdoc.de compatibility
@package
封装包的名称
一组相关类、函数封装的包名称
@var
type
文档中的变量及其类型
@version
文档、类、函数的版本信息
@global
类型:$globalvarname
文档中的全局变量及有关的方法和函数
@static
记录静态类、方法
@static
var
在类、函数中使用的静态变量
@subpackage
子版本
@throws
某一方法抛出的异常
@todo
表示文件未完成或者要完善的地方
@return
如返回bool
函数返回结果描述,一般不用在void(空返回结果的)的函数中
@see
如 Class Login()
文件关联的任何元素(全局变量,包括,页面,类,函数,定义,方法,变量)
@since
version
记录什么时候对文档的哪些部分进行了更改
@param
变量含义注释
实例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 <?php /** * start page for webaccess * * PHP version 5 * * @category PHP * @package PSI_Web * @author Michael Cramer <BigMichi1@users.sourceforge.net> * @copyright 2009 phpSysInfo * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @version SVN: $Id: class.Webpage.inc.php 412 2010-12-29 09:45:53Z Jacky672 $ * @link http://phpsysinfo.sourceforge.net */ /** * generate the dynamic webpage * * @category PHP * @package PSI_Web * @author Michael Cramer <BigMichi1@users.sourceforge.net> * @copyright 2009 phpSysInfo * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @version Release: 3.0 * @link http://phpsysinfo.sourceforge.net */ class Webpage extends Output implements PSI_Interface_Output { /** * configured language * * @var String */ private $_language; /** * configured template * * @var String */ private $_template; /** * all available templates * * @var Array */ private $_templates = array (); /** * all available languages * * @var Array */ private $_languages = array (); /** * check for all extensions that are needed, initialize needed vars and read config.php */ public function __construct () { parent ::__construct(); $this ->_getTemplateList(); $this ->_getLanguageList(); } /** * checking config.php setting for template, if not supportet set phpsysinfo.css as default * checking config.php setting for language, if not supported set en as default * * @return void */ private function _checkTemplateLanguage () { $this ->_template = trim(PSI_DEFAULT_TEMPLATE); if (!file_exists(APP_ROOT.'/templates/' .$this ->_template.".css" )) { $this ->_template = 'phpsysinfo' ; } $this ->_language = trim(PSI_DEFAULT_LANG); if (!file_exists(APP_ROOT.'/language/' .$this ->_language.".xml" )) { $this ->_language = 'en' ; } } /** * get all available tamplates and store them in internal array * * @return void */ private function _getTemplateList () { $dirlist = CommonFunctions::gdc(APP_ROOT.'/templates/' ); sort($dirlist); foreach ($dirlist as $file) { $tpl_ext = substr($file, strlen($file) - 4 ); $tpl_name = substr($file, 0 , strlen($file) - 4 ); if ($tpl_ext === ".css" ) { array_push($this ->_templates, $tpl_name); } } } /** * get all available translations and store them in internal array * * @return void */ private function _getLanguageList () { $dirlist = CommonFunctions::gdc(APP_ROOT.'/language/' ); sort($dirlist); foreach ($dirlist as $file) { $lang_ext = substr($file, strlen($file) - 4 ); $lang_name = substr($file, 0 , strlen($file) - 4 ); if ($lang_ext == ".xml" ) { array_push($this ->_languages, $lang_name); } } } /** * render the page * * @return void */ public function run () { $this ->_checkTemplateLanguage(); $tpl = new Template("/templates/html/index_dynamic.html" ); $tpl->set("template" , $this ->_template); $tpl->set("templates" , $this ->_templates); $tpl->set("language" , $this ->_language); $tpl->set("languages" , $this ->_languages); echo $tpl->fetch(); } } ?>
参考文档 1.小五的博客
2.phpdoc官方文档