Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
callback.php
1<?php
3
7
16{
26 public static function __callStatic($name, $arguments)
27 {
28 $logger = LoggerManager::getInstance()->getLogger();
29 $event = Sender::parseEventName($name);
30 if ($logger)
31 {
32 $logger->debug(
33 "\n{delimiter}\n"
34 . "{date} - {host}\n{delimiter}\n"
35 . "Event {eventName} starts. \n{delimiter}\n"
36 . "{arguments}",
37 [
38 'eventName' => $event['EVENT'],
39 'arguments' => $arguments,
40 ]
41 );
42 }
43
44 $provider = new \CRestProvider();
45 $description = $provider->getDescription();
46
47 foreach($description as $scope => $scopeMethods)
48 {
49 if(
50 array_key_exists(\CRestUtil::EVENTS, $scopeMethods)
51 && is_array($scopeMethods[\CRestUtil::EVENTS])
52 )
53 {
54 foreach($scopeMethods[\CRestUtil::EVENTS] as $key => $restEvent)
55 {
56 if($restEvent[0] == $event['MODULE_ID'] && toUpper($restEvent[1]) == $event['EVENT'])
57 {
58 $event['EVENT_REST'] = array(
59 'EVENT' => $key,
60 'HANDLER' => $restEvent[2],
61 'ADDITIONAL' => array(),
62 );
63
64 if(isset($restEvent[3]) && is_array($restEvent[3]))
65 {
66 $event['EVENT_REST']['ADDITIONAL'] = $restEvent[3];
67 }
68
69 break;
70 }
71 }
72 }
73
74 if(array_key_exists('EVENT_REST', $event))
75 {
76 break;
77 }
78 }
79
80 $handlerFound = false;
81 $appHoldExceptId = 0;
82 if (!empty($arguments[1]['REST_EVENT_HOLD_EXCEPT_APP']))
83 {
84 $app = AppTable::getByClientId($arguments[1]['REST_EVENT_HOLD_EXCEPT_APP']);
85 if ($app['ID'] > 0)
86 {
87 $appHoldExceptId = $app['ID'];
88 }
89 }
90
91 if(array_key_exists('EVENT_REST', $event))
92 {
93 $filter = [
94 '=EVENT_NAME' => mb_strtoupper($event['EVENT_REST']['EVENT']),
95 ];
96 if ($appHoldExceptId > 0)
97 {
98 $filter['=APP_ID'] = $appHoldExceptId;
99 }
100
101 $dbRes = EventTable::getList(
102 [
103 'filter' => $filter,
104 'select' => [
105 '*',
106 'APP_CODE' => 'REST_APP.CLIENT_ID',
107 'APP_ACTIVE' => 'REST_APP.ACTIVE',
108 'APP_INSTALLED' => 'REST_APP.INSTALLED',
109 ],
110 ]
111 );
112
113 $dataProcessed = !is_array($event['EVENT_REST']['HANDLER']) || !is_callable($event['EVENT_REST']['HANDLER']);
114 $call = array();
115 while ($handler = $dbRes->fetch())
116 {
117 if (!empty($handler['APP_CODE']))
118 {
119 if (
120 $handler['APP_ACTIVE'] !== AppTable::ACTIVE
121 || $handler['APP_INSTALLED'] !== AppTable::INSTALLED
122 )
123 {
124 if ($logger)
125 {
126 $logger->error(
127 "\n{delimiter}\n"
128 . "{date} - {host}\n{delimiter}\n"
129 . "Event {eventName} skipped because inactive app: \n"
130 . "{handler}",
131 [
132 'eventName' => $event['EVENT'],
133 'handler' => $handler,
134 ]
135 );
136 }
137
138 continue;
139 }
140
141 $appStatus = AppTable::getAppStatusInfo($handler['APP_CODE'], '');
142 if ($appStatus['PAYMENT_EXPIRED'] === 'Y')
143 {
144 if ($logger)
145 {
146 $logger->error(
147 "\n{delimiter}\n"
148 . "{date} - {host}\n{delimiter}\n"
149 . "Event {eventName} skipped because PAYMENT_EXPIRED: \n"
150 . "{appStatus}",
151 [
152 'eventName' => $event['EVENT'],
153 'appStatus' => $appStatus,
154 ]
155 );
156 }
157
158 continue;
159 }
160 }
161
162 $handlerArguments = $arguments;
163 $handlerFound = true;
164
165 if(!$dataProcessed)
166 {
167 try
168 {
169 $handlerArguments = call_user_func_array($event['EVENT_REST']['HANDLER'], array($handlerArguments, $handler));
170 $call[] = array($handler, $handlerArguments, $event['EVENT_REST']['ADDITIONAL']);
171 }
172 catch(\Exception $e)
173 {
174 if ($logger)
175 {
176 $logger->error(
177 "\n{delimiter}\n"
178 . "{date} - {host}\n{delimiter}\n"
179 . "Event {eventName} exception: \n"
180 . "{errorCode}: {errorMessage}",
181 [
182 'eventName' => $event['EVENT'],
183 'errorCode' => $e->getCode(),
184 'errorMessage' => $e->getMessage(),
185 ]
186 );
187 }
188 }
189 }
190 else
191 {
192 $call[] = array($handler, $handlerArguments, $event['EVENT_REST']['ADDITIONAL']);
193 }
194 }
195
196 if(count($call) > 0)
197 {
198 Sender::call($call);
199 }
200 }
201
202 if(!$handlerFound)
203 {
204 Sender::unbind($event['MODULE_ID'], $event['EVENT']);
205 }
206 }
207}
static getAppStatusInfo($app, $detailUrl)
Definition app.php:675
static getByClientId($clientId)
Definition app.php:929
static __callStatic($name, $arguments)
Definition callback.php:26
static parseEventName($name)
Definition sender.php:52
static call($handlersList)
Definition sender.php:153
static unbind($moduleId, $eventName)
Definition sender.php:81