php反射获取方法及其注释控制权限

来源:互联网 发布:php htmlspecialchars 编辑:程序博客网 时间:2024/05/16 06:33
此处主要用PHP的反射获取方法名及其注释,方便权限的控制。下面给出一个CI下的例子:
    说明:
        控制器里的方法及其注释书写格式为:
        /** 注释 */ 
        private $方法名 = FALSE; 
============================================================================================= 

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * ======================================================
 *
 *        Filename:  access.php
 *
 *     Description:  获取控制器的方法及其注释
 *
 *         Created:  2014年7月22日18:35:55
 *
 *          Author: 
 *         Company:  uvge
 *
 * ======================================================
 */
class Access extends CI_Controller {
//构造方法,加载model
     function __construct() {
parent::__construct();
$this -> load ->model('Access_model');
$this -> load ->helper('file');
}
    //加载方法管理页面 
public function access_index(){
$data['main_content'] = 'access_view';
$this -> load -> view('includes/template_view', $data);
}
/*取出方法名及其注释*/
function getMethod() {
                //取出所有的控制器 
$filenames = get_filenames('./application/controllers/');
$files = array();
$method = array();//全部方法名
$comment = array();//方法对应的注释
// 引入控制器文件
foreach($filenames as $k => $v)
{//过滤文件
    if($v == 'access.php' || $v == 'index.html' || $v =="index.php")
    {
        unset($filenames[$k]);
    }
    else
    {
    array_push($files, $v);
    include($v);
    }
}

foreach ($files as $key => $value)
{
$r = new ReflectionClass(substr($value,0,strrpos($value,'.')));//去除文件名后缀substr($value,0,strrpos($value,'.'))

$properties = $r->getProperties(ReflectionProperty::IS_PRIVATE);
// 获取全部方法名
foreach ($properties as &$property)
{
    array_push($method, $property->getName());
}
//获取方法对应的注释
foreach($properties as &$property)
{
    $str = $property->getDocComment();
    $str = trim(substr($str, 3, -2));
    array_push($comment, $str);
}
}
array_push($method, 'access_index');
array_push($comment, '加载方法管理视图');
array_push($method, 'getMethod');
array_push($comment, '将方法名及其注释写入数据库');
array_push($method, 'access_get');
array_push($comment, '取出方法名和注释');
array_push($method, 'access_bind_action');
array_push($comment, '角色绑定方法');

$data = array();
for($i = 0; $i < count($method ); $i++)
{
$data[$i] = array('name' => $method[$i], 'comment' => $comment[$i]);
}
$res = $this -> Access_model ->updateMethod($data,$method);
if ($res)
{
$json_array = array('code' => 1);
echo json_encode ( $json_array );
}
else
{
$json_array = array('code' => 0);
echo json_encode ( $json_array );
}
}

public function access_get()
{
$limit = $this -> input -> post('pagesize');
$offset = ($this -> input -> post('page') - 1) * $limit;
$data = $this -> Access_model -> get($limit,$offset);
$datas = $this -> Access_model -> get();
$json_array = array();
$j = 0;
foreach ($data->result() as $row) {
$json_array['Rows'][$j++] = array('name' => $row -> name,'comment' => $row -> comment);
}
$json_array['Total'] = $datas -> num_rows();
echo json_encode($json_array);
}
/**
 * 将角色和方法绑定在一起
 */
function access_bind_action () {
$code  =  $this -> Access_model -> get_post_id();
$json_array = array();
$json_array['code'] = $code;
echo json_encode($json_array);
}

=======================================================
that's all. 
0 0