ILIAS  release_8 Revision v8.24
class.ilSystemStyleLessFile.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21/***
22 * This data abstracts a complete less file. A less file is composed of categories, variables and random comments
23 * (unclassified information)
24 */
26{
31 protected array $items = [];
32
36 protected array $comments_ids = [];
37
41 protected array $variables_ids = [];
42
46 protected array $categories_ids = [];
47
51 protected string $less_variables_file_path_name = '';
52
54 {
55 $this->less_variables_file_path_name = $less_variables_file_path_name;
56 $this->read();
57 }
58
63 public function read(): void
64 {
65 $last_variable_comment = '';
66 $last_category_id = '';
67 $last_category_name = '';
68
69 $regex_category = '/\/\/==\s(.*)/'; //Matches //== Category Name
70 $regex_category_by_line = '/^\/\/[\s]?$/'; //Matches // at the end of the line with not comment
71 $regex_category_comment = '/\/\/##\s(.*)/'; //Matches Matches //## Category Description
72 $regex_variable = '/^@(.*)/'; //Matches @VariableName value;
73 $regex_variable_comment = '/\/\/\*\*\s(.*)/'; //Matches //** Variable Comment
74 $regex_variable_name = '/(?:@)(.*)(?:\:)/'; //Matches @variableName
75 $regex_variable_value = '/(?::)(.*)(?:;)/'; //Matches value;
76 $regex_variable_references = '/(?:@)([a-zA-Z0-9_-]*)/'; //Matches references in value
77
78 try {
79 $handle = fopen($this->getLessVariablesFilePathName(), 'r');
80 } catch (Exception $e) {
81 throw new ilSystemStyleException(
84 );
85 }
86
87 if ($handle) {
88 $line_number = 1;
89 $last_line_is_category = false;
90 //Reads file line by line
91 while (($line = fgets($handle)) !== false) {
92 //This might be part of the categories structure, if so, ignore
93 if ($last_line_is_category && preg_match($regex_category_by_line, $line, $out)) {
94 $line = fgets($handle);
95 }
96 $last_line_is_category = false;
97 if (preg_match($regex_category, $line, $out)) {
98 //Check Category
99 $last_category_id = $this->addItem(new ilSystemStyleLessCategory($out[1]));
100 $last_category_name = $out[1] ?: '';
101 $last_line_is_category = true;
102 } elseif (preg_match($regex_category_comment, $line, $out)) {
103 //Check Comment Category
104 $last_category = $this->getItemById($last_category_id);
105 $last_category->setComment($out[1]);
106 } elseif (preg_match($regex_variable_comment, $line, $out)) {
107 //Check Variables Comment
108 $last_variable_comment = $out[1];
109 } elseif (preg_match($regex_variable, $line, $out)) {
110 //Check Variables
111
112 //Name
113 preg_match($regex_variable_name, $out[0], $variable);
114
115 //Value
116 preg_match($regex_variable_value, $line, $value);
117
118 //References
119 $temp_value = $value[0];
120 $references = [];
121 while (preg_match($regex_variable_references, $temp_value, $reference)) {
122 $references[] = $reference[1];
123 $temp_value = str_replace($reference, '', $temp_value);
124 }
125
127 $variable[1],
128 ltrim($value[1]),
129 $last_variable_comment,
130 $last_category_name,
131 $references
132 ));
133 $last_variable_comment = '';
134 } else {
135 $this->addItem(new ilSystemStyleLessComment($line));
136 }
137
138 $line_number++;
139 }
140 fclose($handle);
141 } else {
143 }
144 }
145
149 public function write(): void
150 {
151 file_put_contents($this->getLessVariablesFilePathName(), $this->getContent());
152 }
153
154 public function getContent(): string
155 {
156 $output = '';
157
158 foreach ($this->items as $item) {
159 $output .= $item->__toString();
160 }
161 return $output;
162 }
163
164 public function addItem(ilSystemStyleLessItem $item): int
165 {
166 $id = array_push($this->items, $item) - 1;
167
168 if (get_class($item) == 'ilSystemStyleLessComment') {
169 $this->comments_ids[] = $id;
170 } elseif (get_class($item) == 'ilSystemStyleLessCategory') {
171 $this->categories_ids[] = $id;
172 } elseif (get_class($item) == 'ilSystemStyleLessVariable') {
173 $this->variables_ids[] = $id;
174 }
175
176 return $id;
177 }
178
182 public function getCategories(): array
183 {
184 $categories = [];
185
186 foreach ($this->categories_ids as $category_id) {
187 $categories[] = $this->items[$category_id];
188 }
189
190 return $categories;
191 }
192
196 public function getVariablesPerCategory(string $category = ''): array
197 {
198 $variables = [];
199
200 foreach ($this->variables_ids as $variables_id) {
201 if (!$category || $this->items[$variables_id]->getCategoryName() == $category) {
202 $variables[] = $this->items[$variables_id];
203 }
204 }
205
206 return $variables;
207 }
208
210 {
211 return $this->items[$id];
212 }
213
214 public function getVariableByName(string $name = ''): ?ilSystemStyleLessItem
215 {
216 foreach ($this->variables_ids as $variables_id) {
217 if ($this->items[$variables_id]->getName() == $name) {
218 return $this->items[$variables_id];
219 }
220 }
221 return null;
222 }
223
224 public function getReferencesToVariable(string $variable_name): array
225 {
226 $references = [];
227
228 foreach ($this->variables_ids as $id) {
229 foreach ($this->items[$id]->getReferences() as $reference) {
230 if ($variable_name == $reference) {
231 $references[] = $this->items[$id]->getName();
232 }
233 }
234 }
235 return $references;
236 }
237
238 public function getReferencesToVariableAsString(string $variable_name): string
239 {
240 $references_string = '';
241 foreach ($this->getReferencesToVariable($variable_name) as $reference) {
242 $references_string .= "$reference; ";
243 }
244 return $references_string;
245 }
246
247 public function getRefAndCommentAsString(string $variable_name, string $refs_wording): string
248 {
249 $references_string = '';
250 foreach ($this->getReferencesToVariable($variable_name) as $reference) {
251 $references_string .= "$reference; ";
252 }
253
254 $variable = $this->getVariableByName($variable_name);
255
256 if ($references_string != '') {
257 if ($variable->getComment()) {
258 $info = $variable->getComment() . '</br>' . $refs_wording . ' ' . $references_string;
259 } else {
260 $info = $refs_wording . ' ' . $references_string;
261 }
262 } else {
263 $info = $variable->getComment();
264 }
265
266 return $info;
267 }
268
269 public function getLessVariablesFilePathName(): string
270 {
272 }
273
275 {
276 $this->less_variables_file_path_name = $less_variables_file_path_name;
277 }
278
282 public function getItems(): array
283 {
284 return $this->items;
285 }
286
287 public function getCommentsIds(): array
288 {
289 return $this->comments_ids;
290 }
291
292 public function getVariablesIds(): array
293 {
295 }
296
297 public function getCategoriesIds(): array
298 {
300 }
301}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$out
Definition: buildRTE.php:24
Class for advanced editing exception handling in ILIAS.
Capsules data of a less category in the variables to less file.
Capsules all data which is neither part of a variable or category structure in the less file.
getVariablesPerCategory(string $category='')
addItem(ilSystemStyleLessItem $item)
string $less_variables_file_path_name
Complete path the the variables file on the file system.
setLessVariablesFilePathName(string $less_variables_file_path_name)
getReferencesToVariable(string $variable_name)
array $categories_ids
Separated array with all category ids (performance reasons)
write()
Write the complete file back to the file system (including comments and random content)
__construct(string $less_variables_file_path_name)
array $comments_ids
Separated array with all comments ids (performance reasons)
read()
Reads the file from the file system.
array $variables_ids
Separated array with all variable ids (performance reasons)
getReferencesToVariableAsString(string $variable_name)
getRefAndCommentAsString(string $variable_name, string $refs_wording)
Abstracts content of a less file.
if($format !==null) $name
Definition: metadata.php:247