-
Notifications
You must be signed in to change notification settings - Fork 32
/
DependencyInjection.php
183 lines (168 loc) · 7.22 KB
/
DependencyInjection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php declare(strict_types=1);
namespace mikemadisonweb\rabbitmq;
use mikemadisonweb\rabbitmq\components\{
AbstractConnectionFactory, Consumer, ConsumerInterface, Logger, Producer, Routing
};
use mikemadisonweb\rabbitmq\controllers\RabbitMQController;
use mikemadisonweb\rabbitmq\exceptions\InvalidConfigException;
use PhpAmqpLib\Connection\AbstractConnection;
use yii\base\Application;
use yii\base\BootstrapInterface;
class DependencyInjection implements BootstrapInterface
{
/**
* @var $logger Logger
*/
private $logger;
protected $isLoaded = false;
/**
* Configuration auto-loading
* @param Application $app
* @throws InvalidConfigException
*/
public function bootstrap($app)
{
$config = $app->rabbitmq->getConfig();
$this->registerLogger($config);
$this->registerConnections($config);
$this->registerRouting($config);
$this->registerProducers($config);
$this->registerConsumers($config);
$this->addControllers($app);
}
/**
* Register logger service
* @param $config
*/
private function registerLogger($config)
{
\Yii::$container->setSingleton(Configuration::LOGGER_SERVICE_NAME, ['class' => Logger::class, 'options' => $config->logger]);
}
/**
* Register connections in service container
* @param Configuration $config
*/
protected function registerConnections(Configuration $config)
{
foreach ($config->connections as $options) {
$serviceAlias = sprintf(Configuration::CONNECTION_SERVICE_NAME, $options['name']);
\Yii::$container->setSingleton($serviceAlias, function () use ($options) {
$factory = new AbstractConnectionFactory($options['type'], $options);
return $factory->createConnection();
});
}
}
/**
* Register routing in service container
* @param Configuration $config
*/
protected function registerRouting(Configuration $config)
{
\Yii::$container->setSingleton(Configuration::ROUTING_SERVICE_NAME, function ($container, $params) use ($config) {
$routing = new Routing($params['conn']);
\Yii::$container->invoke([$routing, 'setQueues'], [$config->queues]);
\Yii::$container->invoke([$routing, 'setExchanges'], [$config->exchanges]);
\Yii::$container->invoke([$routing, 'setBindings'], [$config->bindings]);
return $routing;
});
}
/**
* Register producers in service container
* @param Configuration $config
*/
protected function registerProducers(Configuration $config)
{
$autoDeclare = $config->auto_declare;
foreach ($config->producers as $options) {
$serviceAlias = sprintf(Configuration::PRODUCER_SERVICE_NAME, $options['name']);
\Yii::$container->setSingleton($serviceAlias, function () use ($options, $autoDeclare) {
/**
* @var $connection AbstractConnection
*/
$connection = \Yii::$container->get(sprintf(Configuration::CONNECTION_SERVICE_NAME, $options['connection']));
/**
* @var $routing Routing
*/
$routing = \Yii::$container->get(Configuration::ROUTING_SERVICE_NAME, ['conn' => $connection]);
/**
* @var $logger Logger
*/
$logger = \Yii::$container->get(Configuration::LOGGER_SERVICE_NAME);
$producer = new Producer($connection, $routing, $logger, $autoDeclare);
\Yii::$container->invoke([$producer, 'setName'], [$options['name']]);
\Yii::$container->invoke([$producer, 'setContentType'], [$options['content_type']]);
\Yii::$container->invoke([$producer, 'setDeliveryMode'], [$options['delivery_mode']]);
\Yii::$container->invoke([$producer, 'setSafe'], [$options['safe']]);
\Yii::$container->invoke([$producer, 'setSerializer'], [$options['serializer']]);
return $producer;
});
}
}
/**
* Register consumers(one instance per one or multiple queues) in service container
* @param Configuration $config
*/
protected function registerConsumers(Configuration $config)
{
$autoDeclare = $config->auto_declare;
foreach ($config->consumers as $options) {
$serviceAlias = sprintf(Configuration::CONSUMER_SERVICE_NAME, $options['name']);
\Yii::$container->setSingleton($serviceAlias, function () use ($options, $autoDeclare) {
/**
* @var $connection AbstractConnection
*/
$connection = \Yii::$container->get(sprintf(Configuration::CONNECTION_SERVICE_NAME, $options['connection']));
/**
* @var $routing Routing
*/
$routing = \Yii::$container->get(Configuration::ROUTING_SERVICE_NAME, ['conn' => $connection]);
/**
* @var $logger Logger
*/
$logger = \Yii::$container->get(Configuration::LOGGER_SERVICE_NAME);
$consumer = new Consumer($connection, $routing, $logger, $autoDeclare);
$queues = [];
foreach ($options['callbacks'] as $queueName => $callback) {
$callbackClass = $this->getCallbackClass($callback);
$queues[$queueName] = [$callbackClass, 'execute'];
}
\Yii::$container->invoke([$consumer, 'setName'], [$options['name']]);
\Yii::$container->invoke([$consumer, 'setQueues'], [$queues]);
\Yii::$container->invoke([$consumer, 'setQos'], [$options['qos']]);
\Yii::$container->invoke([$consumer, 'setIdleTimeout'], [$options['idle_timeout']]);
\Yii::$container->invoke([$consumer, 'setIdleTimeoutExitCode'], [$options['idle_timeout_exit_code']]);
\Yii::$container->invoke([$consumer, 'setProceedOnException'], [$options['proceed_on_exception']]);
\Yii::$container->invoke([$consumer, 'setDeserializer'], [$options['deserializer']]);
return $consumer;
});
}
}
/**
* Callback can be passed as class name or alias in service container
* @param string $callbackName
* @return ConsumerInterface
* @throws InvalidConfigException
*/
private function getCallbackClass(string $callbackName) : ConsumerInterface
{
if (!class_exists($callbackName)) {
$callbackClass = \Yii::$container->get($callbackName);
} else {
$callbackClass = new $callbackName();
}
if (!($callbackClass instanceof ConsumerInterface)) {
throw new InvalidConfigException("{$callbackName} should implement ConsumerInterface.");
}
return $callbackClass;
}
/**
* Auto-configure console controller classes
* @param Application $app
*/
private function addControllers(Application $app)
{
if($app instanceof \yii\console\Application) {
$app->controllerMap[Configuration::EXTENSION_CONTROLLER_ALIAS] = RabbitMQController::class;
}
}
}