ThinkPHP 3.2 中获取所有函数方法名,以及注释,完整可运行
<?php
namespace Home\Controller;
use Common\Controller\BaseController;
class AuthController extends BaseController{
/**
* @cc index主页面
*/
public function index(){
$modules = array('Home'); //模块名称
$i = 0;
foreach ($modules as $module) {
$all_controller = $this->getController($module);
foreach ($all_controller as $controller) {
$controller_name = $controller;
$all_action = $this->getAction($module, $controller_name);
foreach ($all_action as $action) {
$data[$i] = array(
'name' =>$module.'/'. $controller . '/' . $action,
'status' => 1,
'desc'=>$this->get_cc_desc($module,$controller,$action)
);
$i++;
}
}
}
echo '<pre>';
print_r($data);
}
/**
* @cc 获取所有控制器名称
*
* @param $module
*
* @return array|null
*/
protected function getController($module){
if(empty($module)) return null;
$module_path = APP_PATH . '/' . $module . '/Controller/'; //控制器路径
if(!is_dir($module_path)) return null;
$module_path .= '/*.class.php';
$ary_files = glob($module_path);
foreach ($ary_files as $file) {
if (is_dir($file)) {
continue;
}else {
$files[] = basename($file, C('DEFAULT_C_LAYER').'.class.php');
}
}
return $files;
}
/**
* @cc 获取所有方法名称
*
* @param $module
* @param $controller
*
* @return array|null
*/
protected function getAction($module, $controller){
if(empty($controller)) return null;
$content = file_get_contents(APP_PATH . '/'.$module.'/Controller/'.$controller.'Controller.class.php');
preg_match_all("/.*?public.*?function(.*?)\(.*?\)/i", $content, $matches);
$functions = $matches[1];
//排除部分方法
$inherents_functions = array('_before_index','_after_index','_initialize','__construct','getActionName','isAjax','display','show','fetch','buildHtml','assign','__set','get','__get','__isset','__call','error','success','ajaxReturn','redirect','__destruct','_empty');
foreach ($functions as $func){
$func = trim($func);
if(!in_array($func, $inherents_functions)){
if (strlen($func)>0) $customer_functions[] = $func;
}
}
return $customer_functions;
}
/**
* @cc 获取函数的注释
*
* @param $module Home
* @param $controller Auth
* @param $action index
*
* @return string 注释
*
*/
protected function get_cc_desc($module,$controller,$action){
$desc=$module.'\Controller\\'.$controller.'Controller';
$func = new \ReflectionMethod(new $desc(),$action);
$tmp = $func->getDocComment();
$flag = preg_match_all('/@cc(.*?)\n/',$tmp,$tmp);
$tmp = trim($tmp[1][0]);
$tmp = $tmp !='' ? $tmp:'无';
return $tmp;
}
}
其中注释必须采用固定格式,
/**
* @cc index主页面
*/
适用于ThinkPHP 3.2.2 ,适合用auth 认证时,直接提取所有的控制器及方法名,可以获取注释。非常方便。
参考:
1、http://www.thinkphp.cn/code/718.html
2、http://www.thinkphp.cn/topic/10357.html
ThinkPHP 3.2 中获取所有函数方法名,以及注释,完整可运行的更多相关文章
- java反射查看jar包中所有的类名方法名
不反编译,不用其他工具,用java反射查看jar包中所有的类名方法名,网上很多都报错,下面这个你试试看:话不多说直接撸代码: import java.lang.reflect.Field; impor ...
- C# 反射总结 获取 命名空间 类名 方法名
一.获取 命名空间 类名 方法名 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- c# 获取命名空间 类名 方法名
c# 获取命名空间 类名 方法名 转[http://blog.sina.com.cn/s/blog_3fc2dcc1010189th.html] 分类: Winform public static ...
- vs2010中自动给函数或者类加上注释宏模板
Sub AddFunComment() Dim DocSel As EnvDTE.TextSelection DocSel = DTE.ActiveDocument.Selection DocSel. ...
- FFmpeg 如何探测网络流格式/如何从内存中获取数据
文章转自:http://blog.csdn.net/rootusers/article/details/42551935 一般ffmpeg都是直接从文件中读取或者从网络流中读取,比如rtp://xx. ...
- 关于在C#中构造函数中调用虚函数的问题
在C#中如果存在类的继承关系,应避免在构造函数中调用虚函数.这是由于C#的运行机制造成的,原因如下: 新建一个类实例时,C#会先初始化该类(对类变量赋值,并将函数记在函数表中),然后再初始化父类.构造 ...
- python-获取类名和方法名,动态创建类和方法及属性
获取类名和方法名1.在函数外部获取函数名称,用.__name__获取2.在函数内部获取当前函数名称,用sys._getframe().f_code.co_name方法获取3.使用inspect模块动态 ...
- PHP获取当前类名、函数名、方法名
PHP获取当前类名.方法名 __CLASS__ 获取当前类名 __FUNCTION__ 当前函数名(confirm) __METHOD__ 当前方法名 (bankcard::confirm) _ ...
- Thinkphp中的 I 函数(Thinkphp3.2.3版本)
I 函数的作用是获取系统变量,必要时还可以对变量值进行过滤及强制转化,I 函数的语法格式: I('变量类型.变量名/修饰符',['默认值'],['过滤方法或正则'],['额外数据源']) 一.获取变量 ...
随机推荐
- PAT 1131. Subway Map (30)
最短路. 记录一下到某个点,最后是哪辆车乘到的最短距离.换乘次数以及从哪个位置推过来的,可以开$map$记录一下. #include<map> #include<set> #i ...
- 训练continue
11.16 树状数组 http://codeforces.com/contest/1070/problem/C digit sum+% dp http://codeforces.com/contest ...
- Luogu P3391 【模板】文艺平衡树(FHQ-Treap)
题意 给出一个长为$n$序列$[1,2,...,n]$,$m$次操作,每次指定一段区间$[l,r]$,将这段区间翻转,求最终序列 题解 虽然标题是$Splay$,但是我要用$FHQ\ Treap$,考 ...
- python队列、线程、进程、协程(转)
原文地址: http://www.cnblogs.com/wangqiaomei/p/5682669.html 一.queue 二.线程 #基本使用 #线程锁 #自定义线程池 #生产者消费者模型(队列 ...
- 最长上升子序列:2016 Pacific Northwest Region Programming Contest—Division 2 Problem M
Description A string of lowercase letters is calledalphabeticalif deleting zero or more of its lette ...
- DP套DP
DP套DP,就是将内层DP的结果作为外层DP的状态进行DP的方法. [BZOJ3864]Hero meet devil 对做LCS的DP数组差分后状压,预处理出转移数组,然后直接转移即可. tr[S] ...
- hdu 1251 (Trie水题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- 【高斯消元】BZOJ1013-[JSOI2008]球形空间产生器sphere
[题目大意] 给出n维空间中给出n+1个点的坐标,求出球心坐标. [思路] 令球心坐标为x1,x2...xn,假设当前第i个点坐标为a1,a2...,an,第i+1个点坐标为b1,b2...,bn,则 ...
- python安装BeautifulSoup
1.先下载pip https://pypi.python.org/pypi/pip 安装pip cd到路径 python setuo.py install 2.添加目录到环境变量中 xxx\Pytho ...
- Problem D: 结构体:计算输入日期是该年的第几天
#include <stdio.h> struct time{ int year; int month; int day;}; int main(void) { struct time s ...