1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\MercurePHP\Hub; |
4
|
|
|
|
5
|
|
|
use BenTools\MercurePHP\Configuration\Configuration; |
6
|
|
|
use BenTools\MercurePHP\Controller\HealthController; |
7
|
|
|
use BenTools\MercurePHP\Controller\PublishController; |
8
|
|
|
use BenTools\MercurePHP\Controller\SubscribeController; |
9
|
|
|
use BenTools\MercurePHP\Helpers\LoggerAwareTrait; |
10
|
|
|
use BenTools\MercurePHP\Metrics\MetricsHandlerFactory; |
11
|
|
|
use BenTools\MercurePHP\Metrics\MetricsHandlerFactoryInterface; |
12
|
|
|
use BenTools\MercurePHP\Metrics\MetricsHandlerInterface; |
13
|
|
|
use BenTools\MercurePHP\Metrics\PHP\PHPMetricsHandlerFactory; |
14
|
|
|
use BenTools\MercurePHP\Metrics\Redis\RedisMetricsHandlerFactory; |
15
|
|
|
use BenTools\MercurePHP\Security\Authenticator; |
16
|
|
|
use BenTools\MercurePHP\Storage\NullStorage\NullStorageFactory; |
17
|
|
|
use BenTools\MercurePHP\Storage\PHP\PHPStorageFactory; |
18
|
|
|
use BenTools\MercurePHP\Storage\Redis\RedisStorageFactory; |
|
|
|
|
19
|
|
|
use BenTools\MercurePHP\Storage\StorageFactory; |
20
|
|
|
use BenTools\MercurePHP\Storage\StorageFactoryInterface; |
21
|
|
|
use BenTools\MercurePHP\Storage\StorageInterface; |
22
|
|
|
use BenTools\MercurePHP\Transport\PHP\PHPTransportFactory; |
23
|
|
|
use BenTools\MercurePHP\Transport\Redis\RedisTransportFactory; |
24
|
|
|
use BenTools\MercurePHP\Transport\TransportFactory; |
25
|
|
|
use BenTools\MercurePHP\Transport\TransportFactoryInterface; |
26
|
|
|
use BenTools\MercurePHP\Transport\TransportInterface; |
27
|
|
|
use Psr\Log\LoggerInterface; |
28
|
|
|
use Psr\Log\NullLogger; |
29
|
|
|
use React\EventLoop\LoopInterface; |
30
|
|
|
|
31
|
|
|
use function Clue\React\Block\await; |
32
|
|
|
|
33
|
|
|
final class HubFactory |
34
|
|
|
{ |
35
|
|
|
use LoggerAwareTrait; |
36
|
2 |
|
|
37
|
|
|
private array $config; |
38
|
|
|
private ?TransportFactoryInterface $transportFactory; |
39
|
|
|
private ?StorageFactoryInterface $storageFactory; |
40
|
|
|
private ?MetricsHandlerFactoryInterface $metricsHandlerFactory; |
41
|
|
|
|
42
|
|
|
public function __construct( |
43
|
2 |
|
array $config, |
44
|
2 |
|
?LoggerInterface $logger = null, |
45
|
2 |
|
?TransportFactoryInterface $transportFactory = null, |
46
|
2 |
|
?StorageFactoryInterface $storageFactory = null, |
47
|
2 |
|
?MetricsHandlerFactoryInterface $metricsHandlerFactory = null |
48
|
2 |
|
) { |
49
|
|
|
$this->config = $config; |
50
|
2 |
|
$this->logger = $logger ?? new NullLogger(); |
|
|
|
|
51
|
|
|
$this->transportFactory = $transportFactory; |
52
|
2 |
|
$this->storageFactory = $storageFactory; |
53
|
1 |
|
$this->metricsHandlerFactory = $metricsHandlerFactory; |
54
|
1 |
|
} |
55
|
|
|
|
56
|
1 |
|
public function create(LoopInterface $loop): Hub |
57
|
1 |
|
{ |
58
|
|
|
$transport = $this->createTransport($loop); |
59
|
|
|
$storage = $this->createStorage($loop); |
60
|
1 |
|
$metricsHandler = $this->createMetricsHandler($loop); |
61
|
1 |
|
|
62
|
1 |
|
$subscriberAuthenticator = Authenticator::createSubscriberAuthenticator($this->config); |
63
|
1 |
|
$publisherAuthenticator = Authenticator::createPublisherAuthenticator($this->config); |
64
|
1 |
|
|
65
|
|
|
$controllers = [ |
66
|
1 |
|
new HealthController(), |
67
|
1 |
|
(new SubscribeController($this->config, $subscriberAuthenticator, $loop)) |
68
|
1 |
|
->withTransport($transport) |
69
|
1 |
|
->withStorage($storage) |
70
|
|
|
->withLogger($this->logger()) |
71
|
|
|
, |
72
|
|
|
(new PublishController($publisherAuthenticator)) |
73
|
1 |
|
->withTransport($transport) |
74
|
|
|
->withStorage($storage) |
75
|
1 |
|
->withLogger($this->logger()) |
76
|
|
|
, |
77
|
|
|
]; |
78
|
2 |
|
|
79
|
|
|
$requestHandler = new RequestHandler($controllers); |
80
|
2 |
|
|
81
|
2 |
|
return new Hub($this->config, $requestHandler, $metricsHandler, $this->logger()); |
82
|
|
|
} |
83
|
2 |
|
|
84
|
1 |
|
private function createTransport(LoopInterface $loop): TransportInterface |
85
|
|
|
{ |
86
|
|
|
$factory = $this->transportFactory ?? $this->getDefaultTransportFactory($loop); |
87
|
1 |
|
$dsn = $this->config[Configuration::TRANSPORT_URL]; |
88
|
|
|
|
89
|
|
|
if (!$factory->supports($dsn)) { |
90
|
1 |
|
throw new \RuntimeException(\sprintf('Invalid transport DSN %s', $dsn)); |
91
|
|
|
} |
92
|
1 |
|
|
93
|
1 |
|
return await($factory->create($dsn), $loop); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
1 |
|
private function getDefaultTransportFactory(LoopInterface $loop): TransportFactory |
97
|
|
|
{ |
98
|
|
|
return new TransportFactory([ |
99
|
|
|
new RedisTransportFactory($loop, $this->logger()), |
100
|
1 |
|
new PHPTransportFactory(), |
101
|
|
|
]); |
102
|
|
|
} |
103
|
1 |
|
|
104
|
|
|
private function createStorage(LoopInterface $loop): StorageInterface |
105
|
1 |
|
{ |
106
|
1 |
|
$factory = $this->storageFactory ?? $this->getDefaultStorageFactory($loop); |
107
|
|
|
$dsn = $this->config[Configuration::STORAGE_URL] |
108
|
1 |
|
?? $this->config[Configuration::TRANSPORT_URL]; |
109
|
|
|
|
110
|
1 |
|
if (!$factory->supports($dsn)) { |
111
|
|
|
throw new \RuntimeException(\sprintf('Invalid storage DSN %s', $dsn)); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
return await($factory->create($dsn), $loop); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function getDefaultStorageFactory(LoopInterface $loop): StorageFactory |
118
|
|
|
{ |
119
|
|
|
return new StorageFactory([ |
120
|
|
|
new RedisStorageFactory($loop, $this->logger()), |
121
|
|
|
new PHPStorageFactory(), |
122
|
|
|
new NullStorageFactory(), |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function createMetricsHandler(LoopInterface $loop): MetricsHandlerInterface |
127
|
|
|
{ |
128
|
|
|
$factory = $this->metricsHandlerFactory ?? $this->getDefaultMetricsHandlerFactory($loop); |
129
|
|
|
$dsn = $this->config[Configuration::METRICS_URL] |
130
|
|
|
?? $this->config[Configuration::STORAGE_URL] |
131
|
|
|
?? $this->config[Configuration::TRANSPORT_URL]; |
132
|
|
|
|
133
|
|
|
if (!$factory->supports($dsn)) { |
134
|
|
|
throw new \RuntimeException(\sprintf('Invalid metrics handler DSN %s', $dsn)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return await($factory->create($dsn), $loop); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function getDefaultMetricsHandlerFactory(LoopInterface $loop): MetricsHandlerFactory |
141
|
|
|
{ |
142
|
|
|
return new MetricsHandlerFactory([ |
143
|
|
|
new PHPMetricsHandlerFactory(), |
144
|
|
|
new RedisMetricsHandlerFactory($loop, $this->logger()), |
145
|
|
|
]); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths