-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonRpcDiscovery.php
More file actions
executable file
·82 lines (70 loc) · 2.33 KB
/
JsonRpcDiscovery.php
File metadata and controls
executable file
·82 lines (70 loc) · 2.33 KB
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
<?php
namespace Devim\RpcServerBundle;
use Devim\RpcServerBundle\Annotation\JsonRpcApi;
use Devim\RpcServerBundle\Annotation\JsonRpcMethod;
use Doctrine\Common\Annotations\Reader;
class JsonRpcDiscovery
{
/**
* @var Reader
*/
private $annotationReader;
/**
* @var array
*/
private $classes;
/**
* @var array
*/
private $cache = [];
/**
* JsonRpcDiscovery constructor.
* @param Reader $annotationReader
* @param array $classes
*/
public function __construct(Reader $annotationReader, array $classes)
{
$this->annotationReader = $annotationReader;
$this->classes = $classes;
}
/**
* @param string $requestBody
* @return array
*/
public function resolve(string $requestBody)
{
$requestBodyArr = json_decode($requestBody, true);
$rpcMethod = $requestBodyArr['method'];
if (0 == count($this->cache) && 0 < count($this->classes)) {
list($namespace, $action) = explode('.', $rpcMethod);
foreach ($this->classes as $class) {
$classReflection = new \ReflectionClass($class);
$rpcApiAnn = $this->annotationReader->getClassAnnotation($classReflection, JsonRpcApi::class);
/** @var JsonRpcApi $rpcApiAnn */
if (null == $rpcApiAnn) {
continue;
}
if ($rpcApiAnn->getNamespace() !== $namespace) {
continue;
}
// echo $class; die;
//
foreach ($classReflection->getMethods() as $method) {
$rpcMethodAnn = $this->annotationReader->getMethodAnnotation($method, JsonRpcMethod::class);
/** @var JsonRpcMethod $rpcMethodAnn */
if (null == $rpcMethodAnn) {
continue;
}
if (
(strlen($rpcMethodAnn->getName()) > 0 && $rpcMethodAnn->getName() === $action)
|| $method->getName() === $action
) {
$this->cache[$rpcMethod] = [$classReflection, $method];
}
}
}
}
// var_dump($this->cache);die;
return $this->cache[$rpcMethod];
}
}