Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventmanager.php
1<?php
9namespace Bitrix\Main\ORM;
10
14
22{
26 protected static $instance;
27
31 protected function __construct()
32 {
33 }
34
41 public static function getInstance()
42 {
43 if (!isset(self::$instance))
44 {
45 $c = __CLASS__;
46 self::$instance = new $c;
47 }
48
49 return self::$instance;
50 }
51
63 public function addEventHandler($entity, $eventType, $callback, $includeFile = false, $sort = 100)
64 {
65 $entity = static::obtainEntity($entity);
66 $eventType = static::obtainEventType($entity, $eventType);
67
68 // subscribe
69 return \Bitrix\Main\EventManager::getInstance()
70 ->addEventHandler($entity->getModule(), $eventType, $callback, $includeFile, $sort);
71 }
72
82 public function removeEventHandler($entity, $eventType, $iEventHandlerKey)
83 {
84 $entity = static::obtainEntity($entity);
85 $eventType = static::obtainEventType($entity, $eventType);
86
87 // unsubscribe
88 return \Bitrix\Main\EventManager::getInstance()
89 ->removeEventHandler($entity->getModule(), $eventType, $iEventHandlerKey);
90 }
91
105 public function registerEventHandler($entity, $eventType, $toModuleId, $toClass = "", $toMethod = "", $sort = 100, $toPath = "", $toMethodArg = [])
106 {
107 $entity = static::obtainEntity($entity);
108 $eventType = static::obtainEventType($entity, $eventType);
109
110 // subscribe
111 \Bitrix\Main\EventManager::getInstance()
112 ->registerEventHandler($entity->getModule(), $eventType, $toModuleId, $toClass, $toMethod, $sort, $toPath, $toMethodArg);
113 }
114
127 public function unRegisterEventHandler($entity, $eventType, $toModuleId, $toClass = "", $toMethod = "", $toPath = "", $toMethodArg = [])
128 {
129 $entity = static::obtainEntity($entity);
130 $eventType = static::obtainEventType($entity, $eventType);
131
132 // unsubscribe
133 \Bitrix\Main\EventManager::getInstance()
134 ->unRegisterEventHandler($entity->getModule(), $eventType, $toModuleId, $toClass, $toMethod, $toPath, $toMethodArg);
135 }
136
144 protected static function obtainEntity($entity)
145 {
146 // define entity
147 if ($entity instanceof Entity)
148 {
149 // ok
150 }
151 elseif (is_subclass_of($entity, DataManager::class))
152 {
153 $entity = $entity::getEntity();
154 }
155 elseif (is_subclass_of($entity, EntityObject::class))
156 {
157 $dataClass = $entity::$dataClass;
158 $entity = $dataClass::getEntity();
159 }
160 else
161 {
162 throw new ArgumentException('Unknown entity value');
163 }
164
165 return $entity;
166 }
167
175 protected static function obtainEventType($entity, $eventType)
176 {
177 if (!in_array($eventType, [
178 DataManager::EVENT_ON_BEFORE_ADD,
179 DataManager::EVENT_ON_ADD,
180 DataManager::EVENT_ON_AFTER_ADD,
181 DataManager::EVENT_ON_BEFORE_UPDATE,
182 DataManager::EVENT_ON_UPDATE,
183 DataManager::EVENT_ON_AFTER_UPDATE,
184 DataManager::EVENT_ON_BEFORE_DELETE,
185 DataManager::EVENT_ON_DELETE,
186 DataManager::EVENT_ON_AFTER_DELETE,
187 ], true))
188 {
189 throw new ArgumentException("Unknown event type `{$eventType}`");
190 }
191
192 return $entity->getNamespace() . $entity->getName() . '::' . $eventType;
193 }
194}
static obtainEventType($entity, $eventType)
unRegisterEventHandler($entity, $eventType, $toModuleId, $toClass="", $toMethod="", $toPath="", $toMethodArg=[])
addEventHandler($entity, $eventType, $callback, $includeFile=false, $sort=100)
registerEventHandler($entity, $eventType, $toModuleId, $toClass="", $toMethod="", $sort=100, $toPath="", $toMethodArg=[])
removeEventHandler($entity, $eventType, $iEventHandlerKey)