|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Bootloader\Http; |
|
13
|
|
|
|
|
14
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
|
15
|
|
|
use Spiral\Boot\EnvironmentInterface; |
|
16
|
|
|
use Spiral\Bootloader\Broadcast\BroadcastBootloader; |
|
17
|
|
|
use Spiral\Broadcast\Middleware\WebsocketsMiddleware; |
|
18
|
|
|
use Spiral\Config\ConfiguratorInterface; |
|
19
|
|
|
use Spiral\Config\Patch\Append; |
|
20
|
|
|
use Spiral\Config\Patch\Set; |
|
21
|
|
|
use Spiral\Core\Container\SingletonInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Authorizes websocket and server connections using interceptor middleware. |
|
25
|
|
|
*/ |
|
26
|
|
|
final class WebsocketsBootloader extends Bootloader implements SingletonInterface |
|
27
|
|
|
{ |
|
28
|
|
|
protected const DEPENDENCIES = [ |
|
29
|
|
|
HttpBootloader::class, |
|
30
|
|
|
BroadcastBootloader::class |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** @var ConfiguratorInterface */ |
|
34
|
|
|
private $config; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param ConfiguratorInterface $config |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(ConfiguratorInterface $config) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->config = $config; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param HttpBootloader $http |
|
46
|
|
|
* @param EnvironmentInterface $env |
|
47
|
|
|
*/ |
|
48
|
|
|
public function boot(HttpBootloader $http, EnvironmentInterface $env): void |
|
49
|
|
|
{ |
|
50
|
|
|
$this->config->setDefaults('websockets', [ |
|
51
|
|
|
'path' => $env->get('RR_BROADCAST_PATH', null), |
|
52
|
|
|
'authorizeServer' => null, |
|
53
|
|
|
'authorizeTopics' => [] |
|
54
|
|
|
]); |
|
55
|
|
|
|
|
56
|
|
|
if ($env->get('RR_BROADCAST_PATH', null) !== null) { |
|
57
|
|
|
$http->addMiddleware(WebsocketsMiddleware::class); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param callable|null $callback |
|
63
|
|
|
*/ |
|
64
|
|
|
public function authorizeServer(?callable $callback): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->config->modify('websockets', new Set('authorizeServer', $callback)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $topic |
|
71
|
|
|
* @param callable $callback |
|
72
|
|
|
*/ |
|
73
|
|
|
public function authorizeTopic(string $topic, callable $callback): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->config->modify('websockets', new Append('authorizeTopics', $topic, $callback)); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|