1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
eventbind.php
См. документацию.
1<?php
2
3namespace Bitrix\Rest\Event;
4
5use Bitrix\Main\Engine\Response\Converter;
6use Bitrix\Main\IO\Path;
7use Bitrix\Main\NotImplementedException;
8use Bitrix\Main\ORM\Event;
9use Bitrix\Rest\RestException;
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}
$arParams
Определения access_dialog.php:21
getHandlers(array $names)
Определения eventbind.php:41
static processItemEvent(array $arParams, array $arHandler)
Определения eventbind.php:128
__construct(string $class)
Определения eventbind.php:21
const CATEGORY_CRM
Определения sqs.php:14
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$name
Определения menu_edit.php:35
$event
Определения prolog_after.php:141