1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueue\QueueManager; |
4
|
|
|
|
5
|
|
|
use Gendoria\CommandQueue\Command\CommandInterface; |
6
|
|
|
use Gendoria\CommandQueue\RouteDetection\Detection\DetectionInterface; |
7
|
|
|
use Gendoria\CommandQueue\RouteDetection\Detector\CachedRouteDetector; |
8
|
|
|
use Gendoria\CommandQueue\SendDriver\SendDriverInterface; |
9
|
|
|
use Psr\Log\LoggerAwareInterface; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use Psr\Log\NullLogger; |
12
|
|
|
use RuntimeException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Description of MultipleQueueManager |
16
|
|
|
* |
17
|
|
|
* @author Tomasz Struczyński <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class MultipleQueueManager implements MultipleQueueManagerInterface, LoggerAwareInterface, CommandRouterInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Send drivers array, where keays are pool names. |
24
|
|
|
* |
25
|
|
|
* @var SendDriverInterface[] |
26
|
|
|
*/ |
27
|
|
|
private $sendDrivers; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Default pool name. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $defaultPool = ""; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Logger interface. |
38
|
|
|
* |
39
|
|
|
* @var LoggerInterface |
40
|
|
|
*/ |
41
|
|
|
private $logger; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Pool detector. |
45
|
|
|
* |
46
|
|
|
* @var CachedRouteDetector |
47
|
|
|
*/ |
48
|
|
|
private $poolDetector; |
49
|
|
|
|
50
|
9 |
|
public function __construct(LoggerInterface $logger = null) |
51
|
|
|
{ |
52
|
9 |
|
if ($logger) { |
53
|
1 |
|
$this->logger = $logger; |
54
|
1 |
|
} else { |
55
|
8 |
|
$this->logger = new NullLogger(); |
56
|
|
|
} |
57
|
9 |
|
$this->poolDetector = new CachedRouteDetector(); |
58
|
9 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Add new command route. |
62
|
|
|
* |
63
|
|
|
* @param string $commandExpression |
64
|
|
|
* @param string $poolName |
65
|
|
|
*/ |
66
|
6 |
|
public function addCommandRoute($commandExpression, $poolName) |
67
|
|
|
{ |
68
|
6 |
|
$this->poolDetector->addRoute($commandExpression, $poolName); |
69
|
6 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
7 |
|
public function addSendDriver( |
75
|
|
|
$pool, SendDriverInterface $sendDriver, $isDefault = false |
76
|
|
|
) |
77
|
|
|
{ |
78
|
7 |
|
$this->sendDrivers[$pool] = $sendDriver; |
79
|
7 |
|
if (!$this->defaultPool || $isDefault) { |
80
|
7 |
|
$this->defaultPool = $pool; |
81
|
7 |
|
$this->poolDetector->setDefault($pool); |
82
|
7 |
|
} |
83
|
7 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
8 |
|
public function sendCommand(CommandInterface $command) |
89
|
|
|
{ |
90
|
8 |
|
$poolName = $this->detectPool($command); |
91
|
8 |
|
if (!$poolName || empty($this->sendDrivers[$poolName])) { |
92
|
1 |
|
$this->logger->critical("Send driver not set for given command class, or no default send driver set.", |
93
|
1 |
|
array($command, $this)); |
94
|
1 |
|
throw new RuntimeException("Send driver not set"); |
95
|
|
|
} |
96
|
7 |
|
$this->logger->debug(sprintf("Sending command class %s to pool %s", |
97
|
7 |
|
get_class($command), $poolName), array($command, $this)); |
98
|
7 |
|
$this->sendDrivers[$poolName]->send($command); |
99
|
7 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set logger instance. |
103
|
|
|
* |
104
|
|
|
* @param LoggerInterface $logger |
105
|
|
|
*/ |
106
|
1 |
|
public function setLogger(LoggerInterface $logger) |
107
|
|
|
{ |
108
|
1 |
|
$this->logger = $logger; |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Detect correct pool for given command. |
113
|
|
|
* |
114
|
|
|
* @param CommandInterface $command |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
8 |
|
private function detectPool(CommandInterface $command) |
118
|
|
|
{ |
119
|
8 |
|
$commandClass = get_class($command); |
120
|
8 |
|
return $this->poolDetector->detect($commandClass); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|