3
3
namespace Proklung \RabbitMq \Integration \DI ;
4
4
5
5
use Bitrix \Main \Config \Configuration ;
6
- use Closure ;
7
6
use Exception ;
8
- use ProklUng \ContainerBoilerplate \CompilerContainer ;
9
- use ProklUng \ ContainerBoilerplate \ Utils \ BitrixSettingsDiAdapter ;
10
- use Symfony \ Component \ DependencyInjection \ Container ;
7
+ use ProklUng \ContainerBoilerplate \DI \ AbstractServiceContainer ;
8
+ use ReflectionClass ;
9
+ use ReflectionException ;
11
10
use Symfony \Component \DependencyInjection \ContainerBuilder ;
12
11
use Proklung \RabbitMq \RabbitMq \Binding ;
13
12
use Symfony \Component \DependencyInjection \ContainerInterface ;
18
17
* Class Services
19
18
* @package Proklung\RabbitMq\Integration\DI
20
19
*/
21
- class Services
20
+ class Services extends AbstractServiceContainer
22
21
{
23
22
/**
24
- * @var array $config
23
+ * @var ContainerBuilder|null $container Контейнер.
25
24
*/
26
- private $ config ;
25
+ protected static $ container ;
27
26
28
27
/**
29
- * @var array $parameters
28
+ * @var array $config Битриксовая конфигурация.
30
29
*/
31
- private $ parameters ;
30
+ protected $ config = [] ;
32
31
33
32
/**
34
- * @var array $services
33
+ * @var array $parameters Параметры битриксового сервис-локатора.
35
34
*/
36
- private $ services ;
35
+ protected $ parameters = [] ;
37
36
38
37
/**
39
- * @var ContainerBuilder $container Контейнер .
38
+ * @var array $services Сервисы битриксового сервис-локатора .
40
39
*/
41
- private static $ container ;
40
+ protected $ services = [] ;
42
41
43
42
/**
44
- * @var string $environment
43
+ * @var string $moduleId ID модуля (переопределяется наследником).
45
44
*/
46
- private $ environment ;
47
-
48
- /**
49
- * @var boolean $debug Режим отладки.
50
- */
51
- private $ debug ;
45
+ protected $ moduleId = 'proklung.rabbitmq ' ;
52
46
53
47
/**
54
48
* Services constructor.
55
49
*/
56
50
public function __construct ()
57
51
{
58
- $ this ->debug = (bool )$ _ENV ['DEBUG ' ] ?? true ;
59
- $ this ->environment = $ this ->debug ? 'dev ' : 'prod ' ;
52
+ parent ::__construct ();
60
53
61
54
$ this ->config = Configuration::getInstance ()->get ('rabbitmq ' ) ?? [];
62
- $ this ->parameters = Configuration::getInstance (' proklung.rabbitmq ' )->get ('parameters ' ) ?? [];
63
- $ this ->services = Configuration::getInstance (' proklung.rabbitmq ' )->get ('services ' ) ?? [];
55
+ $ this ->parameters = Configuration::getInstance ($ this -> moduleId )->get ('parameters ' ) ?? [];
56
+ $ this ->services = Configuration::getInstance ($ this -> moduleId )->get ('services ' ) ?? [];
64
57
65
58
// Инициализация параметров контейнера.
66
59
$ this ->parameters ['cache_path ' ] = $ this ->parameters ['cache_path ' ] ?? '/bitrix/cache/proklung.rabbitmq ' ;
67
60
$ this ->parameters ['container.dumper.inline_factories ' ] = $ this ->parameters ['container.dumper.inline_factories ' ] ?? false ;
68
61
$ this ->parameters ['compile_container_envs ' ] = (array )$ this ->parameters ['compile_container_envs ' ];
69
62
}
70
63
71
- /**
72
- * Загрузка и инициализация контейнера.
73
- *
74
- * @return Container
75
- * @throws Exception
76
- */
77
- public static function boot () : Container
78
- {
79
- $ self = new static ();
80
-
81
- $ self ->load ();
82
-
83
- return $ self ->getContainer ();
84
- }
85
-
86
- /**
87
- * Alias boot для читаемости.
88
- *
89
- * @return Container
90
- * @throws Exception
91
- */
92
- public static function getInstance () : Container
93
- {
94
- return static ::boot ();
95
- }
96
-
97
- /**
98
- * @return void
99
- * @throws Exception
100
- */
101
- public function load () : void
102
- {
103
- if (static ::$ container !== null ) {
104
- return ;
105
- }
106
-
107
- $ this ->createContainer ();
108
- $ compilerContainer = new CompilerContainer ($ _SERVER ['DOCUMENT_ROOT ' ]);
109
- $ compilerContainer ->setModuleId ('proklung.rabbitmq ' );
110
-
111
- // Кэшировать контейнер?
112
- if (!in_array ($ this ->environment , $ this ->parameters ['compile_container_envs ' ], true )) {
113
- $ this ->initContainer ();
114
- return ;
115
- }
116
-
117
- static ::$ container = $ compilerContainer ->cacheContainer (
118
- static ::$ container ,
119
- $ _SERVER ['DOCUMENT_ROOT ' ] . $ this ->parameters ['cache_path ' ],
120
- 'container.php ' ,
121
- $ this ->environment ,
122
- $ this ->debug ,
123
- Closure::fromCallable ([$ this , 'initContainer ' ])
124
- );
125
- }
126
-
127
64
/**
128
65
* @return void
129
66
* @throws Exception
@@ -144,31 +81,6 @@ public function initContainer() : void
144
81
static ::$ container ->compile (false );
145
82
}
146
83
147
- /**
148
- * Экземпляр контейнера.
149
- *
150
- * @return Container
151
- */
152
- public function getContainer (): Container
153
- {
154
- return static ::$ container ;
155
- }
156
-
157
- /**
158
- * Создать пустой экземпляр контейнера.
159
- *
160
- * @return void
161
- */
162
- private function createContainer () : void
163
- {
164
- static ::$ container = new ContainerBuilder ();
165
- $ adapter = new BitrixSettingsDiAdapter ();
166
-
167
- $ adapter ->importParameters (static ::$ container , $ this ->config );
168
- $ adapter ->importParameters (static ::$ container , $ this ->parameters );
169
- $ adapter ->importServices (static ::$ container , $ this ->services );
170
- }
171
-
172
84
/**
173
85
* @return void
174
86
* @throws Exception
@@ -635,6 +547,7 @@ private function argumentsStringAsArray($arguments)
635
547
* @param string $name
636
548
*
637
549
* @return void
550
+ * @throws ReflectionException
638
551
*/
639
552
private function addDequeuerAwareCall ($ callback , $ name ) : void
640
553
{
@@ -648,18 +561,34 @@ private function addDequeuerAwareCall($callback, $name) : void
648
561
}
649
562
}
650
563
564
+ /**
565
+ * @param Definition $definition
566
+ * @param mixed $connectionName
567
+ *
568
+ * @return void
569
+ */
651
570
private function injectConnection (Definition $ definition , $ connectionName )
652
571
{
653
572
$ definition ->addArgument (new Reference (sprintf ('rabbitmq.connection.%s ' , $ connectionName )));
654
573
}
655
574
575
+ /**
576
+ * @param string $class
577
+ *
578
+ * @return boolean
579
+ *
580
+ * @throws ReflectionException
581
+ */
656
582
private function isDequeverAwareInterface (string $ class ): bool
657
583
{
658
- $ refClass = new \ ReflectionClass ($ class );
584
+ $ refClass = new ReflectionClass ($ class );
659
585
660
586
return $ refClass ->implementsInterface ('Proklung\RabbitMq\RabbitMq\DequeuerAwareInterface ' );
661
587
}
662
588
589
+ /**
590
+ * @return array
591
+ */
663
592
private function getDefaultExchangeOptions (): array
664
593
{
665
594
return [
@@ -670,6 +599,9 @@ private function getDefaultExchangeOptions(): array
670
599
];
671
600
}
672
601
602
+ /**
603
+ * @return array
604
+ */
673
605
private function getDefaultQueueOptions (): array
674
606
{
675
607
return [
0 commit comments