1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Runner\RoadRunner; |
6
|
|
|
|
7
|
|
|
use Spiral\RoadRunner\GRPC\InvokerInterface; |
8
|
|
|
use Spiral\RoadRunner\GRPC\Server; |
9
|
|
|
use Spiral\RoadRunner\GRPC\ServiceInterface; |
10
|
|
|
use Spiral\RoadRunner\Worker; |
11
|
|
|
use Yiisoft\Yii\Runner\ApplicationRunner; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* `RoadRunnerGrpcApplicationRunner` runs the Yii gRPC application using RoadRunner. |
15
|
|
|
*/ |
16
|
|
|
final class RoadRunnerGrpcApplicationRunner extends ApplicationRunner |
17
|
|
|
{ |
18
|
|
|
public ?InvokerInterface $invoker = null; |
19
|
|
|
public bool $workerInterceptSideEffects = true; |
20
|
|
|
public array $services = []; |
21
|
|
|
private ?Worker $worker; |
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
|
|
|
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
|
|
|
parent::__construct( |
59
|
|
|
$rootPath, |
60
|
|
|
$debug, |
61
|
|
|
$checkEvents, |
62
|
|
|
$environment, |
63
|
|
|
$bootstrapGroup, |
64
|
|
|
$eventsGroup, |
65
|
|
|
$diGroup, |
66
|
|
|
$diProvidersGroup, |
67
|
|
|
$diDelegatesGroup, |
68
|
|
|
$diTagsGroup, |
69
|
|
|
$paramsGroup, |
70
|
|
|
$nestedParamsGroups, |
71
|
|
|
$nestedEventsGroups, |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$this->worker = Worker::create($this->workerInterceptSideEffects); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function run(): void |
78
|
|
|
{ |
79
|
|
|
$server = new Server($this->invoker, ['debug' => $this->debug]); |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var class-string<ServiceInterface> $interface |
83
|
|
|
* @var ServiceInterface $service |
84
|
|
|
*/ |
85
|
|
|
foreach ($this->services as $interface => $service) { |
86
|
|
|
$server->registerService($interface, new $service()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$server->serve($this->worker, static function () { |
90
|
|
|
gc_collect_cycles(); |
91
|
|
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function withWorker(Worker $worker): self |
95
|
|
|
{ |
96
|
|
|
$instance = clone $this; |
97
|
|
|
$instance->worker = $worker; |
98
|
|
|
return $instance; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|