summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/icinga-cli-daemon/src/IcingaCliRpc.php
blob: 473d4ed7d20f14730d27bf9b10ec55b70917bfeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php

namespace gipfl\IcingaCliDaemon;

use Exception;
use gipfl\Protocol\JsonRpc\Connection;
use gipfl\Protocol\NetString\StreamWrapper;
use React\ChildProcess\Process;

class IcingaCliRpc extends IcingaCli
{
    /** @var IcingaCliRunner */
    protected $runner;

    /** @var Connection|null */
    protected $rpc;

    protected $arguments = [];

    protected function init()
    {
        $this->on('start', function (Process $process) {
            $netString = new StreamWrapper(
                $process->stdout,
                $process->stdin
            );
            $netString->on('error', function (Exception $e) {
                $this->emit('error', [$e]);
            });
            $this->rpc()->handle($netString);
        });
    }

    /**
     * @return Connection
     */
    public function rpc()
    {
        if ($this->rpc === null) {
            $this->rpc = new Connection();
        }

        return $this->rpc;
    }
}