使用反射提取项目中函数和类定义原型和注释解决方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用反射提取项目中函数和类定义原型和注释解决方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3595字,纯文字阅读大概需要6分钟。
内容图文
![使用反射提取项目中函数和类定义原型和注释解决方法](/upload/InfoBanner/zyjiaocheng/229/39df0e6d09fc4f0bb085e6d09cb33902.jpg)
前几天有朋友希望得到 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('#Fatal error#', $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);
}
}
}
}
------解决方案--------------------
说真的,这个方式虽然看方法之类的全一些,但感觉要比手册差远了。
最好的还是直接看源码,看看注释,参数之类就够了,况且还是中文的。
------解决方案--------------------
赞一个,斑竹最近很闲啊。
------解决方案--------------------
赞一个。
------解决方案--------------------
内容总结
以上是互联网集市为您收集整理的使用反射提取项目中函数和类定义原型和注释解决方法全部内容,希望文章能够帮你解决使用反射提取项目中函数和类定义原型和注释解决方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。