ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilSystemStyleLessFile.php
Go to the documentation of this file.
1<?php
2require_once("./Services/Style/System/classes/Less/class.ilSystemStyleLessItem.php");
3require_once("./Services/Style/System/classes/Less/class.ilSystemStyleLessCategory.php");
4require_once("./Services/Style/System/classes/Less/class.ilSystemStyleLessComment.php");
5require_once("./Services/Style/System/classes/Less/class.ilSystemStyleLessVariable.php");
6
7
8/***
9 * This data abstracts a complete less file. A less file is composed of categories, variables and random comments
10 * (unclassified information)
11 *
12 * @author Timon Amstutz <timon.amstutz@ilub.unibe.ch>
13 * @version $Id$
14 *
15 */
17{
23 protected $items = array();
24
30 protected $comments_ids = array();
31
37 protected $variables_ids = array();
38
44 protected $categories_ids = array();
45
52
57 public function __construct($less_variables_file)
58 {
59 $this->less_variables_file = $less_variables_file;
60 $this->read();
61 }
62
68 public function read()
69 {
70 $last_variable_comment = null;
71 $last_category_id = null;
72 $last_category_name = null;
73
74 $regex_category = '/\/\/==\s(.*)/'; //Matches //== Category Name
75 $regex_category_by_line = '/^\/\/[\s]?$/'; //Matches // at the end of the line with not comment
76 $regex_category_comment = '/\/\/##\s(.*)/'; //Matches Matches //## Category Description
77 $regex_variable = '/^@(.*)/'; //Matches @VariableName value;
78 $regex_variable_comment = '/\/\/\*\*\s(.*)/'; //Matches //** Variable Comment
79 $regex_variable_name = '/(?:@)(.*)(?:\:)/'; //Matches @variableName
80 $regex_variable_value = '/(?::)(.*)(?:;)/'; //Matches value;
81 $regex_variable_references = '/(?:@)([a-zA-Z0-9_-]*)/'; //Matches references in value
82
83 try {
84 $handle = fopen($this->getLessVariablesFile(), "r");
85 } catch (Exception $e) {
87 }
88
89
90 if ($handle) {
91 $line_number = 1;
92 $last_line_is_category = false;
93 //Reads file line by line
94 while (($line = fgets($handle)) !== false) {
95 //This might be part of the categories structure, if so, ignore
96 if ($last_line_is_category && preg_match($regex_category_by_line, $line, $out)) {
97 $line = fgets($handle);
98 }
99 $last_line_is_category = false;
100 if (preg_match($regex_category, $line, $out)) {
101 //Check Category
102 $last_category_id = $this->addItem(new ilSystemStyleLessCategory($out[1]));
103 $last_category_name = $out[1];
104 $last_line_is_category = true;
105 } elseif (preg_match($regex_category_comment, $line, $out)) {
106 //Check Comment Category
107 $last_category = $this->getItemById($last_category_id);
108 $last_category->setComment($out[1]);
109 } elseif (preg_match($regex_variable_comment, $line, $out)) {
110 //Check Variables Comment
111 $last_variable_comment = $out[1];
112 } elseif (preg_match($regex_variable, $line, $out)) {
113 //Check Variables
114
115 //Name
116 preg_match($regex_variable_name, $out[0], $variable);
117
118 //Value
119 preg_match($regex_variable_value, $line, $value);
120
121 //References
122 $temp_value = $value[0];
123 $references = array();
124 while (preg_match($regex_variable_references, $temp_value, $reference)) {
125 $references[] = $reference[1];
126 $temp_value = str_replace($reference, "", $temp_value);
127 }
128
130 $variable[1],
131 ltrim($value[1], " \t\n\r\0\x0B"),
132 $last_variable_comment,
133 $last_category_name,
134 $references
135 ));
136 $last_variable_comment = "";
137 } else {
138 $this->addItem(new ilSystemStyleLessComment($line));
139 }
140
141
142 $line_number++;
143 }
144 fclose($handle);
145 } else {
147 }
148 }
149
153 public function write()
154 {
155 file_put_contents($this->getLessVariablesFile(), $this->getContent());
156 }
157
161 public function getContent()
162 {
163 $output = "";
164
165 foreach ($this->items as $item) {
166 $output .= $item->__toString();
167 }
168 return $output;
169 }
170
175 public function addItem(ilSystemStyleLessItem $item)
176 {
177 $id = array_push($this->items, $item) - 1;
178
179
180 if (get_class($item) == "ilSystemStyleLessComment") {
181 $this->comments_ids[] = $id;
182 } elseif (get_class($item) == "ilSystemStyleLessCategory") {
183 $this->categories_ids[] = $id;
184 } elseif (get_class($item) == "ilSystemStyleLessVariable") {
185 $this->variables_ids[] = $id;
186 }
187
188 return $id;
189 }
190
194 public function getCategories()
195 {
196 $categories = array();
197
198 foreach ($this->categories_ids as $category_id) {
199 $categories[] = $this->items[$category_id];
200 }
201
202 return $categories;
203 }
204
209 public function getVariablesPerCategory($category = "")
210 {
211 $variables = array();
212
213 foreach ($this->variables_ids as $variables_id) {
214 if (!$category || $this->items[$variables_id]->getCategoryName() == $category) {
215 $variables[] = $this->items[$variables_id];
216 }
217 }
218
219 return $variables;
220 }
221
226 public function getItemById($id)
227 {
228 return $this->items[$id];
229 }
230
235 public function getVariableByName($name = "")
236 {
237 foreach ($this->variables_ids as $variables_id) {
238 if ($this->items[$variables_id]->getName() == $name) {
239 return $this->items[$variables_id];
240 }
241 }
242 return null;
243 }
244
249 public function getReferencesToVariable($variable_name)
250 {
251 $references = [];
252
253 foreach ($this->variables_ids as $id) {
254 foreach ($this->items[$id]->getReferences() as $reference) {
255 if ($variable_name == $reference) {
256 $references[] = $this->items[$id]->getName();
257 }
258 }
259 }
260 return $references;
261 }
262
267 public function getReferencesToVariableAsString($variable_name)
268 {
269 $references_string = "";
270 foreach ($this->getReferencesToVariable($variable_name) as $reference) {
271 $references_string .= "$reference; ";
272 }
273 return $references_string;
274 }
275
279 public function getLessVariablesFile()
280 {
281 return $this->less_variables_file;
282 }
283
287 public function setLessVariablesFile($less_variables_file)
288 {
289 $this->less_variables_file = $less_variables_file;
290 }
291
295 public function getItems()
296 {
297 return $this->items;
298 }
299
303 public function getCommentsIds()
304 {
305 return $this->comments_ids;
306 }
307
311 public function getVariablesIds()
312 {
314 }
315
319 public function getCategoriesIds()
320 {
322 }
323}
An exception for terminatinating execution or to throw for unit testing.
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.
__construct($less_variables_file)
KitchenSinkLessFile constructor.
addItem(ilSystemStyleLessItem $item)
getReferencesToVariableAsString($variable_name)
write()
Write the complete file back to the file system (including comments and random content)
setLessVariablesFile($less_variables_file)
read()
Reads the file from the file system.
Abstracts content of a less file.
if($format !==null) $name
Definition: metadata.php:230