Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventbind.php
1<?php
2
3namespace Bitrix\Rest\Event;
4
10
12{
14 private string $class;
15
21 public function __construct(string $class)
22 {
23 $reflection = new \ReflectionClass($class);
24 if ($reflection->implementsInterface('\\Bitrix\\Rest\\Event\\EventBindInterface'))
25 {
26 $this->class = $class;
27 }
28 else
29 {
30 throw new NotImplementedException($class.' is not implemented interface EventBindInterface');
31 }
32 }
33
41 public function getHandlers(array $names): array
42 {
43 $bindings = [];
44 $eventNames = $this->bind($names);
45
46 foreach ($eventNames as $internalName => $externalName)
47 {
48 $bindings[$externalName] = $this->getItemEventInfo($internalName, $this->class::getCallbackRestEvent());
49 }
50
51 return $bindings;
52 }
53
61 private function bind(array $eventNames): array
62 {
63 $result = [];
64
65 foreach ($eventNames as $internalName => $externalName)
66 {
67 $isAssociativeArray = !is_numeric($internalName);
68 if ($isAssociativeArray)
69 {
70 $result[$internalName] = $externalName;
71 }
72 else
73 {
74 $internalName = $externalName;
75 $result[$internalName] = $this->makeExternalEventName($externalName);
76 }
77 }
78
79 return $result;
80 }
81
82 private function getItemEventInfo(string $eventName, array $callback): array
83 {
84 return [
85 $this->getModuleId(),
86 $eventName,
87 $callback,
88 [
89 'category' => \Bitrix\Rest\Sqs::CATEGORY_CRM,
90 ],
91 ];
92 }
93
94 private function makeExternalEventName(string $eventName): string
95 {
96 $converter = new Converter(Converter::TO_SNAKE);
97 $eventName = str_replace('::', '\\', $eventName);
98 $eventName = str_replace('\\', '', $eventName);
99 $name = $converter->process($eventName);
100
101 return str_replace('_','.', $this->getModuleId().'_'.$name);
102 }
103
108 private function getFilePath(string $class): string
109 {
110 $reflector = new \ReflectionClass($class);
111 return Path::normalize($reflector->getFileName());
112 }
113
114 private function getModuleId(): string
115 {
116 return getModuleId($this->getFilePath($this->class));
117 }
118
128 public static function processItemEvent(array $arParams, array $arHandler): array
129 {
130 $id = null;
131 $event = $arParams[0] ?? null;
132
133 if (!$event)
134 {
135 throw new RestException('event object not found trying to process event');
136 }
137
138 if ($event instanceof Event)
139 {
140 $item = $event->getParameter('id');
141 $id = is_array($item) ? $item['ID']: $item;
142 }
143
144 if (!$id)
145 {
146 throw new RestException('id not found trying to process event');
147 }
148
149 return [
150 'FIELDS' => [
151 'ID' => $id
152 ],
153 ];
154 }
155}
static processItemEvent(array $arParams, array $arHandler)
__construct(string $class)
Definition eventbind.php:21
const CATEGORY_CRM
Definition sqs.php:14