Home > Backend Development > PHP Tutorial > 使用反照提取项目中函数和类定义原型和注释

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

WBOY
Release: 2016-06-13 12:05:20
Original
947 people have browsed it

使用反射提取项目中函数和类定义原型和注释
前几天有朋友希望得到 ThinkPHP 全部函数和类定义的文档。手工整理出来也的确很费事。
为此,用 php 的反射功能写了一个程序。
欢迎拍砖!

if(isset($_GET['fn'])) new appdoc($_GET['fn']);<br />else {<br />	$path = 'ThinkPHP';<br />	//$path = 'phpcms';<br />	//$path = 'wordpress';<br /><br />	new appdoc(realpath($path));<br />}<br /><br />class appdoc {<br />  private $data = array();<br />  private $next = array();<br />  function __construct($path='') {<br />	if(is_array($path) || is_file($path)) return $this->analysis($path);<br /><br />	$res = glob($path . DIRECTORY_SEPARATOR . '*', GLOB_NOSORT);<br />	$next = array();<br />	for($i=0; $i<count($res); $i++) {<br />		$fn = $res[$i];<br />		if(is_dir($fn)) {<br />			$res = array_merge($res, glob($fn . DIRECTORY_SEPARATOR . '*', GLOB_NOSORT));<br />			continue;<br />		}<br />		if(! in_array(pathinfo($fn, PATHINFO_EXTENSION), array('php', 'inc'))) continue;<br />		$s = $this->save($this->load($fn));<br />		if($s) $this->next[$s] = $fn;<br />	}<br />	$this->checknext();<br /><br />	$s = join(PHP_EOL.PHP_EOL, $this->data);<br />	if(mb_check_encoding($s, 'utf-8')) $s = iconv('utf-8', 'gbk', $s);<br />	header("Content-type: text/plain;charset=gbk");<br />	echo $s, PHP_EOL . PHP_EOL;<br />	echo '文件列表' . PHP_EOL;<br />	echo join(PHP_EOL, $res);<br />	if($this->next) {<br />		echo PHP_EOL . PHP_EOL . '残余的' . PHP_EOL;<br />		print_r($this->next);<br />	}<br />  }<br />  private function load($fn) {<br />	$u = is_array($fn) ? http_build_query(array('fn' => array_values($fn))) : "fn=$fn";<br />	$url = "http://$_SERVER[HTTP_HOST]$_SERVER[PHP_SELF]?$u";<br />	$curl = curl_init();<br />	curl_setopt($curl, CURLOPT_URL, $url);<br />	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);<br />	return curl_exec($curl);<br />  }<br />  private function checknext() {<br />	foreach($this->next as $s=>$fn) {<br />		switch(1) {<br />			case is_numeric($s): break;<br />			case preg_match("/Class '(\w+)' not found/", $s, $m) :<br />				$m = preg_quote($m[1]);<br />				foreach(preg_grep("/class $m/i", $this->data) as $r) {;<br />					if(preg_match('/@@\s+(\S+)/', $r, $m)) {<br />						array_unshift($this->next, $m[1]);<br />						break;<br />					}<br />				}<br />				break;<br />		}<br />	}<br />	$u = http_build_query(array('fn' => array_values($this->next)));<br /><br />	$s = $this->save($this->load($this->next));<br />	$this->next = array();<br /><br />	if(empty($s)) unset($this->next[$s]);<br />	else $this->next[] = $s;<br />  }<br />  private function save($s) {<br />	if(empty($s) || preg_match('/~runtime.php/i', $s)) return '';<br />	if(preg_match('#<b>Fatal error</b>#', $s)) return $s;<br />	$t = array();<br />	$ar = preg_split("/[\r\n]+/", $s);<br />	foreach($ar as $i=>$v) {<br />		$t[] = $v;<br />		if($v == '}') {<br />			$t = join(PHP_EOL, $t);<br />			if(! in_array($t, $this->data))	$this->data[] = $t;<br />			$t = array();<br />		}<br />	}<br />	return '';<br />  }<br />  private function import($fn) {<br />	ob_start();<br />	if(is_array($fn)) foreach($fn as $f) include_once($f);<br />	else include_once($fn);<br />	ob_end_clean();<br />  }<br />  private function analysis($fn) {<br />	if(! is_array($fn) && preg_match('/~runtime.php$/i', $fn)) return;<br />	$last = get_defined_constants();<br />	$this->import($fn);<br />	if($t = array_diff($last, get_defined_constants())) {<br />		echo 'Constants' . join(PHP_EOL . "\t", $t) . PHP_EOL . PHP_EOL;<br />	}<br />	foreach(get_defined_functions()['user'] as $name) {<br />		$func = new ReflectionFunction($name);<br />		Reflection::export($func);<br />	}<br />	foreach(get_declared_classes() as $name) {<br />		if(__CLASS__ == $name) continue;<br />		$class = new ReflectionClass($name);<br />		if($class->isUserDefined()) {<br />			Reflection::export($class);<br />		}<br />	}<br />	foreach(get_declared_interfaces() as $name) {<br />		$interfaces = new ReflectionClass($name);<br />		if($interfaces->isUserDefined()) {<br />			Reflection::export($interfaces);<br />		}<br />	}<br />  }<br />}<br />
Copy after login



------解决方案--------------------
说真的,这个方式虽然看方法之类的全一些,但感觉要比手册差远了。
最好的还是直接看源码,看看注释,参数之类就够了,况且还是中文的。
------解决方案--------------------
赞一个,斑竹最近很闲啊。
------解决方案--------------------
赞一个。
------解决方案--------------------

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template