Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
buildermanager.php
1<?php
2
4
6
8{
9 public const EVENT_ID = 'onGetUrlBuilders';
10
11 private static self $instance;
12
14 protected array $builders;
15
17 protected array $map;
18
19 protected function __construct()
20 {
21 $this->builders = [];
22 $this->map = [];
23 $counter = 0;
24
25 $item = new IblockBuilder();
26 $id = $item->getId();
27
28 $this->builders[$id] = $item;
29 $this->map[] = [
30 'ID' => $id,
31 'WEIGHT' => (int)$item->getWeight(),
32 'COUNTER' => $counter,
33 ];
34 $counter++;
35 unset($item);
36
37 $event = new Main\Event('iblock', self::EVENT_ID, []);
38 $event->send();
39 $resultList = $event->getResults();
40 if (empty($resultList) || !is_array($resultList))
41 {
42 return;
43 }
44 foreach ($resultList as $eventResult)
45 {
46 if ($eventResult->getType() !== Main\EventResult::SUCCESS)
47 {
48 continue;
49 }
50 $row = $eventResult->getParameters();
51 if (empty($row) || !is_array($row))
52 {
53 continue;
54 }
55 foreach ($row as $className)
56 {
57 if (!is_string($className) || $className === '')
58 {
59 continue;
60 }
61 if (!class_exists($className))
62 {
63 continue;
64 }
66 $item = new $className();
67 if ($item instanceof BaseBuilder)
68 {
69 $id = $item->getId();
70 if (!isset($this->builders[$id]))
71 {
72 $this->builders[$id] = $item;
73 $this->map[] = [
74 'ID' => $id,
75 'WEIGHT' => (int)$item->getWeight(),
76 'COUNTER' => $counter,
77 ];
78 $counter++;
79 }
80 }
81 unset($item);
82 }
83 }
84 unset($eventResult, $resultList);
85
86 if (!empty($this->map))
87 {
88 Main\Type\Collection::sortByColumn(
89 $this->map,
90 [
91 'WEIGHT' => SORT_DESC,
92 'COUNTER' => SORT_ASC,
93 ]
94 );
95 }
96 }
97
98 public static function getInstance(): BuilderManager
99 {
100 if (!isset(self::$instance))
101 {
102 self::$instance = new BuilderManager();
103 }
104
105 return self::$instance;
106 }
107
108 public function getBuilder(string $builder = BaseBuilder::TYPE_AUTODETECT): ?BaseBuilder
109 {
110 $result = null;
111 if ($builder === BaseBuilder::TYPE_AUTODETECT)
112 {
113 if (defined('URL_BUILDER_TYPE') && is_string(URL_BUILDER_TYPE))
114 {
115 if (isset($this->builders[URL_BUILDER_TYPE]))
116 {
117 $result = $this->builders[URL_BUILDER_TYPE];
118 }
119 }
120 if ($result === null)
121 {
122 foreach ($this->map as $row)
123 {
124 if ($this->builders[$row['ID']]->use())
125 {
126 $result = $this->builders[$row['ID']];
127 break;
128 }
129 }
130 unset($row);
131 }
132 }
133 else
134 {
135 if (isset($this->builders[$builder]))
136 {
137 $result = $this->builders[$builder];
138 }
139 }
140
141 return $result;
142 }
143}
getBuilder(string $builder=BaseBuilder::TYPE_AUTODETECT)