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
/
Copy pathServiceWrapper.php
146 lines (120 loc) · 3.82 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
144
145
146
<?php
/**
* This file is part of RoadRunner GRPC package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
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 class-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|null $input
* @return string
* @throws NotFoundException
* @throws InvokeException
*/
public function invoke(string $method, ContextInterface $context, ?string $input): string
{
if (! isset($this->methods[$method])) {
throw NotFoundException::create("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 class-string $interface
* @param ServiceInterface $service
* @throws ServiceException
*/
protected function configure(string $interface, ServiceInterface $service): void
{
try {
$reflection = new \ReflectionClass($interface);
if (! $reflection->hasConstant('NAME')) {
$message = "Invalid service interface `{$interface}`, constant `NAME` not found.";
throw ServiceException::create($message);
}
$name = $reflection->getConstant('NAME');
if (! \is_string($name)) {
$message = "Constant `NAME` of service interface `{$interface}` must be a type of string";
throw ServiceException::create($message);
}
$this->name = $name;
} catch (\ReflectionException $e) {
$message = "Invalid service interface `{$interface}`.";
throw ServiceException::create($message, StatusCode::INTERNAL, $e);
}
if (! $service instanceof $interface) {
throw ServiceException::create("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<string, Method>
*/
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;
}
}