Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
registry.php
1<?php
2
4
7
11final class Registry
12{
13 const EVENT_NAME = 'onGetUserFieldTypeFactory';
14
16 protected $factories = [];
17 protected $itemTypes = [];
18
20 private static $instance;
21
22 private function __construct()
23 {
24 $this->addFactoriesByEvent();
25 }
26
27 private function __clone()
28 {
29 }
30
35 public static function getInstance(): Registry
36 {
37 if (!isset(self::$instance))
38 {
39 self::$instance = new Registry;
40 }
41
42 return self::$instance;
43 }
44
45 public function registerFactory(TypeFactory $factory)
46 {
47 $this->factories[$factory->getCode()] = $factory;
48 }
49
54 public function registerTypeByEntity(Entity $entity, array $type): void
55 {
56 $this->itemTypes[$entity->getName()] = $type;
57 }
58
63 public function getTypeByEntity(Entity $entity): ?array
64 {
65 $entityName = $entity->getName();
66 if(isset($this->itemTypes[$entityName]))
67 {
68 return $this->itemTypes[$entityName];
69 }
70
71 return null;
72 }
73
74 public function getFactoryByCode(string $code): ?TypeFactory
75 {
76 if(isset($this->factories[$code]))
77 {
78 return $this->factories[$code];
79 }
80
81 return null;
82 }
83
84 public function getFactoryByTypeDataClass($typeDataClass): ?TypeFactory
85 {
86 foreach($this->factories as $factory)
87 {
88 if($factory->getTypeDataClass() == $typeDataClass)
89 {
90 return $factory;
91 }
92 }
93
94 return null;
95 }
96
101 public function getUserFieldEntityIdByItemEntity(Entity $entity): ?string
102 {
103 $type = $this->getTypeByEntity($entity);
104 if($type && $type['code'])
105 {
106 $factory = $this->getFactoryByCode($type['code']);
107 if($factory)
108 {
109 return $factory->getUserFieldEntityId($type['ID']);
110 }
111 }
112
113 return null;
114 }
115
116 protected function addFactoriesByEvent(): void
117 {
118 foreach(EventManager::getInstance()->findEventHandlers('main', static::EVENT_NAME) as $handler)
119 {
120 $eventResult = ExecuteModuleEventEx($handler);
121 if(is_array($eventResult))
122 {
123 foreach($eventResult as $factory)
124 {
125 if($factory instanceof TypeFactory)
126 {
127 $this->registerFactory($factory);
128 }
129 }
130 }
131 }
132 }
133}
getUserFieldEntityIdByItemEntity(Entity $entity)
Definition registry.php:101
registerFactory(TypeFactory $factory)
Definition registry.php:45
registerTypeByEntity(Entity $entity, array $type)
Definition registry.php:54