1 <?php
  2 
  3 /**
  4  * Clang class.
  5  *
  6  * @author gharlan
  7  *
  8  * @package redaxo\core
  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      * Checks if the given clang exists.
 28      *
 29      * @param int $id Clang id
 30      *
 31      * @return bool
 32      */
 33     public static function exists($id)
 34     {
 35         self::checkCache();
 36         return isset(self::$clangs[$id]);
 37     }
 38 
 39     /**
 40      * Returns the clang object for the given id.
 41      *
 42      * @param int $id Clang id
 43      *
 44      * @return self
 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      * Returns the clang start id.
 56      *
 57      * @return int
 58      */
 59     public static function getStartId()
 60     {
 61         foreach (self::getAll() as $id => $clang) {
 62             return $id;
 63         }
 64     }
 65 
 66     /**
 67      * Returns the current clang object.
 68      *
 69      * @return self
 70      */
 71     public static function getCurrent()
 72     {
 73         return self::get(self::getCurrentId());
 74     }
 75 
 76     /**
 77      * Returns the current clang id.
 78      *
 79      * @return int Current clang id
 80      */
 81     public static function getCurrentId()
 82     {
 83         return self::$currentId;
 84     }
 85 
 86     /**
 87      * Sets the current clang id.
 88      *
 89      * @param int $id Clang id
 90      *
 91      * @throws rex_exception
 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      * Returns the id.
103      *
104      * @return int
105      */
106     public function getId()
107     {
108         return (int) $this->id;
109     }
110 
111     /**
112      * Returns the lang code.
113      *
114      * @return string
115      */
116     public function getCode()
117     {
118         return $this->code;
119     }
120 
121     /**
122      * Returns the name.
123      *
124      * @return string
125      */
126     public function getName()
127     {
128         return $this->name;
129     }
130 
131     /**
132      * Returns the priority.
133      *
134      * @return int
135      */
136     public function getPriority()
137     {
138         return $this->priority;
139     }
140 
141     /**
142      * Returns the status.
143      *
144      * @return bool
145      */
146     public function isOnline()
147     {
148         return $this->status;
149     }
150 
151     /**
152      * Checks whether the clang has the given value.
153      *
154      * @param string $value
155      *
156      * @return bool
157      */
158     public function hasValue($value)
159     {
160         return isset($this->$value) || isset($this->{'clang_' . $value});
161     }
162 
163     /**
164      * Returns the given value.
165      *
166      * @param string $value
167      *
168      * @return mixed
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      * Counts the clangs.
185      *
186      * @param bool $ignoreOfflines
187      *
188      * @return int
189      */
190     public static function count($ignoreOfflines = false)
191     {
192         self::checkCache();
193         return count(self::getAll($ignoreOfflines));
194     }
195 
196     /**
197      * Returns an array of all clang ids.
198      *
199      * @param bool $ignoreOfflines
200      *
201      * @return int[]
202      */
203     public static function getAllIds($ignoreOfflines = false)
204     {
205         self::checkCache();
206         return array_keys(self::getAll($ignoreOfflines));
207     }
208 
209     /**
210      * Returns an array of all clangs.
211      *
212      * @param bool $ignoreOfflines
213      *
214      * @return self[]
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      * Loads the cache if not already loaded.
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      * Resets the intern cache of this class.
256      */
257     public static function reset()
258     {
259         self::$cacheLoaded = false;
260         self::$clangs = [];
261     }
262 }
263