Completed
Push — master ( fc5faa...c92243 )
by Kirill
34s queued 18s
created

LegacyRoadRunnerBootloader::worker()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 27
rs 9.2222
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Bootloader\Server;
13
14
use Spiral\Boot\Bootloader\Bootloader;
15
use Spiral\Boot\EnvironmentInterface;
16
use Spiral\Boot\Exception\BootException;
17
use Spiral\Core\Container;
18
use Spiral\Goridge\RPC;
19
use Spiral\Goridge\SocketRelay;
20
use Spiral\Goridge\StreamRelay;
21
use Spiral\RoadRunner\Metrics;
22
use Spiral\RoadRunner\MetricsInterface;
23
use Spiral\RoadRunner\Worker;
24
25
class LegacyRoadRunnerBootloader extends Bootloader
26
{
27
    /**
28
     * @var string
29
     */
30
    private const RPC_DEFAULT = 'tcp://127.0.0.1:6001';
31
32
    /**
33
     * @var string
34
     */
35
    private const WORKER_DEFAULT = 'pipes';
36
37
    /**
38
     * @param Container $container
39
     */
40
    public function boot(Container $container)
41
    {
42
        $container->bindSingleton(RPC::class, function (EnvironmentInterface $env) {
43
            return $this->rpc($env);
44
        });
45
46
        $container->bindSingleton(Worker::class, function (EnvironmentInterface $env) {
47
            return $this->worker($env);
48
        });
49
50
        $container->bindSingleton(MetricsInterface::class, function (RPC $rpc) {
51
            return new Metrics($rpc);
52
        });
53
    }
54
55
    /**
56
     * @param EnvironmentInterface $env
57
     * @return RPC
58
     */
59
    protected function rpc(EnvironmentInterface $env): RPC
60
    {
61
        $conn = $env->get('RR_RPC', self::RPC_DEFAULT);
62
63
        if (!preg_match('#^([a-z]+)://([^:]+):?(\d+)?$#i', $conn, $parts)) {
64
            throw new BootException(
65
                "Unable to configure RPC connection, invalid DSN given `{$conn}`."
66
            );
67
        }
68
69
        if (!in_array($parts[1], ['tcp', 'unix'])) {
70
            throw new BootException(
71
                "Unable to configure RPC connection, invalid DSN given `{$conn}`."
72
            );
73
        }
74
75
        if ($parts[1] == 'unix') {
76
            $relay = new SocketRelay($parts[2], null, SocketRelay::SOCK_UNIX);
77
        } else {
78
            $relay = new SocketRelay($parts[2], (int)$parts[3], SocketRelay::SOCK_TCP);
79
        }
80
81
        return new RPC($relay);
82
    }
83
84
    /**
85
     * @param EnvironmentInterface $env
86
     * @return Worker
87
     */
88
    protected function worker(EnvironmentInterface $env): Worker
89
    {
90
        $conn = $env->get('RR_RELAY', self::WORKER_DEFAULT);
91
92
        if ($conn === 'pipes' || empty($conn)) {
93
            return new Worker(new StreamRelay(STDIN, STDOUT));
94
        }
95
96
        if (!preg_match('#^([a-z]+)://([^:]+):?(\d+)?$#i', $conn, $parts)) {
97
            throw new BootException(
98
                "Unable to configure Worker connection, invalid DSN given `{$conn}`."
99
            );
100
        }
101
102
        if (!in_array($parts[1], ['tcp', 'unix'])) {
103
            throw new BootException(
104
                "Unable to configure Worker connection, invalid DSN given `{$conn}`."
105
            );
106
        }
107
108
        if ($parts[1] == 'unix') {
109
            $relay = new SocketRelay($parts[2], null, SocketRelay::SOCK_UNIX);
110
        } else {
111
            $relay = new SocketRelay($parts[2], (int)$parts[3], SocketRelay::SOCK_TCP);
112
        }
113
114
        return new Worker($relay);
115
    }
116
}
117