Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
provideroffline.php
1<?php
2namespace Bitrix\Rest\Event;
3
10
12{
16 protected static $instance = null;
17
18 private array $eventList = [];
19 private bool $isFinaliseInit = false;
20
21 public static function instance(): ProviderOffline
22 {
23 if (static::$instance === null)
24 {
25 static::$instance = new static();
26 }
27
28 return static::$instance;
29 }
30
31 public function send(array $eventList)
32 {
33 $this->eventList = array_merge($this->eventList, $eventList);
34 $this->registerFinalize();
35 }
36
40 public function registerFinalize(): void
41 {
42 if (!$this->isFinaliseInit)
43 {
44 $this->isFinaliseInit = true;
45 Application::getInstance()->addBackgroundJob([__CLASS__, 'runFinalize']);
46 }
47 }
48
52 public static function runFinalize(): void
53 {
54 $instance = static::instance();
55 $instance->finalize();
56 }
57
61 public function finalize(): void
62 {
63 $serverAuthData = $this->getServerAuthData();
64
65 $offlineEventsCount = [];
66 $offlineEventsApp = [];
67
68 foreach ($this->eventList as $item)
69 {
70 $application = $item['APPLICATION'];
71 $handler = $item['HANDLER'];
72
73 if (
74 $serverAuthData['client_id'] !== $application['CLIENT_ID']
75 || $serverAuthData['auth_connector'] !== $handler['CONNECTOR_ID']
76 )
77 {
78 if (!isset($offlineEventsCount[$application['CLIENT_ID']]))
79 {
80 $offlineEventsCount[$application['CLIENT_ID']] = [];
81 }
82
83 if (!isset($offlineEventsCount[$application['CLIENT_ID']][$handler['CONNECTOR_ID']]))
84 {
85 $offlineEventsCount[$application['CLIENT_ID']][$handler['CONNECTOR_ID']] = 0;
86 }
87
89 [
90 'APP_ID' => $application['ID'],
91 'EVENT_NAME' => $handler['EVENT_NAME'],
92 'EVENT_DATA' => $item['DATA'],
93 'EVENT_ADDITIONAL' => $item['AUTH'],
94 'CONNECTOR_ID' => $handler['CONNECTOR_ID'],
95 ]
96 );
97
98 $offlineEventsCount[$application['CLIENT_ID']][$handler['CONNECTOR_ID']]++;
99 $offlineEventsApp[$application['ID']] = true;
100 }
101 else
102 {
103 $logger = LoggerManager::getInstance()->getLogger();
104 if ($logger)
105 {
106 $logger->debug(
107 "\n{delimiter}\n"
108 . "{date} - {host}\n{delimiter}\n"
109 . "Event skipped because initializer is current application. \n"
110 . "auth: {serverAuthData}"
111 . "app: {application}\n",
112 [
113 'serverAuthData' => $serverAuthData,
114 'application' => $application,
115 ]
116 );
117 }
118 }
119 }
120
121 if (!empty($offlineEventsCount))
122 {
123 $this->notifyApplications($offlineEventsCount);
124 }
125
126 if (!empty($offlineEventsApp))
127 {
128 $this->sendOfflineEvent(array_keys($offlineEventsApp));
129 }
130 }
131
132 protected function getServerAuthData()
133 {
134 $server = \CRestServer::instance();
135 $serverAuthData = array('auth_connector' => '', 'client_id' => '');
136 if($server !== null)
137 {
138 $serverAuthData = $server->getAuthData();
139 if(!isset($serverAuthData['auth_connector']))
140 {
141 $serverAuthData['auth_connector'] = '';
142 }
143
144 $serverAuthData['client_id'] = $server->getClientId();
145 }
146
147 return $serverAuthData;
148 }
149
150 protected function sendOfflineEvent(array $appList)
151 {
152 foreach (EventManager::getInstance()->findEventHandlers(
153 "rest",
154 "onAfterOfflineEventCall"
155 ) as $event)
156 {
157 ExecuteModuleEventEx($event, [['APP_LIST' => $appList]]);
158 }
159 }
160
161 protected function notifyApplications(array $counters)
162 {
163 foreach($counters as $clientId => $connectorCounters)
164 {
165 if(is_array($connectorCounters) && !empty($connectorCounters))
166 {
167 $this->notifyApplication($clientId, $connectorCounters);
168 }
169 }
170 }
171
172 protected function notifyApplication($clientId, array $connectorCounters)
173 {
174 if(Loader::includeModule('pull'))
175 {
176 $eventParam = array();
177
178 foreach($connectorCounters as $connectorId => $count)
179 {
180 $eventParam[] = array(
181 'connector_id' => $connectorId,
182 'count' => $count
183 );
184 }
185
186 Pull\Event::add(Pull\Event::SHARED_CHANNEL, array(
187 'module_id' => 'rest',
188 'command' => 'event_offline',
189 'params' => $eventParam,
190 ), $clientId);
191 }
192 }
193}
notifyApplication($clientId, array $connectorCounters)