|
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
|
|
|
* @package Onnov\JsonRpcServer |
|
24
|
|
|
*/ |
|
25
|
|
|
class RpcDefinitionGenerator |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @param GeneratedDefinition $definition |
|
29
|
|
|
* @return string |
|
30
|
|
|
* @throws JsonException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function convertToJson(GeneratedDefinition $definition): string |
|
33
|
|
|
{ |
|
34
|
|
|
return json_encode( |
|
35
|
|
|
$this->convertToArray($definition), |
|
36
|
|
|
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param GeneratedDefinition $definition |
|
42
|
|
|
* @return mixed[] |
|
43
|
|
|
*/ |
|
44
|
|
|
public function convertToArray(GeneratedDefinition $definition): array |
|
45
|
|
|
{ |
|
46
|
|
|
$def = ['$schema' => $definition->getSchema()] + $definition->toArray(); |
|
47
|
|
|
unset($def['schema']); |
|
48
|
|
|
$def['info'] = $definition->getInfo()->toArray(); |
|
49
|
|
|
$def['methods'] = $definition->getMethods(); |
|
50
|
|
|
|
|
51
|
|
|
foreach ($def['methods'] as &$method) { |
|
52
|
|
|
$method = $method->toArray(); |
|
53
|
|
|
if (is_array($method['errors'])) { |
|
54
|
|
|
$method['errors'] = array_values($method['errors']); |
|
55
|
|
|
/** @var RpcErrorDefinition $error */ |
|
56
|
|
|
foreach ($method['errors'] as &$error) { |
|
57
|
|
|
$error = $error->toArray(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
if (is_object($method['paramsObject'])) { |
|
61
|
|
|
$method['paramsObject'] = get_class($method['paramsObject']); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $def; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param RpcGeneralDefinition $definition |
|
70
|
|
|
* @param RpcFactoryInterface $factory |
|
71
|
|
|
* @param RpcAuthDefinition|null $auth |
|
72
|
|
|
* @return GeneratedDefinition |
|
73
|
|
|
*/ |
|
74
|
|
|
public function generateObject( |
|
75
|
|
|
RpcGeneralDefinition $definition, |
|
76
|
|
|
RpcFactoryInterface $factory, |
|
77
|
|
|
RpcAuthDefinition $auth = null |
|
78
|
|
|
): GeneratedDefinition { |
|
79
|
|
|
$def = new GeneratedDefinition(); |
|
80
|
|
|
$def |
|
81
|
|
|
->setJrgen($definition->getJrgen()) |
|
82
|
|
|
->setJsonrpc($definition->getJsonrpc()) |
|
83
|
|
|
->setInfo($definition->getInfo()) |
|
84
|
|
|
->setDefinitions($definition->getDefinitions()); |
|
85
|
|
|
|
|
86
|
|
|
$procedures = array_keys($factory::getSubscribedServices()); |
|
87
|
|
|
sort($procedures); |
|
88
|
|
|
$methods = []; |
|
89
|
|
|
foreach ($procedures as $procedure) { |
|
90
|
|
|
/** @var RpcProcedureInterface $procObj */ |
|
91
|
|
|
$procObj = $factory->get($procedure); |
|
92
|
|
|
$procDef = $procObj->getDefinition(); |
|
93
|
|
|
|
|
94
|
|
|
/** добавим ошибку авторизации */ |
|
95
|
|
|
if ($auth !== null && !in_array($procedure, $auth->getProcWithoutAuth(), true)) { |
|
96
|
|
|
$procDef |
|
97
|
|
|
->setErrors( |
|
98
|
|
|
[ |
|
99
|
|
|
$auth |
|
100
|
|
|
->getAuthError() |
|
101
|
|
|
->getCode() => $auth |
|
102
|
|
|
->getAuthError() |
|
103
|
|
|
] + ($procDef->getErrors() ?? []) |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$methods[$procedure] = $procDef; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$def->setMethods($methods); |
|
111
|
|
|
|
|
112
|
|
|
return $def; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|