@@ -37,7 +37,7 @@ class Services
3737 /**
3838 * @var ContainerBuilder $container Контейнер.
3939 */
40- private $ container ;
40+ private static $ container ;
4141
4242 /**
4343 * @var string $environment
@@ -65,13 +65,6 @@ public function __construct()
6565 $ this ->parameters ['cache_path ' ] = $ this ->parameters ['cache_path ' ] ?? '/bitrix/cache/proklung.rabbitmq ' ;
6666 $ this ->parameters ['container.dumper.inline_factories ' ] = $ this ->parameters ['container.dumper.inline_factories ' ] ?? false ;
6767 $ this ->parameters ['compile_container_envs ' ] = (array )$ this ->parameters ['compile_container_envs ' ];
68-
69- $ this ->container = new ContainerBuilder ();
70- $ adapter = new BitrixSettingsDiAdapter ();
71-
72- $ adapter ->importParameters ($ this ->container , $ this ->config );
73- $ adapter ->importParameters ($ this ->container , $ this ->parameters );
74- $ adapter ->importServices ($ this ->container , $ this ->services );
7568 }
7669
7770 /**
@@ -106,6 +99,11 @@ public static function getInstance() : Container
10699 */
107100 public function load () : void
108101 {
102+ if (static ::$ container !== null ) {
103+ return ;
104+ }
105+
106+ $ this ->createContainer ();
109107 $ compilerContainer = new CompilerContainer ();
110108
111109 // Кэшировать контейнер?
@@ -114,8 +112,8 @@ public function load() : void
114112 return ;
115113 }
116114
117- $ this -> container = $ compilerContainer ->cacheContainer (
118- $ this -> container ,
115+ static :: $ container = $ compilerContainer ->cacheContainer (
116+ static :: $ container ,
119117 $ _SERVER ['DOCUMENT_ROOT ' ] . $ this ->parameters ['cache_path ' ],
120118 'container.php ' ,
121119 $ this ->environment ,
@@ -141,7 +139,7 @@ public function initContainer() : void
141139
142140 $ this ->loadPartsHolder ();
143141
144- $ this -> container ->compile (false );
142+ static :: $ container ->compile (false );
145143 }
146144
147145 /**
@@ -151,7 +149,22 @@ public function initContainer() : void
151149 */
152150 public function getContainer (): Container
153151 {
154- return $ this ->container ;
152+ return static ::$ container ;
153+ }
154+
155+ /**
156+ * Создать пустой экземпляр контейнера.
157+ *
158+ * @return void
159+ */
160+ private function createContainer () : void
161+ {
162+ static ::$ container = new ContainerBuilder ();
163+ $ adapter = new BitrixSettingsDiAdapter ();
164+
165+ $ adapter ->importParameters (static ::$ container , $ this ->config );
166+ $ adapter ->importParameters (static ::$ container , $ this ->parameters );
167+ $ adapter ->importServices (static ::$ container , $ this ->services );
155168 }
156169
157170 /**
@@ -176,7 +189,7 @@ private function loadPartsHolder() : void
176189 $ this ->injectConnection ($ definition , $ binding ['connection ' ]);
177190 $ key = md5 (json_encode ($ binding ));
178191
179- $ this -> container ->setDefinition (sprintf ('rabbitmq.binding.%s ' , $ key ), $ definition );
192+ static :: $ container ->setDefinition (sprintf ('rabbitmq.binding.%s ' , $ key ), $ definition );
180193 }
181194 }
182195
@@ -202,7 +215,7 @@ private function loadConnections() : void
202215 }
203216 $ definition ->setPublic (true );
204217 $ factoryName = sprintf ('rabbitmq.connection_factory.%s ' , $ key );
205- $ this -> container ->setDefinition ($ factoryName , $ definition );
218+ static :: $ container ->setDefinition ($ factoryName , $ definition );
206219
207220 $ definition = new Definition ($ classParam );
208221 if (method_exists ($ definition , 'setFactory ' )) {
@@ -216,7 +229,7 @@ private function loadConnections() : void
216229 $ definition ->addTag ('rabbitmq.connection ' );
217230 $ definition ->setPublic (true );
218231
219- $ this -> container ->setDefinition (sprintf ('rabbitmq.connection.%s ' , $ key ), $ definition );
232+ static :: $ container ->setDefinition (sprintf ('rabbitmq.connection.%s ' , $ key ), $ definition );
220233 }
221234 }
222235
@@ -243,7 +256,7 @@ private function loadBindings() : void
243256 $ connectionName = "rabbitmq.connection. {$ binding ['connection ' ]}" ;
244257
245258 /** @var Binding $instance */
246- $ instance = new $ className ($ this -> container ->get ($ connectionName ));
259+ $ instance = new $ className (static :: $ container ->get ($ connectionName ));
247260
248261 $ instance ->setArguments ($ binding ['arguments ' ]);
249262 $ instance ->setDestination ($ binding ['destination ' ]);
@@ -255,7 +268,7 @@ private function loadBindings() : void
255268 return $ instance ;
256269 };
257270
258- $ this -> container ->set (
271+ static :: $ container ->set (
259272 "rabbitmq.binding. {$ key }" ,
260273 $ binding ()
261274 );
@@ -270,7 +283,7 @@ private function loadProducers() : void
270283 {
271284 if ($ this ->config ['sandbox ' ] == false ) {
272285 foreach ($ this ->config ['producers ' ] as $ key => $ producer ) {
273- $ definition = new Definition ($ producer ['class ' ] ?? $ this -> container ->getParameter ('rabbitmq.producer.class ' ));
286+ $ definition = new Definition ($ producer ['class ' ] ?? static :: $ container ->getParameter ('rabbitmq.producer.class ' ));
274287 $ definition ->setPublic (true );
275288 $ definition ->addTag ('rabbitmq.base_amqp ' );
276289 $ definition ->addTag ('rabbitmq.producer ' );
@@ -296,15 +309,15 @@ private function loadProducers() : void
296309
297310 $ producerServiceName = sprintf ('rabbitmq.%s_producer ' , $ key );
298311
299- $ this -> container ->setDefinition ($ producerServiceName , $ definition );
312+ static :: $ container ->setDefinition ($ producerServiceName , $ definition );
300313 if (null !== $ producer ['service_alias ' ]) {
301- $ this -> container ->setAlias ($ producer ['service_alias ' ], $ producerServiceName );
314+ static :: $ container ->setAlias ($ producer ['service_alias ' ], $ producerServiceName );
302315 }
303316 }
304317 } else {
305318 foreach ($ this ->config ['producers ' ] as $ key => $ producer ) {
306319 $ definition = new Definition ('%rabbitmq.fallback.class% ' );
307- $ this -> container ->setDefinition (sprintf ('rabbitmq.%s_producer ' , $ key ), $ definition );
320+ static :: $ container ->setDefinition (sprintf ('rabbitmq.%s_producer ' , $ key ), $ definition );
308321 }
309322 }
310323 }
@@ -372,7 +385,7 @@ private function loadConsumers() : void
372385 }
373386
374387 $ name = sprintf ('rabbitmq.%s_consumer ' , $ key );
375- $ this -> container ->setDefinition ($ name , $ definition );
388+ static :: $ container ->setDefinition ($ name , $ definition );
376389 $ this ->addDequeuerAwareCall ($ consumer ['callback ' ], $ name );
377390 }
378391 }
@@ -399,7 +412,7 @@ private function loadRpcClients() : void
399412 }
400413 $ definition ->setPublic (true );
401414
402- $ this -> container ->setDefinition (sprintf ('rabbitmq.%s_rpc ' , $ key ), $ definition );
415+ static :: $ container ->setDefinition (sprintf ('rabbitmq.%s_rpc ' , $ key ), $ definition );
403416 }
404417 }
405418
@@ -442,7 +455,7 @@ private function loadRpcServers() : void
442455 $ definition ->addMethodCall ('setSerializer ' , array ($ server ['serializer ' ]));
443456 }
444457
445- $ this -> container ->setDefinition (sprintf ('rabbitmq.%s_server ' , $ key ), $ definition );
458+ static :: $ container ->setDefinition (sprintf ('rabbitmq.%s_server ' , $ key ), $ definition );
446459 }
447460 }
448461
@@ -464,7 +477,7 @@ private function loadAnonConsumers() : void
464477 $ this ->injectConnection ($ definition , $ anon ['connection ' ]);
465478
466479 $ name = sprintf ('rabbitmq.%s_anon ' , $ key );
467- $ this -> container ->setDefinition ($ name , $ definition );
480+ static :: $ container ->setDefinition ($ name , $ definition );
468481 $ this ->addDequeuerAwareCall ($ anon ['callback ' ], $ name );
469482 }
470483 }
@@ -528,7 +541,7 @@ private function loadBatchConsumers() : void
528541 $ this ->injectLogger ($ definition );
529542 }
530543
531- $ this -> container ->setDefinition (sprintf ('rabbitmq.%s_batch ' , $ key ), $ definition );
544+ static :: $ container ->setDefinition (sprintf ('rabbitmq.%s_batch ' , $ key ), $ definition );
532545 }
533546 }
534547
@@ -557,7 +570,7 @@ private function registerCallbackAsService(string $class)
557570 // Регистрация class как сервиса.
558571 $ defCallBack = new Definition ($ class );
559572 $ defCallBack ->setPublic (true );
560- $ this -> container ->setDefinition ($ class , $ defCallBack );
573+ static :: $ container ->setDefinition ($ class , $ defCallBack );
561574 }
562575
563576 /**
@@ -623,11 +636,11 @@ private function argumentsStringAsArray($arguments)
623636 */
624637 private function addDequeuerAwareCall ($ callback , $ name ) : void
625638 {
626- if (!$ this -> container ->has ($ callback )) {
639+ if (!static :: $ container ->has ($ callback )) {
627640 return ;
628641 }
629642
630- $ callbackDefinition = $ this -> container ->findDefinition ($ callback );
643+ $ callbackDefinition = static :: $ container ->findDefinition ($ callback );
631644 if ($ this ->isDequeverAwareInterface ($ callbackDefinition ->getClass ())) {
632645 $ callbackDefinition ->addMethodCall ('setDequeuer ' , array (new Reference ($ name )));
633646 }
0 commit comments