This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
ServiceWrapper.php
143 lines (124 loc) · 3.48 KB
/
ServiceWrapper.php
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
<?php
/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/
namespace Spiral\GRPC;
use Spiral\GRPC\Exception\InvokeException;
use Spiral\GRPC\Exception\NotFoundException;
use Spiral\GRPC\Exception\ServiceException;
/**
* Wraps handlers methods.
*/
final class ServiceWrapper
{
/** @var string */
private $name;
/** @var ServiceInterface */
private $service;
/** @var InvokerInterface */
private $invoker;
/** @var Method[] */
private $methods;
/**
* @param InvokerInterface $invoker
* @param string $interface Service interface name.
* @param ServiceInterface $service
*
* @throws ServiceException
*/
public function __construct(
InvokerInterface $invoker,
string $interface,
ServiceInterface $service
) {
$this->invoker = $invoker;
$this->configure($interface, $service);
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return ServiceInterface
*/
public function getService(): ServiceInterface
{
return $this->service;
}
/**
* @return array
*/
public function getMethods(): array
{
return array_values($this->methods);
}
/**
* @param string $method
* @param ContextInterface $context
* @param string $input
* @return string
*
* @throws NotFoundException
* @throws InvokeException
*/
public function invoke(string $method, ContextInterface $context, string $input): string
{
if (!isset($this->methods[$method])) {
throw new NotFoundException("Method `{$method}` not found in service `{$this->name}`.");
}
return $this->invoker->invoke($this->service, $this->methods[$method], $context, $input);
}
/**
* Configure service name and methods.
*
* @param string $interface
* @param ServiceInterface $service
*
* @throws ServiceException
*/
protected function configure(string $interface, ServiceInterface $service)
{
try {
$r = new \ReflectionClass($interface);
if (!$r->hasConstant('NAME')) {
throw new ServiceException(
"Invalid service interface `{$interface}`, constant `NAME` not found."
);
}
$this->name = $r->getConstant('NAME');
} catch (\ReflectionException $e) {
throw new ServiceException(
"Invalid service interface `{$interface}`.",
StatusCode::INTERNAL,
$e
);
}
if (!$service instanceof $interface) {
throw new ServiceException("Service handler does not implement `{$interface}`.");
}
$this->service = $service;
// list of all available methods and their object types
$this->methods = $this->fetchMethods($service);
}
/**
* @param ServiceInterface $service
* @return array
*/
protected function fetchMethods(ServiceInterface $service): array
{
$reflection = new \ReflectionObject($service);
$methods = [];
foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
if (Method::match($method)) {
$methods[$method->getName()] = Method::parse($method);
}
}
return $methods;
}
}