Service.php
4.2 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace AukeySwrpc;
use Psr\Log\LoggerInterface;
use ReflectionClass;
use ReflectionMethod;
use AukeySwrpc\Request\Request;
/**
* Class Service
*
* @package Swrpc
* @author pengjch 202439 11:39:41
*/
class Service
{
private array $services = [];
protected array $filers
= [
'factory',
'initTracer',
'setModule',
'setTracerUrl',
'setParams',
'setTracerContext',
'getTracerContext'
];
/** @var LoggerInterface $logger */
private $logger;
public function __construct($logger)
{
$this->logger = $logger;
}
/**
* 注册服务实例
*
* @param $obj
* @param $prefix
* @return bool
* @author pengjch 202438 13:43:21
*/
public function addInstance($obj, $prefix = ''): bool
{
if (is_string($obj)) {
$obj = new $obj();
}
if (!is_object($obj)) {
$this->logger->error('Service is not an object.', ['service' => $obj]);
return false;
}
if (!($obj instanceof LogicService)) {
$this->logger->error('The Service does not inherit LogicService', ['service' => get_class($obj)]);
return false;
}
$className = get_class($obj);
$methods = get_class_methods($obj);
foreach ($methods as $method) {
if (in_array($method, $this->filers)) {
continue;
}
if (strlen($prefix) > 0) {
$key = $prefix . '_' . $className . '_' . $method;
} else {
$key = $className . '_' . $method;
}
$this->services[$key] = $className;
$this->logger->info(sprintf('import %s => %s.', $key, $className));
}
return true;
}
/**
* 获取服务
*
* @param $key
* @return mixed|null
* @author pengjch 202438 13:43:17
*/
public function getService($key)
{
return $this->services[$key] ?? null;
}
/**
* 获取所有服务
* getServices
*
* @return array
* @author pengjch 202438 15:23:58
*/
public function getServices(): array
{
return $this->services;
}
/**
* count
*
* @return int
* @author pengjch 202439 12:56:46
*/
public function count(): int
{
return count($this->services);
}
/**
* @param $key
* @return bool
* @author pengjch 202438 14:32:50
*/
public function isExist($key): bool
{
return isset($this->services[$key]);
}
/**
* 调用服务
*
* @param Request $request
* @return Response
* @throws \ReflectionException
* @author pengjch 202439 10:17:59
*/
public function call(Request $request): Response
{
if ($err = $request->getError()) {
return Response::error($err);
}
$service = $this->getService($request->getMethod());
if (!$service) {
$this->logger->debug('service is not exist.', ['method' => $request->getMethod()]);
return Response::error('service is not exist.');
}
$methodArr = explode('_', $request->getMethod());
$methodName = array_pop($methodArr);
$reflect = new ReflectionClass($service);
$instance = $reflect->newInstanceArgs();
if (!method_exists($instance, $methodName)) {
$this->logger->debug('method is not exist.', ['method' => $request->getMethod()]);
return Response::error(sprintf('%s method[%s] is not exist.', $service, $methodName));
}
$ctx = $request->getTraceContext();
if ($ctx && method_exists($instance, 'setTracerContext')) {
$instance->setTracerUrl($ctx->getReporterUrl())->setTracerContext($ctx);
}
try {
$methodObj = new ReflectionMethod($reflect->getName(), $methodName);
$result = $methodObj->invokeArgs($instance, $request->getParams());
} catch (\Throwable $e) {
return Response::error($e->getMessage());
}
return Response::success([
'result' => $result
]);
}
}