怎么監(jiān)聽某一個文件里面所有的方法
主要是監(jiān)聽api目錄下所有文件里面的方法
$statistic_address = 'udp://127.0.0.1:55656';
// 判斷數(shù)據(jù)是否正確
if(empty($data['class']) || empty($data['method']) || !isset($data['param_array']))
{
// 發(fā)送數(shù)據(jù)給客戶端,請求包錯誤
return $connection->send(array('code'=>400, 'msg'=>'bad request', 'data'=>null));
}
// 獲得要調(diào)用的類、方法、及參數(shù)
$class = $data['class'];
$method = $data['method'];
$param_array = $data['param_array'];
StatisticClient::tick($class, $method);
$success = false;
// 判斷類對應(yīng)文件是否載入
if(!class_exists($class))
{
$include_file = __DIR__ . "/Services/$class.php";
if(is_file($include_file))
{
require_once $include_file;
}
if(!class_exists($class) || !method_exists($class, $method))
{
$code = 404;
$msg = "class $class or method $method not found";
StatisticClient::report($class, $method, $success, $code, $msg, $statistic_address);
// 發(fā)送數(shù)據(jù)給客戶端 類不存在
return $connection->send(array('code'=>$code, 'msg'=>$msg, 'data'=>null));
}
}
// 調(diào)用類的方法
try
{
$ret = call_user_func_array(array($class, $method), $param_array);
StatisticClient::report($class, $method, 1, 0, '', $statistic_address);
// 發(fā)送數(shù)據(jù)給客戶端,調(diào)用成功,data下標對應(yīng)的元素即為調(diào)用結(jié)果
return $connection->send(array('code'=>0, 'msg'=>'ok', 'data'=>$ret));
}
// 有異常
catch(Exception $e)
{
// 發(fā)送數(shù)據(jù)給客戶端,發(fā)生異常,調(diào)用失敗
$code = $e->getCode() ? $e->getCode() : 500;
StatisticClient::report($class, $method, $success, $code, $e, $statistic_address);
return $connection->send(array('code'=>$code, 'msg'=>$e->getMessage(), 'data'=>$e));
}