使用反射提取項目中函數和類定義原型和注釋

來源:互聯網
上載者:User
前幾天有朋友希望得到 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; $isave($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);}}  }}



回複討論(解決方案)

說真的,這個方式雖然看方法之類的全一些,但感覺要比手冊差遠了。
最好的還是直接看源碼,看看注釋,參數之類就夠了,況且還是中文的。

贊一個,斑竹最近很閑啊。

贊一個。

反射機制還是很有用處的,版主的精神更是可佳。

ThinkPHP 類庫參考手冊:
http://doc.thinkphp.cn/reference.html

好像很高端的樣子~

怎麼那麼像.net文法

我是來學習的!

learning

學習,贊一個

太難了!都沒看明白呢!

連帶注釋都查了出來?強悍!

接個分。

強貼留名。

額,表示看不懂。。

版主果真強大,膜拜中~~~,佔個位置學習學習。

跟蹤學習

路過,贊一個

看帖拿分!

Thinkphp手冊極端的好。簡單而又全面。

高端。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.