php 反射调用

<?php
/**
 * Created by PhpStorm.
 * User: talen
 * Date: 2018/12/22
 * Time: 10:52
 */

namespace App\Controller;

use App\Common\SeasLog;
use ReflectionClass;
use Swoolefy\Core\Swfy;

class IndexController extends BaseController {

    private $param_cfg;
    private $func_name;
    private $func_data;
    private $func_model;

    /**
     * @return string
     * @throws \ReflectionException
     */
    public function index() {

        $this->init();
        $res  = $this->getRequestParams();
        foreach ($res as $key=>$value){
            $res = $key;
        }
        $this->checkData($res);
        $class = new ReflectionClass($this->func_model); // 建立 这个类的反射类
        $instance = $class->newInstance($this->param_cfg, $this->func_data);
        if (!$class->hasMethod($this->func_name)) {
            $this->return_error(99998, "暂未提供");
        }
        $method = $class->getMethod($this->func_name);
        if (!$method->isPublic()) {
            $this->return_error(99998, "暂未提供");
        }
        $result = $method->invoke($instance, $this->func_data);
        if ($result === false){
            $result = ['code' => $class->getMethod('getErrorCode')->invoke($instance), 'msg' =>  $class->getMethod('getErrorMsg')->invoke($instance)];
            $this->returnJson($result);
        }
        $this->returnJson($result);
    }


    public function init(){
        $conf = Swfy::getConf();
        $log_file = $conf['setting']['log_file'];
        if (!empty($log_file)) {
            \SeasLog::setBasePath(LOG_PATH);
        }

        // 加载公共文件
        $commFile = __DIR__ . '/../Common/Common.php';
        if (is_file($commFile)) {
            include_once $commFile;
        }

    }


    public function checkData($strBody){
        $data = json_decode($strBody, true);
        if (!is_array($data)) return false;
        if (!array_key_exists(self::FUNCTION_NAME, $data) || !array_key_exists(self::FUNCTION_DATA, $data)) return false;
        $this->func_name = $data[self::FUNCTION_NAME];
        $this->func_data = $data[self::FUNCTION_DATA];
        $this->param_cfg = $this->cfgDecode($data[self::PARAM_CFG], $this->func_name, $this->func_data);
        $this->func_model = "App\Model\\".$data['func_model'];
        SeasLog::info("http($this->func_name) => " . $this->func_name, ['func_data' => $this->func_data, 'param_cfg' => $this->param_cfg], $data['func_model']);

    }

    private function cfgDecode($strCfg, $func_name, $func_data)
    {
        if (is_array($func_data)) $strData = json_encode($func_data, JSON_UNESCAPED_UNICODE);
        else $strData = $func_data;
        $strKey = base64_encode(substr(md5($func_name . '-' . $strData), 8, 16));
        $strJson = \decrypt($strCfg, $strKey);
        return json_decode($strJson, true);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值