66use Closure ;
77use Exception ;
88use Proklung \RabbitMQ \RabbitMq \Consumer ;
9+ use Symfony \Component \DependencyInjection \Container ;
910use Symfony \Component \DependencyInjection \ContainerBuilder ;
10- use Proklung \RabbitMQ \Provider \ConnectionParametersProviderInterface ;
11- use Proklung \RabbitMq \RabbitMq \AMQPConnectionFactory ;
1211use Proklung \RabbitMq \RabbitMq \AmqpPartsHolder ;
1312use Proklung \RabbitMq \RabbitMq \Binding ;
1413use Proklung \RabbitMQ \RabbitMq \DequeuerAwareInterface ;
@@ -87,10 +86,10 @@ public function __construct()
8786 /**
8887 * Загрузка и инициализация контейнера.
8988 *
90- * @return ContainerBuilder
89+ * @return Container
9190 * @throws Exception
9291 */
93- public static function boot () : ContainerBuilder
92+ public static function boot () : Container
9493 {
9594 $ self = new static ();
9695
@@ -105,10 +104,10 @@ public static function boot() : ContainerBuilder
105104 /**
106105 * Alias boot для читаемости.
107106 *
108- * @return ContainerBuilder
107+ * @return Container
109108 * @throws Exception
110109 */
111- public static function getInstance () : ContainerBuilder
110+ public static function getInstance () : Container
112111 {
113112 return static ::boot ();
114113 }
@@ -170,9 +169,9 @@ public function initContainer() : void
170169 /**
171170 * Экземпляр контейнера.
172171 *
173- * @return ContainerBuilder
172+ * @return Container
174173 */
175- public function getContainer (): ContainerBuilder
174+ public function getContainer (): Container
176175 {
177176 return $ this ->container ;
178177 }
@@ -223,46 +222,35 @@ private function loadConnections() : void
223222 {
224223 foreach ($ this ->config ['connections ' ] as $ key => $ connection ) {
225224 $ 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 );
261252
262- $ this ->container ->set (
263- $ connectionName ,
264- $ createConnector ()
265- );
253+ $ this ->container ->setDefinition (sprintf ('rabbitmq.connection.%s ' , $ key ), $ definition );
266254 }
267255 }
268256
@@ -734,4 +722,4 @@ private function getDefaultQueueOptions(): array
734722 'declare ' => false ,
735723 ];
736724 }
737- }
725+ }
0 commit comments