1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* Project: json-rpc-server |
6
|
|
|
* User: sv |
7
|
|
|
* Date: 07.02.2021 |
8
|
|
|
* Time: 11:08 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Onnov\JsonRpcServer; |
14
|
|
|
|
15
|
|
|
use JsonException; |
16
|
|
|
use Onnov\JsonRpcServer\Definition\GeneratedDefinition; |
17
|
|
|
use Onnov\JsonRpcServer\Definition\RpcAuthDefinition; |
18
|
|
|
use Onnov\JsonRpcServer\Definition\RpcErrorDefinition; |
19
|
|
|
use Onnov\JsonRpcServer\Definition\RpcGeneralDefinition; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class RpcDefinitionGenerator |
23
|
|
|
* |
24
|
|
|
* @package Onnov\JsonRpcServer |
25
|
|
|
*/ |
26
|
|
|
class RpcDefinitionGenerator |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @param GeneratedDefinition $definition |
30
|
|
|
* @return string |
31
|
|
|
* @throws JsonException |
32
|
|
|
*/ |
33
|
|
|
public function convertToJson(GeneratedDefinition $definition): string |
34
|
|
|
{ |
35
|
|
|
return json_encode( |
36
|
|
|
$this->convertToArray($definition), |
37
|
|
|
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param GeneratedDefinition $definition |
43
|
|
|
* @return mixed[] |
44
|
|
|
*/ |
45
|
|
|
public function convertToArray(GeneratedDefinition $definition): array |
46
|
|
|
{ |
47
|
|
|
$def = ['$schema' => $definition->getSchema()] + $definition->toArray(); |
48
|
|
|
unset($def['schema']); |
49
|
|
|
$def['info'] = $definition->getInfo()->toArray(); |
50
|
|
|
$def['methods'] = $definition->getMethods(); |
51
|
|
|
|
52
|
|
|
foreach ($def['methods'] as &$method) { |
53
|
|
|
$method = $method->toArray(); |
54
|
|
|
if (is_array($method['errors'])) { |
55
|
|
|
$method['errors'] = array_values($method['errors']); |
56
|
|
|
/** |
57
|
|
|
* @var RpcErrorDefinition $error |
58
|
|
|
*/ |
59
|
|
|
foreach ($method['errors'] as &$error) { |
60
|
|
|
$error = $error->toArray(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
if (is_object($method['paramsObject'])) { |
64
|
|
|
$method['paramsObject'] = get_class($method['paramsObject']); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $def; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param RpcGeneralDefinition $definition |
73
|
|
|
* @param RpcFactoryInterface $factory |
74
|
|
|
* @param RpcAuthDefinition|null $auth |
75
|
|
|
* @return GeneratedDefinition |
76
|
|
|
*/ |
77
|
|
|
public function generateObject( |
78
|
|
|
RpcGeneralDefinition $definition, |
79
|
|
|
RpcFactoryInterface $factory, |
80
|
|
|
RpcAuthDefinition $auth = null |
81
|
|
|
): GeneratedDefinition { |
82
|
|
|
$def = new GeneratedDefinition(); |
83
|
|
|
$def |
84
|
|
|
->setJrgen($definition->getJrgen()) |
85
|
|
|
->setJsonrpc($definition->getJsonrpc()) |
86
|
|
|
->setInfo($definition->getInfo()) |
87
|
|
|
->setDefinitions($definition->getDefinitions()); |
88
|
|
|
|
89
|
|
|
$procedures = array_keys($factory::getSubscribedServices()); |
90
|
|
|
sort($procedures); |
91
|
|
|
$methods = []; |
92
|
|
|
foreach ($procedures as $procedure) { |
93
|
|
|
/** |
94
|
|
|
* @var RpcProcedureInterface $procObj |
95
|
|
|
*/ |
96
|
|
|
$procObj = $factory->get($procedure); |
97
|
|
|
$procDef = $procObj->getDefinition(); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* добавим ошибку авторизации |
101
|
|
|
*/ |
102
|
|
|
if ($auth !== null && !in_array($procedure, $auth->getProcWithoutAuth(), true)) { |
103
|
|
|
$procDef |
104
|
|
|
->setErrors( |
105
|
|
|
[ |
106
|
|
|
$auth |
107
|
|
|
->getAuthError() |
108
|
|
|
->getCode() => $auth |
109
|
|
|
->getAuthError() |
110
|
|
|
] + ($procDef->getErrors() ?? []) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$methods[$procedure] = $procDef; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$def->setMethods($methods); |
118
|
|
|
|
119
|
|
|
return $def; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|