1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Runner\RoadRunner; |
6
|
|
|
|
7
|
|
|
use Spiral\RoadRunner\GRPC\Invoker; |
8
|
|
|
use Spiral\RoadRunner\GRPC\InvokerInterface; |
9
|
|
|
use Spiral\RoadRunner\GRPC\Server; |
10
|
|
|
use Spiral\RoadRunner\GRPC\ServiceInterface; |
11
|
|
|
use Spiral\RoadRunner\Worker; |
12
|
|
|
use Yiisoft\Yii\Runner\ApplicationRunner; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* `RoadRunnerGrpcApplicationRunner` runs the Yii gRPC application using RoadRunner. |
16
|
|
|
*/ |
17
|
|
|
final class RoadRunnerGrpcApplicationRunner extends ApplicationRunner |
18
|
|
|
{ |
19
|
|
|
private ?InvokerInterface $invoker = null; |
20
|
|
|
private array $services = []; |
21
|
|
|
private ?Worker $worker = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $rootPath The absolute path to the project root. |
25
|
|
|
* @param bool $debug Whether the debug mode is enabled. |
26
|
|
|
* @param bool $checkEvents Whether to check events' configuration. |
27
|
|
|
* @param string|null $environment The environment name. |
28
|
|
|
* @param string $bootstrapGroup The bootstrap configuration group name. |
29
|
|
|
* @param string $eventsGroup The events' configuration group name. |
30
|
|
|
* @param string $diGroup The container definitions' configuration group name. |
31
|
|
|
* @param string $diProvidersGroup The container providers' configuration group name. |
32
|
|
|
* @param string $diDelegatesGroup The container delegates' configuration group name. |
33
|
|
|
* @param string $diTagsGroup The container tags' configuration group name. |
34
|
|
|
* @param string $paramsGroup The configuration parameters group name. |
35
|
|
|
* @param array $nestedParamsGroups Configuration group names that are included into configuration parameters group. |
36
|
|
|
* This is needed for recursive merging of parameters. |
37
|
|
|
* @param array $nestedEventsGroups Configuration group names that are included into events' configuration group. |
38
|
|
|
* This is needed for reverse and recursive merge of events' configurations. |
39
|
|
|
* |
40
|
|
|
* @psalm-param list<string> $nestedParamsGroups |
41
|
|
|
* @psalm-param list<string> $nestedEventsGroups |
42
|
|
|
*/ |
43
|
2 |
|
public function __construct( |
44
|
|
|
string $rootPath, |
45
|
|
|
bool $debug = false, |
46
|
|
|
bool $checkEvents = false, |
47
|
|
|
?string $environment = null, |
48
|
|
|
string $bootstrapGroup = 'bootstrap-web', |
49
|
|
|
string $eventsGroup = 'events-web', |
50
|
|
|
string $diGroup = 'di-web', |
51
|
|
|
string $diProvidersGroup = 'di-providers-web', |
52
|
|
|
string $diDelegatesGroup = 'di-delegates-web', |
53
|
|
|
string $diTagsGroup = 'di-tags-web', |
54
|
|
|
string $paramsGroup = 'params-web', |
55
|
|
|
array $nestedParamsGroups = ['params'], |
56
|
|
|
array $nestedEventsGroups = ['events'], |
57
|
|
|
) { |
58
|
2 |
|
parent::__construct( |
59
|
2 |
|
$rootPath, |
60
|
2 |
|
$debug, |
61
|
2 |
|
$checkEvents, |
62
|
2 |
|
$environment, |
63
|
2 |
|
$bootstrapGroup, |
64
|
2 |
|
$eventsGroup, |
65
|
2 |
|
$diGroup, |
66
|
2 |
|
$diProvidersGroup, |
67
|
2 |
|
$diDelegatesGroup, |
68
|
2 |
|
$diTagsGroup, |
69
|
2 |
|
$paramsGroup, |
70
|
2 |
|
$nestedParamsGroups, |
71
|
2 |
|
$nestedEventsGroups, |
72
|
2 |
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public function run(): void |
76
|
|
|
{ |
77
|
1 |
|
$server = new Server($this->getInvoker(), ['debug' => $this->debug]); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var class-string<ServiceInterface> $interface |
81
|
|
|
* @var ServiceInterface $service |
82
|
|
|
*/ |
83
|
1 |
|
foreach ($this->getServices() as $interface => $service) { |
84
|
1 |
|
$server->registerService($interface, new $service()); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
$server->serve($this->getWorker(), static function () { |
88
|
1 |
|
gc_collect_cycles(); |
89
|
1 |
|
}); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns a new instance with the specified gRPC worker instance |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
1 |
|
public function withWorker(Worker $worker): self |
98
|
|
|
{ |
99
|
1 |
|
$instance = clone $this; |
100
|
1 |
|
$instance->worker = $worker; |
101
|
|
|
|
102
|
1 |
|
return $instance; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Transmitted services for registration gRPC server |
107
|
|
|
* |
108
|
|
|
* @param array $services Services array (key-value pairs) |
109
|
|
|
* ```php |
110
|
|
|
* [ |
111
|
|
|
* ServiceInterface::class => Service::class |
112
|
|
|
* ] |
113
|
|
|
* ``` |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
2 |
|
public function setServices(array $services): self |
118
|
|
|
{ |
119
|
2 |
|
$this->services = $services; |
120
|
|
|
|
121
|
2 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
public function getServices(): array |
125
|
|
|
{ |
126
|
2 |
|
return $this->services; |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
public function getWorker(): Worker |
130
|
|
|
{ |
131
|
1 |
|
return $this->worker ?? Worker::create(); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
public function getInvoker(): InvokerInterface |
135
|
|
|
{ |
136
|
1 |
|
return $this->invoker ?? new Invoker(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|