6
6
use Closure ;
7
7
use Exception ;
8
8
use Proklung \RabbitMQ \RabbitMq \Consumer ;
9
+ use Symfony \Component \DependencyInjection \Container ;
9
10
use Symfony \Component \DependencyInjection \ContainerBuilder ;
10
- use Proklung \RabbitMQ \Provider \ConnectionParametersProviderInterface ;
11
- use Proklung \RabbitMq \RabbitMq \AMQPConnectionFactory ;
12
11
use Proklung \RabbitMq \RabbitMq \AmqpPartsHolder ;
13
12
use Proklung \RabbitMq \RabbitMq \Binding ;
14
13
use Proklung \RabbitMQ \RabbitMq \DequeuerAwareInterface ;
@@ -87,10 +86,10 @@ public function __construct()
87
86
/**
88
87
* Загрузка и инициализация контейнера.
89
88
*
90
- * @return ContainerBuilder
89
+ * @return Container
91
90
* @throws Exception
92
91
*/
93
- public static function boot () : ContainerBuilder
92
+ public static function boot () : Container
94
93
{
95
94
$ self = new static ();
96
95
@@ -105,10 +104,10 @@ public static function boot() : ContainerBuilder
105
104
/**
106
105
* Alias boot для читаемости.
107
106
*
108
- * @return ContainerBuilder
107
+ * @return Container
109
108
* @throws Exception
110
109
*/
111
- public static function getInstance () : ContainerBuilder
110
+ public static function getInstance () : Container
112
111
{
113
112
return static ::boot ();
114
113
}
@@ -170,9 +169,9 @@ public function initContainer() : void
170
169
/**
171
170
* Экземпляр контейнера.
172
171
*
173
- * @return ContainerBuilder
172
+ * @return Container
174
173
*/
175
- public function getContainer (): ContainerBuilder
174
+ public function getContainer (): Container
176
175
{
177
176
return $ this ->container ;
178
177
}
@@ -223,46 +222,35 @@ private function loadConnections() : void
223
222
{
224
223
foreach ($ this ->config ['connections ' ] as $ key => $ connection ) {
225
224
$ connectionSuffix = $ connection ['use_socket ' ] ? 'socket_connection.class ' : 'connection.class ' ;
226
- $ classParam = $ connection ['lazy ' ]
227
- ? 'rabbitmq.lazy. ' . $ connectionSuffix
228
- : 'rabbitmq. ' . $ connectionSuffix ;
229
-
230
- $ factoryName = "rabbitmq.connection_factory. {$ key }" ;
231
- $ connectionName = "rabbitmq.connection. {$ key }" ;
232
-
233
- $ constructor = function () use ($ classParam , $ connection ) {
234
- $ className = $ this ->parameters ['rabbitmq.connection_factory.class ' ];
235
-
236
- $ parametersProvider = null ;
237
-
238
- if (isset ($ connection ['connection_parameters_provider ' ])) {
239
- /** @var ConnectionParametersProviderInterface $parametersProvider */
240
- $ parametersProvider = $ this ->container ->get ($ connection ['connection_parameters_provider ' ]);
241
- }
242
-
243
- /** @var AMQPConnectionFactory $instance */
244
- $ instance = new $ className (
245
- $ this ->parameters [$ classParam ],
246
- $ connection ,
247
- $ parametersProvider
248
- );
249
-
250
- return $ instance ;
251
- };
252
-
253
- $ createConnector = function () use ($ factoryName ) {
254
- return $ this ->container ->get ($ factoryName )->createConnection ();
255
- };
256
-
257
- $ this ->container ->set (
258
- $ factoryName ,
259
- $ constructor ()
260
- );
225
+ $ classParam =
226
+ $ connection ['lazy ' ]
227
+ ? '%rabbitmq.lazy. ' .$ connectionSuffix .'% '
228
+ : '%rabbitmq. ' .$ connectionSuffix .'% ' ;
229
+
230
+ $ definition = new Definition ('%rabbitmq.connection_factory.class% ' , array (
231
+ $ classParam , $ connection ,
232
+ ));
233
+ if (isset ($ connection ['connection_parameters_provider ' ])) {
234
+ $ definition ->addArgument (new Reference ($ connection ['connection_parameters_provider ' ]));
235
+ unset($ connection ['connection_parameters_provider ' ]);
236
+ }
237
+ $ definition ->setPublic (true );
238
+ $ factoryName = sprintf ('rabbitmq.connection_factory.%s ' , $ key );
239
+ $ this ->container ->setDefinition ($ factoryName , $ definition );
240
+
241
+ $ definition = new Definition ($ classParam );
242
+ if (method_exists ($ definition , 'setFactory ' )) {
243
+ // to be inlined in services.xml when dependency on Symfony DependencyInjection is bumped to 2.6
244
+ $ definition ->setFactory (array (new Reference ($ factoryName ), 'createConnection ' ));
245
+ } else {
246
+ // to be removed when dependency on Symfony DependencyInjection is bumped to 2.6
247
+ $ definition ->setFactoryService ($ factoryName );
248
+ $ definition ->setFactoryMethod ('createConnection ' );
249
+ }
250
+ $ definition ->addTag ('rabbitmq.connection ' );
251
+ $ definition ->setPublic (true );
261
252
262
- $ this ->container ->set (
263
- $ connectionName ,
264
- $ createConnector ()
265
- );
253
+ $ this ->container ->setDefinition (sprintf ('rabbitmq.connection.%s ' , $ key ), $ definition );
266
254
}
267
255
}
268
256
@@ -734,4 +722,4 @@ private function getDefaultQueueOptions(): array
734
722
'declare ' => false ,
735
723
];
736
724
}
737
- }
725
+ }
0 commit comments