1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Spiral\RoadRunner\Bootloaders; |
10
|
|
|
|
11
|
|
|
use Spiral\Boot\EnvironmentInterface; |
12
|
|
|
use Spiral\Boot\KernelInterface; |
13
|
|
|
use Spiral\Core\Bootloaders\Bootloader; |
14
|
|
|
use Spiral\Goridge\RPC; |
15
|
|
|
use Spiral\Goridge\SocketRelay; |
16
|
|
|
use Spiral\RoadRunner\Exceptions\RoadRunnerException; |
17
|
|
|
use Spiral\RoadRunner\RoadRunnerDispatcher; |
18
|
|
|
|
19
|
|
|
class RoadRunnerBootloader extends Bootloader |
20
|
|
|
{ |
21
|
|
|
const BOOT = true; |
22
|
|
|
const RPC_DEFAULT = 'tcp://127.0.0.1:6001'; |
23
|
|
|
const SINGLETONS = [ |
24
|
|
|
RoadRunnerDispatcher::class => RoadRunnerDispatcher::class, |
25
|
|
|
RPC::class => [self::class, 'rpc'] |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param KernelInterface $kernel |
30
|
|
|
* @param RoadRunnerDispatcher $rr |
31
|
|
|
*/ |
32
|
|
|
public function boot(KernelInterface $kernel, RoadRunnerDispatcher $rr) |
33
|
|
|
{ |
34
|
|
|
$kernel->addDispatcher($rr); |
35
|
|
|
|
36
|
|
|
if (function_exists('gc_collect_cycles')) { |
37
|
|
|
$rr->addFinalizer('gc_collect_cycles'); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param EnvironmentInterface $environment |
43
|
|
|
* @return RPC |
44
|
|
|
*/ |
45
|
|
|
protected function rpc(EnvironmentInterface $environment): RPC |
46
|
|
|
{ |
47
|
|
|
$conn = $environment->get('RR_RPC', static::RPC_DEFAULT); |
48
|
|
|
|
49
|
|
|
if (!preg_match('#^([a-z]+)://([^:]+):?(\d+)?$#i', $conn, $parts)) { |
50
|
|
|
throw new RoadRunnerException( |
51
|
|
|
"Unable to create RPC connection, invalid DSN given `{$conn}`." |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!in_array($parts[1], ['tcp', 'unix'])) { |
56
|
|
|
throw new RoadRunnerException( |
57
|
|
|
"Unable to create RPC connection, invalid DSN given `{$conn}`." |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($parts[1] == 'unix') { |
62
|
|
|
$relay = new SocketRelay($parts[2], null, SocketRelay::SOCK_UNIX); |
63
|
|
|
} else { |
64
|
|
|
$relay = new SocketRelay($parts[2], $parts[3], SocketRelay::SOCK_TCP); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return new RPC($relay); |
68
|
|
|
} |
69
|
|
|
} |