|
3 | 3 | namespace Proklung\Redis\DI; |
4 | 4 |
|
5 | 5 | use Bitrix\Main\Config\Configuration; |
6 | | -use Closure; |
7 | 6 | use Enqueue\Consumption\Extension\ReplyExtension; |
8 | 7 | use Enqueue\Consumption\Extension\SignalExtension; |
9 | | -use ProklUng\ContainerBoilerplate\CompilerContainer; |
10 | | -use ProklUng\ContainerBoilerplate\Utils\BitrixSettingsDiAdapter; |
| 8 | +use ProklUng\ContainerBoilerplate\DI\AbstractServiceContainer; |
11 | 9 | use Proklung\Redis\DI\Extensions\ResetServicesExtension; |
12 | 10 | use Proklung\Redis\Profiler\MessageQueueCollector; |
13 | 11 | use Enqueue\Client\CommandSubscriberInterface; |
|
30 | 28 | use Symfony\Component\Config\Definition\Processor; |
31 | 29 | use Symfony\Component\Config\FileLocator; |
32 | 30 | use Symfony\Component\DependencyInjection\Compiler\PassConfig; |
33 | | -use Symfony\Component\DependencyInjection\Container; |
34 | 31 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
35 | 32 | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
36 | 33 | use Symfony\Component\DependencyInjection\Reference; |
|
42 | 39 | * @since 13.07.2021 |
43 | 40 | * @internal Частично форкнуто из оригинального пакета https://github.com/php-enqueue/enqueue-bundle. |
44 | 41 | */ |
45 | | -class Services |
| 42 | +class Services extends AbstractServiceContainer |
46 | 43 | { |
47 | 44 | /** |
48 | 45 | * @var ContainerBuilder|null $container Контейнер. |
49 | 46 | */ |
50 | | - private static $container; |
| 47 | + protected static $container; |
51 | 48 |
|
52 | 49 | /** |
53 | | - * @var array $config |
| 50 | + * @var array $config Битриксовая конфигурация. |
54 | 51 | */ |
55 | | - private $config; |
| 52 | + protected $config = []; |
56 | 53 |
|
57 | 54 | /** |
58 | | - * @var array $parameters |
| 55 | + * @var array $parameters Параметры битриксового сервис-локатора. |
59 | 56 | */ |
60 | | - private $parameters; |
| 57 | + protected $parameters = []; |
61 | 58 |
|
62 | 59 | /** |
63 | | - * @var array $services |
| 60 | + * @var array $services Сервисы битриксового сервис-локатора. |
64 | 61 | */ |
65 | | - private $services; |
| 62 | + protected $services = []; |
66 | 63 |
|
67 | 64 | /** |
68 | | - * @var string $environment |
| 65 | + * @var string $moduleId ID модуля (переопределяется наследником). |
69 | 66 | */ |
70 | | - private $environment; |
71 | | - |
72 | | - /** |
73 | | - * @var boolean $debug Режим отладки. |
74 | | - */ |
75 | | - private $debug; |
| 67 | + protected $moduleId = 'proklung.redis'; |
76 | 68 |
|
77 | 69 | /** |
78 | 70 | * Services constructor. |
79 | 71 | */ |
80 | 72 | public function __construct() |
81 | 73 | { |
82 | | - $this->debug = (bool)$_ENV['DEBUG'] ?? true; |
83 | | - $this->environment = $this->debug ? 'dev' : 'prod'; |
| 74 | + parent::__construct(); |
84 | 75 |
|
85 | | - $this->config = Configuration::getInstance()->get('proklung.redis') ?? ['enqueue' => []]; |
86 | | - $this->parameters = Configuration::getInstance('proklung.redis')->get('parameters') ?? []; |
87 | | - $this->services = Configuration::getInstance('proklung.redis')->get('services') ?? []; |
| 76 | + $this->config = Configuration::getInstance()->get($this->moduleId) ?? ['enqueue' => []]; |
| 77 | + $this->parameters = Configuration::getInstance($this->moduleId)->get('parameters') ?? []; |
| 78 | + $this->services = Configuration::getInstance($this->moduleId)->get('services') ?? []; |
88 | 79 |
|
89 | 80 | // Инициализация параметров контейнера. |
90 | 81 | $this->parameters['cache_path'] = $this->parameters['cache_path'] ?? '/bitrix/cache/proklung.redis'; |
91 | 82 | $this->parameters['container.dumper.inline_factories'] = $this->parameters['container.dumper.inline_factories'] ?? false; |
92 | 83 | $this->parameters['compile_container_envs'] = (array)$this->parameters['compile_container_envs']; |
93 | 84 | } |
94 | 85 |
|
95 | | - /** |
96 | | - * Загрузка и инициализация контейнера. |
97 | | - * |
98 | | - * @return Container |
99 | | - * @throws Exception |
100 | | - */ |
101 | | - public static function boot() : Container |
102 | | - { |
103 | | - $self = new static(); |
104 | | - |
105 | | - $self->load(); |
106 | | - |
107 | | - return $self->getContainer(); |
108 | | - } |
109 | | - |
110 | | - /** |
111 | | - * Alias boot для читаемости. |
112 | | - * |
113 | | - * @return Container |
114 | | - * @throws Exception |
115 | | - */ |
116 | | - public static function getInstance() : Container |
117 | | - { |
118 | | - return static::boot(); |
119 | | - } |
120 | | - |
121 | | - /** |
122 | | - * Загрузка всего хозяйства. |
123 | | - * |
124 | | - * @return void |
125 | | - * @throws Exception |
126 | | - */ |
127 | | - public function load() : void |
128 | | - { |
129 | | - if (static::$container !== null) { |
130 | | - return; |
131 | | - } |
132 | | - |
133 | | - $this->createContainer(); |
134 | | - $compilerContainer = new CompilerContainer($_SERVER['DOCUMENT_ROOT']); |
135 | | - $compilerContainer->setModuleId('proklung.redis'); |
136 | | - |
137 | | - // Кэшировать контейнер? |
138 | | - if (!in_array($this->environment, $this->parameters['compile_container_envs'], true)) { |
139 | | - $this->initContainer(); |
140 | | - return; |
141 | | - } |
142 | | - |
143 | | - static::$container = $compilerContainer->cacheContainer( |
144 | | - static::$container, |
145 | | - $_SERVER['DOCUMENT_ROOT'] . $this->parameters['cache_path'], |
146 | | - 'container.php', |
147 | | - $this->environment, |
148 | | - $this->debug, |
149 | | - Closure::fromCallable([$this, 'initContainer']) |
150 | | - ); |
151 | | - } |
152 | | - |
153 | 86 | /** |
154 | 87 | * Инициализация контейнера. |
155 | 88 | * |
@@ -252,31 +185,6 @@ public function initContainer() : void |
252 | 185 | static::$container->compile(true); |
253 | 186 | } |
254 | 187 |
|
255 | | - /** |
256 | | - * Экземпляр контейнера. |
257 | | - * |
258 | | - * @return Container |
259 | | - */ |
260 | | - public function getContainer(): Container |
261 | | - { |
262 | | - return static::$container; |
263 | | - } |
264 | | - |
265 | | - /** |
266 | | - * Создать пустой экземпляр контейнера. |
267 | | - * |
268 | | - * @return void |
269 | | - */ |
270 | | - private function createContainer() : void |
271 | | - { |
272 | | - static::$container = new ContainerBuilder(); |
273 | | - $adapter = new BitrixSettingsDiAdapter(); |
274 | | - |
275 | | - $adapter->importParameters(static::$container, $this->config); |
276 | | - $adapter->importParameters(static::$container, $this->parameters); |
277 | | - $adapter->importServices(static::$container, $this->services); |
278 | | - } |
279 | | - |
280 | 188 | /** |
281 | 189 | * @param ContainerBuilder $container |
282 | 190 | * @param string $defaultClient |
|
0 commit comments