1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
BrokerManager.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Main\Messenger\Internals;
6
7use Bitrix\Main\ArgumentException;
8use Bitrix\Main\Config\Configuration;
9use Bitrix\Main\Config\ConfigurationException;
10use Bitrix\Main\DI\ServiceLocator;
11use Bitrix\Main\Loader;
12use Bitrix\Main\LoaderException;
13use Bitrix\Main\Messenger\Broker\BrokerInterface;
14use Bitrix\Main\Messenger\Internals\Broker\DbBroker;
15use Bitrix\Main\Messenger\Internals\Config\QueueConfigRegistry;
16use Bitrix\Main\Messenger\Internals\Storage\Db\Model\MessengerMessageTable;
17use Bitrix\Main\ModuleManager;
18use Bitrix\Main\SystemException;
19
24{
28 private array $brokers = [];
29
33 private array $configs = [];
34
38 public function __construct()
39 {
40 $this->loadConfig();
41 }
42
46 private function loadConfig(): void
47 {
48 $this->loadGlobalConfig();
49 $this->loadModulesConfigs();
50 }
51
55 private function loadGlobalConfig(): void
56 {
57 $config = Configuration::getValue('messenger');
58
59 if (!$config)
60 {
61 $config = [
62 'brokers' => [
63 'default' => [
64 'type' => DbBroker::TYPE_CODE,
65 'params' => [
66 'table' => MessengerMessageTable::class,
67 ]
68 ]
69 ],
70 ];
71 }
72
73 if (empty($config['brokers']['default']))
74 {
75 throw new ConfigurationException('Default broker for messenger did not configured');
76 }
77
78 foreach ($config['brokers'] as $name => $broker)
79 {
80 $this->loadBrokerConfig($name, $broker);
81 }
82 }
83
87 private function loadModulesConfigs(): void
88 {
90
91 foreach ($modules as $moduleId => $moduleData)
92 {
93 $settings = Configuration::getInstance($moduleId)->get('messenger');
94
95 if (!empty($settings['brokers']) && is_array($settings['brokers']))
96 {
97 foreach ($settings['brokers'] as $name => $broker)
98 {
99 $this->loadBrokerConfig($name, $broker);
100 }
101 }
102 }
103 }
104
111 private function loadBrokerConfig(string $brokerName, array $brokerConfig): void
112 {
113 if (empty($brokerConfig['type']) || empty($brokerConfig['params']))
114 {
115 throw new ConfigurationException('Wrong configuration of broker ' . $brokerName);
116 }
117
118 // In future replace to use config of broker types (type => className)
119 if (!in_array($brokerConfig['type'], [DbBroker::TYPE_CODE], true))
120 {
121 throw new ConfigurationException('Unknown broker type ' . $brokerConfig['type']);
122 }
123
124 $this->configs[$brokerName] = $brokerConfig;
125 }
126
132 public function getBroker(string $queueId): BrokerInterface
133 {
135 $registry = ServiceLocator::getInstance()->get(QueueConfigRegistry::class);
136
137 $queueConfig = $registry->getQueueConfig($queueId);
138
139 if (!$queueConfig)
140 {
141 throw new ConfigurationException(sprintf('Queue "%s" was not configured', $queueId));
142 }
143
144 $brokerCode = $queueConfig->brokerCode ?? 'default';
145
146 return $this->brokers[$brokerCode] ?? $this->initBroker($brokerCode);
147 }
148
152 public function getDbBrokerTableClass(array $brokerConfig, string $brokerName): string
153 {
154 $tableClass = $brokerConfig['params']['table'] ?? null;
155
156 if (empty($tableClass))
157 {
158 throw new ConfigurationException('Table class should be not empty for broker ' . $brokerName);
159 }
160
161 if ($tableClass !== MessengerMessageTable::class && !is_subclass_of($tableClass, MessengerMessageTable::class))
162 {
163 throw new ConfigurationException(
164 sprintf('Table class "%s" should be in hierarchy of "%s"', $tableClass, MessengerMessageTable::class)
165 );
166 }
167
168 return $tableClass;
169 }
170
176 private function initBroker(string $brokerCode): BrokerInterface
177 {
178 if (!isset($this->configs[$brokerCode]))
179 {
180 throw new ConfigurationException(sprintf('Broker "%s" did not configured', $brokerCode));
181 }
182
183 $brokerConfig = $this->configs[$brokerCode];
184
185 if ($brokerConfig['type'] === DbBroker::TYPE_CODE)
186 {
187 if (isset($brokerConfig['params']['module']))
188 {
189 Loader::includeModule($brokerConfig['params']['module']);
190 }
191
193 $tableClass = $this->getDbBrokerTableClass($brokerConfig, $brokerCode);
194
195 try
196 {
197 $this->brokers[$brokerCode] = new DbBroker($tableClass::getEntity());
198
199 return $this->brokers[$brokerCode];
200 }
201 catch (ArgumentException $e)
202 {
203 throw new ConfigurationException($e->getMessage(), $e);
204 }
205 }
206
207 throw new ConfigurationException(sprintf('Broker "%s" could not be initialized', $brokerCode));
208 }
209}
static includeModule($moduleName)
Определения loader.php:67
getDbBrokerTableClass(array $brokerConfig, string $brokerName)
Определения BrokerManager.php:152
static getInstalledModules()
Определения modulemanager.php:9
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$moduleId
$modules
Определения bitrix.php:26
$name
Определения menu_edit.php:35
$settings
Определения product_settings.php:43
$config
Определения quickway.php:69