1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* Project: json_rpc_server |
6
|
|
|
* User: sv |
7
|
|
|
* Date: 25.06.19 |
8
|
|
|
* Time: 7:54 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Onnov\JsonRpcServer\Service; |
14
|
|
|
|
15
|
|
|
use JsonMapper; |
16
|
|
|
use JsonMapper_Exception; |
17
|
|
|
use Onnov\JsonRpcServer\ApiMethodInterface; |
18
|
|
|
use Onnov\JsonRpcServer\Exception\InternalErrorException; |
19
|
|
|
use Onnov\JsonRpcServer\Exception\MethodNotFoundException; |
20
|
|
|
use Onnov\JsonRpcServer\Exception\ParseErrorException; |
21
|
|
|
use Onnov\JsonRpcServer\Model\RpcModel; |
22
|
|
|
use Onnov\JsonRpcServer\Model\RpcRequest; |
23
|
|
|
use Onnov\JsonRpcServer\Model\RunModel; |
24
|
|
|
use Onnov\JsonRpcServer\Traits\JsonHelperTrait; |
25
|
|
|
use Onnov\JsonRpcServer\Validator\JsonRpcSchema; |
26
|
|
|
use Onnov\JsonRpcServer\Validator\JsonSchemaValidator; |
27
|
|
|
|
28
|
|
|
class ApiExecService |
29
|
|
|
{ |
30
|
1 |
|
use JsonHelperTrait; |
31
|
|
|
|
32
|
|
|
/** @var JsonSchemaValidator */ |
33
|
|
|
protected $validator; |
34
|
|
|
|
35
|
|
|
/** @var JsonRpcSchema */ |
36
|
|
|
protected $rpcSchema; |
37
|
|
|
|
38
|
|
|
/** @var JsonMapper */ |
39
|
|
|
private $mapper; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* ApiExecService constructor. |
43
|
|
|
* |
44
|
|
|
* @param JsonSchemaValidator $validator |
45
|
|
|
* @param JsonRpcSchema $rpcSchema |
46
|
|
|
* @param JsonMapper $mapper |
47
|
|
|
*/ |
48
|
6 |
|
public function __construct( |
49
|
|
|
JsonSchemaValidator $validator, |
50
|
|
|
JsonRpcSchema $rpcSchema, |
51
|
|
|
JsonMapper $mapper |
52
|
|
|
) { |
53
|
6 |
|
$this->validator = $validator; |
54
|
6 |
|
$this->rpcSchema = $rpcSchema; |
55
|
6 |
|
$this->mapper = $mapper; |
56
|
6 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param RunModel $model |
60
|
|
|
* @param RpcModel $rpc |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
3 |
|
public function exe( |
65
|
|
|
RunModel $model, |
66
|
|
|
RpcModel $rpc |
67
|
|
|
) { |
68
|
3 |
|
$factory = $model->getApiFactory(); |
69
|
3 |
|
$method = $rpc->getMethod(); |
70
|
|
|
/** Проверим существование метода */ |
71
|
3 |
|
if ($factory->has($method) === false) { |
72
|
1 |
|
throw new MethodNotFoundException( |
73
|
1 |
|
'Method "' . $method . '" not found' |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** Создаем экземпляр класса |
78
|
|
|
* |
79
|
|
|
* @var ApiMethodInterface $class |
80
|
|
|
*/ |
81
|
2 |
|
$class = $factory->get($method); |
82
|
|
|
|
83
|
|
|
/** Проверим соответствие интерфейсу */ |
84
|
2 |
|
$this->checkInterface($class, $method); |
85
|
|
|
|
86
|
|
|
/** Валидируем парамертры ЗАПРОСА */ |
87
|
2 |
|
if ($class->requestSchema() !== null) { |
88
|
|
|
$this->getValidator()->validate( |
89
|
|
|
$class->requestSchema(), |
90
|
|
|
$rpc->getParams(), |
91
|
|
|
'requestParams' |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
$paramsObject = null; |
96
|
2 |
|
if (method_exists($class, 'customParamsObject') && $class->customParamsObject() !== null) { |
97
|
|
|
try { |
98
|
|
|
$paramsObject = $this->getMapper()->map($rpc->getParams(), $class->customParamsObject()); |
99
|
|
|
} catch (JsonMapper_Exception $e) { |
100
|
|
|
throw new ParseErrorException('', 0, $e->getPrevious()); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** засетим в метод RpcRequest*/ |
105
|
2 |
|
if (method_exists($class, 'setRpcRequest')) { |
106
|
2 |
|
$class->setRpcRequest(new RpcRequest($rpc, $paramsObject)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** Выполним метод */ |
110
|
2 |
|
$res = $class->execute()->getResult(); |
111
|
|
|
|
112
|
2 |
|
if ($model->isResponseCheck() && $class->responseSchema() !== null) { |
113
|
|
|
/** Валидируем парамертры ОТВЕТА */ |
114
|
|
|
$this->getValidator()->validate( |
115
|
|
|
$class->responseSchema(), |
116
|
|
|
$res, |
117
|
|
|
'responseParams' |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
return $res; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param ApiMethodInterface $class |
126
|
|
|
* @param string $method |
127
|
|
|
*/ |
128
|
2 |
|
private function checkInterface(ApiMethodInterface $class, string $method): void |
129
|
|
|
{ |
130
|
|
|
// ??? |
131
|
2 |
|
$interfaces = (array)class_implements($class); |
132
|
|
|
if ( |
133
|
2 |
|
(bool)$interfaces === false |
134
|
2 |
|
|| in_array(ApiMethodInterface::class, $interfaces, true) === false |
135
|
|
|
) { |
136
|
|
|
throw new InternalErrorException( |
137
|
|
|
'Method "' . $method . '" does not match Interface' |
138
|
|
|
); |
139
|
|
|
} |
140
|
2 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return JsonSchemaValidator |
144
|
|
|
*/ |
145
|
|
|
public function getValidator(): JsonSchemaValidator |
146
|
|
|
{ |
147
|
|
|
return $this->validator; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return JsonRpcSchema |
152
|
|
|
*/ |
153
|
|
|
public function getRpcSchema(): JsonRpcSchema |
154
|
|
|
{ |
155
|
|
|
return $this->rpcSchema; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return JsonMapper |
160
|
|
|
*/ |
161
|
|
|
public function getMapper(): JsonMapper |
162
|
|
|
{ |
163
|
|
|
return $this->mapper; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|