1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\SymfonyJsonRpcHttpServerDoc\Creator; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
5
|
|
|
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodAwareInterface; |
6
|
|
|
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface; |
7
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\HttpServerDoc; |
8
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc; |
9
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\ServerDoc; |
10
|
|
|
use Yoanm\SymfonyJsonRpcHttpServerDoc\Event\MethodDocCreatedEvent; |
11
|
|
|
use Yoanm\SymfonyJsonRpcHttpServerDoc\Event\ServerDocCreatedEvent; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class HttpServerDocCreator |
15
|
|
|
*/ |
16
|
|
|
class HttpServerDocCreator implements JsonRpcMethodAwareInterface |
17
|
|
|
{ |
18
|
|
|
/** @var EventDispatcherInterface */ |
19
|
|
|
private $dispatcher; |
20
|
|
|
/** @var JsonRpcMethodInterface[] */ |
21
|
|
|
private $methodList = []; |
22
|
|
|
/** @var string|null */ |
23
|
|
|
private $jsonRpcEndpoint = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param EventDispatcherInterface $dispatcher |
27
|
|
|
* @param string|null $jsonRpcEndpoint |
28
|
|
|
*/ |
29
|
5 |
|
public function __construct(EventDispatcherInterface $dispatcher, string $jsonRpcEndpoint = null) |
30
|
|
|
{ |
31
|
5 |
|
$this->dispatcher = $dispatcher; |
32
|
5 |
|
$this->jsonRpcEndpoint = $jsonRpcEndpoint; |
33
|
5 |
|
} |
34
|
|
|
/** |
35
|
|
|
* @param string|null $host |
36
|
|
|
* |
37
|
|
|
* @return HttpServerDoc |
38
|
|
|
*/ |
39
|
5 |
|
public function create($host = null) : HttpServerDoc |
40
|
|
|
{ |
41
|
5 |
|
$serverDoc = new HttpServerDoc(); |
42
|
5 |
|
if (null !== $this->jsonRpcEndpoint) { |
43
|
5 |
|
$serverDoc->setEndpoint($this->jsonRpcEndpoint); |
44
|
|
|
} |
45
|
5 |
|
if (null !== $host) { |
46
|
1 |
|
$serverDoc->setHost($host); |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
$this->appendMethodsDoc($serverDoc); |
50
|
|
|
|
51
|
5 |
|
$event = new ServerDocCreatedEvent($serverDoc); |
52
|
5 |
|
$this->dispatcher->dispatch($event::EVENT_NAME, $event); |
|
|
|
|
53
|
|
|
|
54
|
5 |
|
return $serverDoc; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
2 |
|
public function addJsonRpcMethod(string $methodName, JsonRpcMethodInterface $method) : void |
61
|
|
|
{ |
62
|
2 |
|
$this->methodList[$methodName] = $method; |
63
|
2 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param ServerDoc $serverDoc |
67
|
|
|
*/ |
68
|
5 |
|
protected function appendMethodsDoc(ServerDoc $serverDoc) |
69
|
|
|
{ |
70
|
5 |
|
foreach ($this->methodList as $methodName => $method) { |
71
|
|
|
$event = ( |
72
|
2 |
|
new MethodDocCreatedEvent( |
73
|
2 |
|
new MethodDoc($methodName) |
74
|
|
|
) |
75
|
|
|
) |
76
|
2 |
|
->setMethod($method); |
77
|
|
|
|
78
|
2 |
|
$this->dispatcher->dispatch($event::EVENT_NAME, $event); |
|
|
|
|
79
|
|
|
|
80
|
2 |
|
$serverDoc->addMethod($event->getDoc()); |
81
|
|
|
} |
82
|
5 |
|
} |
83
|
|
|
} |
84
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.