ThinkPHP 3.2.2 获取项目所有方法名称

jerry thinkphp 2015年11月19日 收藏
ThinkPHP 3.2.2 获取项目所有方法名称,用途:呵呵!
如果使用了多级控制器,请自行修改代码。
  1.     public function index(){
  2.         $modules = array('Admin');  //模块名称
  3.         $i = 0;
  4.         foreach ($modules as $module) {
  5.             $all_controller = $this->getController($module);
  6.             foreach ($all_controller as $controller) {
  7.                 $controller_name = $module.'/'.$controller;
  8.                 $all_action = $this->getAction($controller_name);
  9.                 foreach ($all_action as $action) {
  10.                     $data[$i]['module'] = $module;
  11.                     $data[$i]['controller'] = $controller;
  12.                     $data[$i]['action'] = $action;
  13.                     $i++;
  14.                 }
  15.             }
  16.         }
  17.         echo '<pre>';
  18.         print_r($data);
  19.     }
  20.     //获取所有控制器名称
  21.     protected function getController($module){
  22.         if(empty($module)) return null;
  23.         $module_path = APP_PATH . '/' . $module . '/Controller/';  //控制器路径
  24.         if(!is_dir($module_path)) return null;
  25.         $module_path .= '/*.class.php';
  26.         $ary_files = glob($module_path);
  27.         foreach ($ary_files as $file) {
  28.             if (is_dir($file)) {
  29.                 continue;
  30.             }else {
  31.                 $files[] = basename($file, C('DEFAULT_C_LAYER').'.class.php');
  32.             }
  33.         }
  34.         return $files;
  35.     }
  36.     //获取所有方法名称
  37.     protected function getAction($controller){
  38.         if(empty($controller)) return null;
  39.         $con = A($controller);
  40.         $functions = get_class_methods($con);
  41.         //排除部分方法
  42.         $inherents_functions = array('_initialize','__construct','getActionName','isAjax','display','show','fetch','buildHtml','assign','__set','get','__get','__isset','__call','error','success','ajaxReturn','redirect','__destruct', '_empty');
  43.         foreach ($functions as $func){
  44.             if(!in_array($func, $inherents_functions)){
  45.                 $customer_functions[] = $func;
  46.             }
  47.         }
  48.         return $customer_functions;
  49.     }
改了下,用正则获取所有公共方法
  1.     public function index(){
  2.         $modules = array('Admin');  //模块名称
  3.         $i = 0;
  4.         foreach ($modules as $module) {
  5.             $all_controller = $this->getController($module);
  6.             foreach ($all_controller as $controller) {
  7.                 $controller_name = $controller;
  8.                 $all_action = $this->getAction($module, $controller_name);
  9.                 foreach ($all_action as $action) {
  10.                     $data[$i] = array(
  11.                         'name' => $controller . '_' . $action,
  12.                         'status' => 1
  13.                     );
  14.                     $i++;
  15.                 }
  16.             }
  17.         }
  18.         echo '<pre>';
  19.         print_r($data);
  20.     }

  21.     //获取所有控制器名称
  22.     protected function getController($module){
  23.         if(empty($module)) return null;
  24.         $module_path = APP_PATH . '/' . $module . '/Controller/';  //控制器路径
  25.         if(!is_dir($module_path)) return null;
  26.         $module_path .= '/*.class.php';
  27.         $ary_files = glob($module_path);
  28.         foreach ($ary_files as $file) {
  29.             if (is_dir($file)) {
  30.                 continue;
  31.             }else {
  32.                 $files[] = basename($file, C('DEFAULT_C_LAYER').'.class.php');
  33.             }
  34.         }
  35.         return $files;
  36.     }

  37.     //获取所有方法名称
  38.     protected function getAction($module, $controller){
  39.         if(empty($controller)) return null;
  40.         $content = file_get_contents(APP_PATH . '/'.$module.'/Controller/'.$controller.'Controller.class.php');
  41.         preg_match_all("/.*?public.*?function(.*?)\(.*?\)/i", $content, $matches);
  42.         $functions = $matches[1];
  43.         //排除部分方法
  44.         $inherents_functions = array('_initialize','__construct','getActionName','isAjax','display','show','fetch','buildHtml','assign','__set','get','__get','__isset','__call','error','success','ajaxReturn','redirect','__destruct','_empty');
  45.         foreach ($functions as $func){
  46.             $func = trim($func);
  47.             if(!in_array($func, $inherents_functions)){
  48.                 $customer_functions[] = $func;
  49.             }
  50.         }
  51.         return $customer_functions;
  52.     }