-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathService.php
More file actions
109 lines (91 loc) · 3.38 KB
/
Copy pathService.php
File metadata and controls
109 lines (91 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* 服务入口。
* @author 肖喜进
*/
class Service
{
private static $service;
static $failed_msg_history=[];//执行失败消息历史
private function __construct(){
set_error_handler(array($this, 'onErrorHandle'), E_USER_ERROR);
}
static function getInstance(){
if(!self::$service)
self::$service = new self();
return self::$service;
}
/**
* 捕获set_error_handle错误
*/
function onErrorHandle($errno, $errstr, $errfile, $errline)
{
$error = 'message=>'.$errstr."\r\n";
$error .= 'file=>'.$errfile."\r\n";
$error .= 'line=>'.$errline."\r\n";
\Log::put($error);
throw new \ErrorException('SYSTEM_ERROR');
}
public function run($uri,$params=[])
{
$uri = trim($uri, " \t\n\r\0\x0B/\\");
if (!$uri || Validate::notService($uri))
throw new \Exception("SERVICE_URI_INVALID");
//获取ctl,act,act_params的值
$route = explode('/', $uri);
$route_len = count($route);
if($route_len == 1){
$act_name = $route[0];
$service_name = 'Common';
}else{
$act_name= $route[$route_len-1];
unset($route[$route_len-1]);
$service_name = implode('/',$route);
}
$service_obj = \Factory::getInstance()->getProduct('service',$service_name);
if (!method_exists($service_obj, $act_name) || !is_callable([$service_obj, $act_name]))
throw new \Exception("API_NOT_FOUND");
$act_params = [];
$act_reflection = new ReflectionMethod($service_obj,$act_name);
$act_number_required_params=$act_reflection->getNumberOfRequiredParameters();
$act_number_params=$act_reflection->getNumberOfParameters();
if($act_number_params>0){
foreach($act_reflection->getParameters() as $arg)
{
if($arg->name=='params' && count($params)>0){
$act_params[]=& $params;
}
elseif(isset($params[$arg->name])){
$act_params[]=$params[$arg->name];
unset($params[$arg->name]);
}
// else
// $act_params[]=[];
}
if($act_number_required_params>count($act_params))
return pushFailedMsg('服务接口参数不够.');
}
//bootstrap init
if (method_exists($this, '__init'))
call_user_func(array($this, '__init'));
//class init
if (method_exists($service_obj, '__init'))
call_user_func(array($service_obj, '__init'));
//before action
if (method_exists($service_obj, '__beforeAction'))
call_user_func(array($service_obj, '__beforeAction'));
//do action
$result = call_user_func_array([$service_obj,$act_name],$act_params);
// $result = $service_obj->$act_name($params);
if (method_exists($service_obj, '__afterAction'))
call_user_func(array($service_obj, '__afterAction'));
//after action
//class clean
if (method_exists($service_obj, '__clean'))
call_user_func(array($service_obj, '__clean'));
//bootstrap clean
if (method_exists($this, '__clean'))
call_user_func(array($this, '__clean'));
return $result;
}
}