1
|
|
|
<?php |
2
|
|
|
namespace PSB\Core\Routing; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use PSB\Core\SendOptions; |
6
|
|
|
use PSB\Core\Transport\Config\TransportInfrastructure; |
7
|
|
|
|
8
|
|
|
class UnicastRouter implements UnicastRouterInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $localAddress; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var UnicastRoutingTable |
17
|
|
|
*/ |
18
|
|
|
private $unicastRoutingTable; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var TransportInfrastructure |
22
|
|
|
*/ |
23
|
|
|
private $transportInfrastructure; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $localAddress |
27
|
|
|
* @param UnicastRoutingTable $unicastRoutingTable |
28
|
|
|
* @param TransportInfrastructure $transportInfrastructure |
29
|
|
|
*/ |
30
|
5 |
|
public function __construct( |
31
|
|
|
$localAddress, |
32
|
|
|
UnicastRoutingTable $unicastRoutingTable, |
33
|
|
|
TransportInfrastructure $transportInfrastructure |
34
|
|
|
) { |
35
|
5 |
|
$this->localAddress = $localAddress; |
36
|
5 |
|
$this->unicastRoutingTable = $unicastRoutingTable; |
37
|
5 |
|
$this->transportInfrastructure = $transportInfrastructure; |
38
|
5 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param SendOptions $options |
42
|
|
|
* @param string $messageClass |
43
|
|
|
* |
44
|
|
|
* @return AddressTagInterface[] |
45
|
|
|
*/ |
46
|
4 |
|
public function route(SendOptions $options, $messageClass) |
47
|
|
|
{ |
48
|
4 |
|
$destination = $options->isRoutedToLocalInstance() ? $this->localAddress : null; |
49
|
4 |
|
$destination = $options->getExplicitDestination() ?: $destination; |
50
|
|
|
|
51
|
4 |
|
if ($destination === null || $destination === '') { |
52
|
1 |
|
$messageTypes = array_merge([$messageClass], array_values(class_implements($messageClass, true))); |
53
|
1 |
|
$endpointNames = $this->unicastRoutingTable->getEndpointNamesFor($messageTypes); |
54
|
1 |
|
$destinations = []; |
55
|
1 |
|
foreach ($endpointNames as $endpointName) { |
56
|
1 |
|
$destinations[] = $this->transportInfrastructure->toTransportAddress($endpointName); |
57
|
1 |
|
} |
58
|
1 |
|
} else { |
59
|
3 |
|
$destinations = [$destination]; |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
$addressTags = []; |
63
|
4 |
|
foreach ($destinations as $destination) { |
64
|
4 |
|
$addressTags[] = new UnicastAddressTag($destination); |
65
|
4 |
|
} |
66
|
|
|
|
67
|
4 |
|
return $addressTags; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|