|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\SymfonyJsonRpcHttpServerDoc\Creator; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Contracts\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
|
8 |
|
public function __construct(EventDispatcherInterface $dispatcher, string $jsonRpcEndpoint = null) |
|
30
|
|
|
{ |
|
31
|
8 |
|
$this->dispatcher = $dispatcher; |
|
32
|
8 |
|
$this->jsonRpcEndpoint = $jsonRpcEndpoint; |
|
33
|
|
|
} |
|
34
|
|
|
/** |
|
35
|
|
|
* @param string|null $host |
|
36
|
|
|
* |
|
37
|
|
|
* @return HttpServerDoc |
|
38
|
|
|
*/ |
|
39
|
8 |
|
public function create($host = null) : HttpServerDoc |
|
40
|
|
|
{ |
|
41
|
8 |
|
$serverDoc = new HttpServerDoc(); |
|
42
|
8 |
|
if (null !== $this->jsonRpcEndpoint) { |
|
43
|
8 |
|
$serverDoc->setEndpoint($this->jsonRpcEndpoint); |
|
44
|
|
|
} |
|
45
|
8 |
|
if (null !== $host) { |
|
46
|
4 |
|
$serverDoc->setHost($host); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
8 |
|
$this->appendMethodsDoc($serverDoc); |
|
50
|
|
|
|
|
51
|
8 |
|
$event = new ServerDocCreatedEvent($serverDoc); |
|
52
|
8 |
|
$this->dispatcher->dispatch($event, $event::EVENT_NAME); |
|
53
|
|
|
|
|
54
|
8 |
|
return $serverDoc; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
5 |
|
public function addJsonRpcMethod(string $methodName, JsonRpcMethodInterface $method) : void |
|
61
|
|
|
{ |
|
62
|
5 |
|
$this->methodList[$methodName] = $method; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param ServerDoc $serverDoc |
|
67
|
|
|
*/ |
|
68
|
8 |
|
protected function appendMethodsDoc(ServerDoc $serverDoc) |
|
69
|
|
|
{ |
|
70
|
8 |
|
foreach ($this->methodList as $methodName => $method) { |
|
71
|
5 |
|
$event = ( |
|
72
|
5 |
|
new MethodDocCreatedEvent( |
|
73
|
5 |
|
new MethodDoc($methodName) |
|
74
|
5 |
|
) |
|
75
|
5 |
|
) |
|
76
|
5 |
|
->setMethod($method); |
|
77
|
|
|
|
|
78
|
5 |
|
$this->dispatcher->dispatch($event, $event::EVENT_NAME); |
|
79
|
|
|
|
|
80
|
5 |
|
$serverDoc->addMethod($event->getDoc()); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|