使用反射提取项目中函数和类定义原型和注释

xuzuning 2014-07-17 10:39:16
加精
前几天有朋友希望得到 ThinkPHP 全部函数和类定义的文档。手工整理出来也的确很费事。
为此,用 php 的反射功能写了一个程序。
欢迎拍砖!
if(isset($_GET['fn'])) new appdoc($_GET['fn']);
else {
$path = 'ThinkPHP';
//$path = 'phpcms';
//$path = 'wordpress';

new appdoc(realpath($path));
}

class appdoc {
private $data = array();
private $next = array();
function __construct($path='') {
if(is_array($path) || is_file($path)) return $this->analysis($path);

$res = glob($path . DIRECTORY_SEPARATOR . '*', GLOB_NOSORT);
$next = array();
for($i=0; $i<count($res); $i++) {
$fn = $res[$i];
if(is_dir($fn)) {
$res = array_merge($res, glob($fn . DIRECTORY_SEPARATOR . '*', GLOB_NOSORT));
continue;
}
if(! in_array(pathinfo($fn, PATHINFO_EXTENSION), array('php', 'inc'))) continue;
$s = $this->save($this->load($fn));
if($s) $this->next[$s] = $fn;
}
$this->checknext();

$s = join(PHP_EOL.PHP_EOL, $this->data);
if(mb_check_encoding($s, 'utf-8')) $s = iconv('utf-8', 'gbk', $s);
header("Content-type: text/plain;charset=gbk");
echo $s, PHP_EOL . PHP_EOL;
echo '文件列表' . PHP_EOL;
echo join(PHP_EOL, $res);
if($this->next) {
echo PHP_EOL . PHP_EOL . '残余的' . PHP_EOL;
print_r($this->next);
}
}
private function load($fn) {
$u = is_array($fn) ? http_build_query(array('fn' => array_values($fn))) : "fn=$fn";
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[PHP_SELF]?$u";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
return curl_exec($curl);
}
private function checknext() {
foreach($this->next as $s=>$fn) {
switch(1) {
case is_numeric($s): break;
case preg_match("/Class '(\w+)' not found/", $s, $m) :
$m = preg_quote($m[1]);
foreach(preg_grep("/class $m/i", $this->data) as $r) {;
if(preg_match('/@@\s+(\S+)/', $r, $m)) {
array_unshift($this->next, $m[1]);
break;
}
}
break;
}
}
$u = http_build_query(array('fn' => array_values($this->next)));

$s = $this->save($this->load($this->next));
$this->next = array();

if(empty($s)) unset($this->next[$s]);
else $this->next[] = $s;
}
private function save($s) {
if(empty($s) || preg_match('/~runtime.php/i', $s)) return '';
if(preg_match('#<b>Fatal error</b>#', $s)) return $s;
$t = array();
$ar = preg_split("/[\r\n]+/", $s);
foreach($ar as $i=>$v) {
$t[] = $v;
if($v == '}') {
$t = join(PHP_EOL, $t);
if(! in_array($t, $this->data)) $this->data[] = $t;
$t = array();
}
}
return '';
}
private function import($fn) {
ob_start();
if(is_array($fn)) foreach($fn as $f) include_once($f);
else include_once($fn);
ob_end_clean();
}
private function analysis($fn) {
if(! is_array($fn) && preg_match('/~runtime.php$/i', $fn)) return;
$last = get_defined_constants();
$this->import($fn);
if($t = array_diff($last, get_defined_constants())) {
echo 'Constants' . join(PHP_EOL . "\t", $t) . PHP_EOL . PHP_EOL;
}
foreach(get_defined_functions()['user'] as $name) {
$func = new ReflectionFunction($name);
Reflection::export($func);
}
foreach(get_declared_classes() as $name) {
if(__CLASS__ == $name) continue;
$class = new ReflectionClass($name);
if($class->isUserDefined()) {
Reflection::export($class);
}
}
foreach(get_declared_interfaces() as $name) {
$interfaces = new ReflectionClass($name);
if($interfaces->isUserDefined()) {
Reflection::export($interfaces);
}
}
}
}


...全文
3113 34 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_19475301 2017-01-19
  • 打赏
  • 举报
回复
引用 33 楼 qq_19475301 的回复:
引用 5 楼 cattpon 的回复:
好像很高端的样子~
....
qq_19475301 2017-01-19
  • 打赏
  • 举报
回复
引用 5 楼 cattpon 的回复:
好像很高端的样子~
咚咚咚、 2016-12-26
  • 打赏
  • 举报
回复
版主 牛掰 !
qq_34921650 2016-11-08
  • 打赏
  • 举报
回复
版主 强大 学习了
lbj23lbj 2016-11-01
  • 打赏
  • 举报
回复
老实说,tp的代码注释真是够烂的,你出来也没有用,有什么事看源码最好,还能学习
Wishos 2014-12-12
  • 打赏
  • 举报
回复
我运行了一下,其中有一句出了点问题,数组可以这样用么?我本地报错,是不是因为版本问题。
zsolis 2014-12-10
  • 打赏
  • 举报
回复
学习到了,谢谢版主分享
深圳phper 2014-11-14
  • 打赏
  • 举报
回复
chaotianwww 2014-11-05
  • 打赏
  • 举报
回复
高端。
potency 2014-08-05
  • 打赏
  • 举报
回复
Thinkphp手册极端的好。简单而又全面。
  • 打赏
  • 举报
回复
看帖拿分!
xuchuang934411545 2014-07-25
  • 打赏
  • 举报
回复
路过,赞一个
际遇_ 2014-07-24
  • 打赏
  • 举报
回复
厉害 虽然看着不是很舒服 但能想到这么做就给个大大的赞了!
腹黑的大象 2014-07-24
  • 打赏
  • 举报
回复
跟踪学习
austin9972 2014-07-23
  • 打赏
  • 举报
回复
liyingju001 2014-07-21
  • 打赏
  • 举报
回复
猪崽儿0o0 2014-07-21
  • 打赏
  • 举报
回复
版主果真强大,膜拜中~~~,占个位置学习学习。
cainiao_chen 2014-07-21
  • 打赏
  • 举报
回复
额,表示看不懂。。
傲雪星枫 2014-07-20
  • 打赏
  • 举报
回复
强贴留名。
碼上道 2014-07-20
  • 打赏
  • 举报
回复
接个分。
加载更多回复(14)
原书附带光盘文件第2章 02/ 基于Ajax的留言板示例第3章 03/3.1.3.html JavaScript在Ajax的作用范例 03/3.4.6.html 加入注释,实现九九乘法表 03/3.4.11.html 使用逻辑表达式范例第4章 04/4.1.2.html 使用if…else…处理条件不成立的情形范例 04/4.1.4.html 使用switch语句进行多重条件判断范例 04/4.1.6.html 使用do…while循环范例 04/4.1.9.html continue语句:停止当前循环进入下一次循环 04/4.2.3.html 给函数传递参数范例第5章 05/5.2.2.html 从Date对象提取信息范例 05/5.3.2.html 使用随机函数Math.randon()范例 05/5.4.4.html 使用数组的length属性范例 05/5.6.4.html 文档操作初步范例:document对象 05/5.10.2.html 使用复选框范例 05/5.11.12.html 下拉框实例:二级联动的下拉列表菜单第6章 06/6.1.3.html 使用方括号([])引用对象的属性和方法范例 06/6.1.5.html 使用大括号({})语法创建无型对象范例 06/6.1.6.html prototype原型对象范例 06/6.2.6.html 函数的apply、call方法和length属性范例 06/6.2.7.html 深入认识JavaScript的this指针范例 06/6.3.2.html 使用prototype对象定义成员范例 06/6.4.2.html 实现的私有成员范例 06/6.6.1.html 利用共享prototype实现继承范例 06/6.6.2.html 利用反射机制和prototype实现继承范例 06/6.6.3.html prototype-1.3.1框架继承实现机制范例 06/6.7.2.html 在JavaScript实现抽象范例 06/6.8.3.html 给事件处理程序传递参数范例 06/6.8.4.html 使自定义事件支持多绑定范例 06/6.9/ 实例:使用面向对象思想处理cookie第7章 07/7.2.3.html 获取cookie的值范例 07/7.2.4.html 给cookie设置终止日期范例 07/7.2.5.html 删除cookie范例 07/7.2.8.html 综合示例:构造通用的cookie处理函数 07/7.4.2.html 使用定时器实现JavaScript的延期执行或重复执行范例 07/7.4.3.html 给定时器调用传递参数范例 07/7.5.3.html 使用throw语句抛出异常范例第8章 08/8.2.7.html 使用responseText获得返回的文本范例 08/8.2.8.html 使用responseXML属性获取范例 08/8.2.9.html 使用abort方法取消一个请求范例 08/8.3.2.html 使用post方法向服务器提交数据范例 08/8.3.3.html 实现服务器相关的二级联动菜单范例 08/a.html 用于ajax获取文件的示例 08/checkUserName.asp 检测用户名的代码文件第9章 09/9.3.1.html 直接引用结点范例 09/9.5.1.html 表格操作范例 09/9.5.5.html 添加单元格范例 09/9.5.8.html 删除行和单元格范例 09/9.5.9.html 交换两行的位置范例第10章 10/10.1.3.html CSS在Ajax开发的作用 10/10.2.4.html 使用id选择器范例 10/1

20,398

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧