16 protected const CACHE_ID =
'b_module_to_module';
35 if (!isset(self::$instance))
38 self::$instance =
new $c;
60 'FROM_MODULE_ID' => $fromModuleId,
61 'MESSAGE_ID' => $eventType,
62 'CALLBACK' => $callback,
64 'FULL_PATH' => $includeFile,
65 'VERSION' => $version,
69 $fromModuleId = strtoupper($fromModuleId);
70 $eventType = strtoupper($eventType);
72 if (!isset($this->handlers[$fromModuleId]) || !is_array($this->handlers[$fromModuleId]))
74 $this->handlers[$fromModuleId] = [];
77 $arEvents = &$this->handlers[$fromModuleId];
79 if (empty($arEvents[$eventType]) || !is_array($arEvents[$eventType]))
81 $arEvents[$eventType] = [$arEvent];
82 $iEventHandlerKey = 0;
87 $iEventHandlerKey = max(array_keys($arEvents[$eventType])) + 1;
89 foreach ($arEvents[$eventType] as $key => $value)
91 if ($value[
'SORT'] > $arEvent[
'SORT'])
93 $newEvents[$iEventHandlerKey] = $arEvent;
96 $newEvents[$key] = $value;
98 $newEvents[$iEventHandlerKey] = $arEvent;
99 $arEvents[$eventType] = $newEvents;
102 return $iEventHandlerKey;
105 public function addEventHandler($fromModuleId, $eventType, $callback, $includeFile =
false, $sort = 100)
125 $fromModuleId = strtoupper($fromModuleId);
126 $eventType = strtoupper($eventType);
128 if (isset($this->handlers[$fromModuleId][$eventType][$iEventHandlerKey]))
130 unset($this->handlers[$fromModuleId][$eventType][$iEventHandlerKey]);
137 public function unRegisterEventHandler($fromModuleId, $eventType, $toModuleId, $toClass =
'', $toMethod =
'', $toPath =
'', $toMethodArg = [])
139 $toMethodArg = (!is_array($toMethodArg) || empty($toMethodArg) ?
'' : serialize($toMethodArg));
142 $sqlHelper = $con->getSqlHelper();
145 "DELETE FROM b_module_to_module " .
146 "WHERE FROM_MODULE_ID='" . $sqlHelper->forSql($fromModuleId) .
"'" .
147 " AND MESSAGE_ID='" . $sqlHelper->forSql($eventType) .
"' " .
148 " AND TO_MODULE_ID='" . $sqlHelper->forSql($toModuleId) .
"' " .
149 (($toClass !=
'') ?
" AND TO_CLASS='" . $sqlHelper->forSql($toClass) .
"' " :
" AND (TO_CLASS='' OR TO_CLASS IS NULL) ") .
150 (($toMethod !=
'') ?
" AND TO_METHOD='" . $sqlHelper->forSql($toMethod) .
"'" :
" AND (TO_METHOD='' OR TO_METHOD IS NULL) ") .
151 (($toPath !=
'' && $toPath !== 1) ?
" AND TO_PATH='" . $sqlHelper->forSql($toPath) .
"'" :
" AND (TO_PATH='' OR TO_PATH IS NULL) ") .
152 (($toMethodArg !=
'') ?
" AND TO_METHOD_ARG='" . $sqlHelper->forSql($toMethodArg) .
"'" :
" AND (TO_METHOD_ARG='' OR TO_METHOD_ARG IS NULL) ");
154 $con->queryExecute($strSql);
159 public function registerEventHandler($fromModuleId, $eventType, $toModuleId, $toClass =
'', $toMethod =
'', $sort = 100, $toPath =
'', $toMethodArg = [])
164 public function registerEventHandlerCompatible($fromModuleId, $eventType, $toModuleId, $toClass =
'', $toMethod =
'', $sort = 100, $toPath =
'', $toMethodArg = [])
171 $toMethodArg = (!is_array($toMethodArg) || empty($toMethodArg) ?
'' : serialize($toMethodArg));
172 $sort = intval($sort);
173 $version = intval($version);
175 $uniqueID = md5(mb_strtolower($fromModuleId .
'.' . $eventType .
'.' . $toModuleId .
'.' . $toPath .
'.' . $toClass .
'.' . $toMethod .
'.' . $toMethodArg .
'.' . $version));
178 $sqlHelper = $connection->getSqlHelper();
180 $fromModuleId = $sqlHelper->forSql($fromModuleId);
181 $eventType = $sqlHelper->forSql($eventType);
182 $toModuleId = $sqlHelper->forSql($toModuleId);
183 $toClass = $sqlHelper->forSql($toClass);
184 $toMethod = $sqlHelper->forSql($toMethod);
185 $toPath = $sqlHelper->forSql($toPath);
186 $toMethodArg = $sqlHelper->forSql($toMethodArg);
188 $fields =
'(SORT, FROM_MODULE_ID, MESSAGE_ID, TO_MODULE_ID, TO_CLASS, TO_METHOD, TO_PATH, TO_METHOD_ARG, VERSION, UNIQUE_ID)';
189 $values =
"(" . $sort .
", '" . $fromModuleId .
"', '" . $eventType .
"', '" . $toModuleId .
"', " .
" '" . $toClass .
"', '" . $toMethod .
"', '" . $toPath .
"', '" . $toMethodArg .
"', " . $version .
", '" . $uniqueID .
"')";
190 $sql = $sqlHelper->getInsertIgnore(
'b_module_to_module', $fields,
'VALUES ' . $values);
191 $connection->queryExecute($sql);
199 if (isset($arEvent[
'CALLBACK']))
201 if (is_array($arEvent[
'CALLBACK']))
203 $strName .= (is_object($arEvent[
'CALLBACK'][0]) ? get_class($arEvent[
'CALLBACK'][0]) : $arEvent[
'CALLBACK'][0]) .
'::' . $arEvent[
'CALLBACK'][1];
205 elseif (is_callable($arEvent[
'CALLBACK']))
207 $strName .=
'callable';
211 $strName .= $arEvent[
'CALLBACK'];
216 $strName .= $arEvent[
'TO_CLASS'] .
'::' . $arEvent[
'TO_METHOD'];
218 if (!empty($arEvent[
'TO_MODULE_ID']))
220 $strName .=
' (' . $arEvent[
'TO_MODULE_ID'] .
')';
229 if ($cache->read(3600, self::CACHE_ID, self::CACHE_ID))
231 $rawEvents = $cache->get(self::CACHE_ID);
233 if (!is_array($rawEvents))
243 SELECT FROM_MODULE_ID, MESSAGE_ID, SORT, TO_MODULE_ID, TO_PATH,
244 TO_CLASS, TO_METHOD, TO_METHOD_ARG, VERSION
245 FROM b_module_to_module m2m
246 INNER JOIN b_module m ON (m2m.TO_MODULE_ID = m.ID)
250 $rawEvents = $rs->fetchAll();
252 $cache->set(self::CACHE_ID, $rawEvents);
256 $hasHandlers = !empty($this->handlers);
258 foreach ($rawEvents as $ar)
261 'TO_MODULE_ID' => $ar[
'TO_MODULE_ID'],
262 'TO_CLASS' => $ar[
'TO_CLASS'],
263 'TO_METHOD' => $ar[
'TO_METHOD'],
265 $ar[
'FROM_MODULE_ID'] = strtoupper($ar[
'FROM_MODULE_ID']);
266 $ar[
'MESSAGE_ID'] = strtoupper($ar[
'MESSAGE_ID']);
267 if ($ar[
'TO_METHOD_ARG'] !=
'')
269 $ar[
'TO_METHOD_ARG'] = unserialize($ar[
'TO_METHOD_ARG'], [
'allowed_classes' =>
false]);
273 $ar[
'TO_METHOD_ARG'] = [];
276 $this->handlers[$ar[
'FROM_MODULE_ID']][$ar[
'MESSAGE_ID']][] = [
277 'SORT' => (int)$ar[
'SORT'],
278 'TO_MODULE_ID' => $ar[
'TO_MODULE_ID'],
279 'TO_PATH' => $ar[
'TO_PATH'],
280 'TO_CLASS' => $ar[
'TO_CLASS'],
281 'TO_METHOD' => $ar[
'TO_METHOD'],
282 'TO_METHOD_ARG' => $ar[
'TO_METHOD_ARG'],
283 'VERSION' => $ar[
'VERSION'],
284 'TO_NAME' => $ar[
'TO_NAME'],
292 foreach (array_keys(
$handlers) as $moduleId)
294 foreach (array_keys(
$handlers[$moduleId]) as $event)
296 Collection::sortByColumn(
297 $this->handlers[$moduleId][$event],
298 [
'SORT' => SORT_ASC],
307 $this->isHandlersLoaded =
true;
313 $managedCache->clean(self::CACHE_ID, self::CACHE_ID);
315 foreach ($this->handlers as $module => $types)
317 foreach ($types as $type => $events)
319 foreach ($events as $i => $event)
321 if (isset($event[
'FROM_DB']) && $event[
'FROM_DB'])
323 unset($this->handlers[$module][$type][$i]);
328 $this->isHandlersLoaded =
false;
333 if (!$this->isHandlersLoaded)
338 $eventModuleId = strtoupper($eventModuleId);
339 $eventType = strtoupper($eventType);
341 if (!isset($this->handlers[$eventModuleId]) || !isset($this->handlers[$eventModuleId][$eventType]))
346 $handlers = $this->handlers[$eventModuleId][$eventType];
352 if (is_array($filter) && !empty($filter))
356 foreach ($handlersTmp as $handler)
358 if (isset($handler[
'TO_MODULE_ID']) && in_array($handler[
'TO_MODULE_ID'], $filter))
382 $includeResult =
true;
386 if (!empty($handler[
'TO_MODULE_ID']) && ($handler[
'TO_MODULE_ID'] !=
'main'))
390 elseif (!empty($handler[
'TO_PATH']))
392 $path = ltrim($handler[
'TO_PATH'],
'/');
395 $includeResult = include_once($path);
398 elseif (!empty($handler[
'FULL_PATH']) &&
IO\File::isFileExists($handler[
'FULL_PATH']))
400 $includeResult = include_once($handler[
'FULL_PATH']);
407 if (!empty($handler[
'TO_METHOD_ARG']) && is_array($handler[
'TO_METHOD_ARG']))
409 $args = $handler[
'TO_METHOD_ARG'];
416 if ($handler[
'VERSION'] > 1)
422 $args = array_merge($args, array_values($event->
getParameters()));
426 if (isset($handler[
'CALLBACK']))
428 $callback = $handler[
'CALLBACK'];
430 elseif (!empty($handler[
'TO_CLASS']) && !empty($handler[
'TO_METHOD']) && class_exists($handler[
'TO_CLASS']))
432 $callback = [$handler[
'TO_CLASS'], $handler[
'TO_METHOD']];
435 if ($callback !=
null)
437 $result = call_user_func_array($callback, $args);
441 $result = $includeResult;
444 if (($result !=
null) && !($result instanceof
EventResult))
446 $result =
new EventResult(EventResult::UNDEFINED, $result, $handler[
'TO_MODULE_ID'] ??
null);
457 catch (\Exception $ex)
static getConnection($name="")
addException(\Exception $exception)
addResult(EventResult $result)
registerEventHandler($fromModuleId, $eventType, $toModuleId, $toClass='', $toMethod='', $sort=100, $toPath='', $toMethodArg=[])
formatEventName($arEvent)
addEventHandlerInternal($fromModuleId, $eventType, $callback, $includeFile, $sort, $version)
registerEventHandlerCompatible($fromModuleId, $eventType, $toModuleId, $toClass='', $toMethod='', $sort=100, $toPath='', $toMethodArg=[])
addEventHandlerCompatible($fromModuleId, $eventType, $callback, $includeFile=false, $sort=100)
removeEventHandler($fromModuleId, $eventType, $iEventHandlerKey)
sendToEventHandler(array $handler, Event $event)
addEventHandler($fromModuleId, $eventType, $callback, $includeFile=false, $sort=100)
unRegisterEventHandler($fromModuleId, $eventType, $toModuleId, $toClass='', $toMethod='', $toPath='', $toMethodArg=[])
registerEventHandlerInternal($fromModuleId, $eventType, $toModuleId, $toClass, $toMethod, $sort, $toPath, $toMethodArg, $version)
static setInstance($instance)
findEventHandlers($eventModuleId, $eventType, array $filter=null)
static includeModule($moduleName)
static getLocal($path, $root=null)