Test Setup Failed
Pull Request — master (#67)
by Moiseenko
21:53
created

RoadRunnerGrpcApplicationRunner::withWorker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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