1 <?php
2
3 4 5 6 7 8 9
10 class rex_clang
11 {
12 private static $cacheLoaded = false;
13 private static $clangs = [];
14 private static $currentId = 1;
15
16 private $id;
17 private $code;
18 private $name;
19 private $priority;
20 private $status;
21
22 private function __construct()
23 {
24 }
25
26 27 28 29 30 31 32
33 public static function exists($id)
34 {
35 self::checkCache();
36 return isset(self::$clangs[$id]);
37 }
38
39 40 41 42 43 44 45
46 public static function get($id)
47 {
48 if (self::exists($id)) {
49 return self::$clangs[$id];
50 }
51 return null;
52 }
53
54 55 56 57 58
59 public static function getStartId()
60 {
61 foreach (self::getAll() as $id => $clang) {
62 return $id;
63 }
64 }
65
66 67 68 69 70
71 public static function getCurrent()
72 {
73 return self::get(self::getCurrentId());
74 }
75
76 77 78 79 80
81 public static function getCurrentId()
82 {
83 return self::$currentId;
84 }
85
86 87 88 89 90 91 92
93 public static function setCurrentId($id)
94 {
95 if (!self::exists($id)) {
96 throw new rex_exception('Clang id "' . $id . '" doesn\'t exist');
97 }
98 self::$currentId = (int) $id;
99 }
100
101 102 103 104 105
106 public function getId()
107 {
108 return (int) $this->id;
109 }
110
111 112 113 114 115
116 public function getCode()
117 {
118 return $this->code;
119 }
120
121 122 123 124 125
126 public function getName()
127 {
128 return $this->name;
129 }
130
131 132 133 134 135
136 public function getPriority()
137 {
138 return $this->priority;
139 }
140
141 142 143 144 145
146 public function isOnline()
147 {
148 return $this->status;
149 }
150
151 152 153 154 155 156 157
158 public function hasValue($value)
159 {
160 return isset($this->$value) || isset($this->{'clang_' . $value});
161 }
162
163 164 165 166 167 168 169
170 public function getValue($value)
171 {
172 if (isset($this->$value)) {
173 return $this->$value;
174 }
175
176 if (isset($this->{'clang_' . $value})) {
177 return $this->{'clang_' . $value};
178 }
179
180 return null;
181 }
182
183 184 185 186 187 188 189
190 public static function count($ignoreOfflines = false)
191 {
192 self::checkCache();
193 return count(self::getAll($ignoreOfflines));
194 }
195
196 197 198 199 200 201 202
203 public static function getAllIds($ignoreOfflines = false)
204 {
205 self::checkCache();
206 return array_keys(self::getAll($ignoreOfflines));
207 }
208
209 210 211 212 213 214 215
216 public static function getAll($ignoreOfflines = false)
217 {
218 self::checkCache();
219
220 if (!$ignoreOfflines) {
221 return self::$clangs;
222 }
223
224 return array_filter(self::$clangs, function (self $clang) {
225 return $clang->isOnline();
226 });
227 }
228
229 230 231
232 private static function checkCache()
233 {
234 if (self::$cacheLoaded) {
235 return;
236 }
237
238 $file = rex_path::coreCache('clang.cache');
239 if (!file_exists($file)) {
240 rex_clang_service::generateCache();
241 }
242 foreach (rex_file::getCache($file) as $id => $data) {
243 $clang = new self();
244
245 foreach ($data as $key => $value) {
246 $clang->$key = $value;
247 }
248
249 self::$clangs[$id] = $clang;
250 }
251 self::$cacheLoaded = true;
252 }
253
254 255 256
257 public static function reset()
258 {
259 self::$cacheLoaded = false;
260 self::$clangs = [];
261 }
262 }
263