ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilSystemStyleLessFile.php
Go to the documentation of this file.
1 <?php
2 require_once("./Services/Style/System/classes/Less/class.ilSystemStyleLessItem.php");
3 require_once("./Services/Style/System/classes/Less/class.ilSystemStyleLessCategory.php");
4 require_once("./Services/Style/System/classes/Less/class.ilSystemStyleLessComment.php");
5 require_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 
51  protected $less_variables_file_path = "";
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  $last_variable_comment = null;
70  $last_category_id = null;
71  $last_category_name = null;
72 
73  $regex_category = '/\/\/==\s(.*)/'; //Matches //== Category Name
74  $regex_category_by_line = '/^\/\/[\s]?$/'; //Matches // at the end of the line with not comment
75  $regex_category_comment = '/\/\/##\s(.*)/'; //Matches Matches //## Category Description
76  $regex_variable = '/^@(.*)/'; //Matches @VariableName value;
77  $regex_variable_comment = '/\/\/\*\*\s(.*)/'; //Matches //** Variable Comment
78  $regex_variable_name = '/(?:@)(.*)(?:\:)/'; //Matches @variableName
79  $regex_variable_value = '/(?::)(.*)(?:;)/'; //Matches value;
80  $regex_variable_references = '/(?:@)([a-zA-Z0-9_-]*)/'; //Matches references in value
81 
82  try{
83  $handle = fopen($this->getLessVariablesFile(), "r");
84  }catch(Exception $e){
86  }
87 
88 
89  if ($handle) {
90  $line_number = 1;
91  $last_line_is_category = false;
92  //Reads file line by line
93  while (($line = fgets($handle)) !== false) {
94  //This might be part of the categories structure, if so, ignore
95  if($last_line_is_category && preg_match($regex_category_by_line, $line, $out)){
96  $line = fgets($handle);
97  }
98  $last_line_is_category = false;
99  if(preg_match($regex_category, $line, $out)){
100  //Check Category
101  $last_category_id = $this->addItem(new ilSystemStyleLessCategory($out[1]));
102  $last_category_name = $out[1];
103  $last_line_is_category = true;
104  } else if(preg_match($regex_category_comment, $line, $out)){
105  //Check Comment Category
106  $last_category = $this->getItemById($last_category_id);
107  $last_category->setComment($out[1]);
108  } else if(preg_match($regex_variable_comment, $line, $out)){
109  //Check Variables Comment
110  $last_variable_comment = $out[1];
111  } else if(preg_match($regex_variable, $line, $out)){
112  //Check Variables
113 
114  //Name
115  preg_match($regex_variable_name, $out[0], $variable);
116 
117  //Value
118  preg_match($regex_variable_value, $line, $value);
119 
120  //References
121  $temp_value = $value[0];
122  $references = array();
123  while(preg_match($regex_variable_references,$temp_value,$reference)){
124  $references[] = $reference[1];
125  $temp_value = str_replace($reference,"",$temp_value);
126  }
127 
129  $variable[1],
130  ltrim ( $value[1] ," \t\n\r\0\x0B" ),
131  $last_variable_comment,
132  $last_category_name,
133  $references));
134  $last_variable_comment = "";
135 
136  }else{
137  $this->addItem(new ilSystemStyleLessComment($line));
138  }
139 
140 
141  $line_number++;
142  }
143  fclose($handle);
144  } else {
146  }
147  }
148 
152  public function write(){
153  file_put_contents($this->getLessVariablesFile(),$this->getContent());
154  }
155 
159  public function getContent(){
160  $output = "";
161 
162  foreach($this->items as $item){
163  $output .= $item->__toString();
164  }
165  return $output;
166  }
167 
172  public function addItem(ilSystemStyleLessItem $item){
173  $id = array_push($this->items,$item)-1;
174 
175 
176  if(get_class($item)=="ilSystemStyleLessComment"){
177  $this->comments_ids[] = $id;
178  }else if(get_class($item)=="ilSystemStyleLessCategory"){
179  $this->categories_ids[] = $id;
180  }else if(get_class($item)=="ilSystemStyleLessVariable"){
181  $this->variables_ids[] = $id;
182  }
183 
184  return $id;
185  }
186 
190  public function getCategories(){
191  $categories = array();
192 
193  foreach($this->categories_ids as $category_id){
194  $categories[] = $this->items[$category_id];
195  }
196 
197  return $categories;
198 
199  }
200 
205  public function getVariablesPerCategory($category = ""){
206  $variables = array();
207 
208  foreach($this->variables_ids as $variables_id){
209  if(!$category || $this->items[$variables_id]->getCategoryName() == $category){
210  $variables[] = $this->items[$variables_id];
211  }
212  }
213 
214  return $variables;
215  }
216 
221  public function getItemById($id){
222  return $this->items[$id];
223  }
224 
229  public function getVariableByName($name = ""){
230  foreach($this->variables_ids as $variables_id){
231  if($this->items[$variables_id]->getName() == $name){
232  return $this->items[$variables_id];
233  }
234  }
235  return null;
236 
237  }
238 
243  public function getReferencesToVariable($variable_name){
244  $references = [];
245 
246  foreach($this->variables_ids as $id){
247  foreach($this->items[$id]->getReferences() as $reference){
248  if($variable_name == $reference)
249  $references[] = $this->items[$id]->getName();
250  }
251  }
252  return $references;
253  }
254 
259  public function getReferencesToVariableAsString($variable_name){
260  $references_string = "";
261  foreach($this->getReferencesToVariable($variable_name) as $reference){
262  $references_string .= "$reference; ";
263  }
264  return $references_string;
265  }
266 
270  public function getLessVariablesFile()
271  {
272  return $this->less_variables_file;
273  }
274 
278  public function setLessVariablesFile($less_variables_file)
279  {
280  $this->less_variables_file = $less_variables_file;
281  }
282 
286  public function getItems()
287  {
288  return $this->items;
289  }
290 
294  public function getCommentsIds()
295  {
296  return $this->comments_ids;
297  }
298 
302  public function getVariablesIds()
303  {
304  return $this->variables_ids;
305  }
306 
310  public function getCategoriesIds()
311  {
312  return $this->categories_ids;
313  }
314 }
__construct($less_variables_file)
KitchenSinkLessFile constructor.
read()
Reads the file from the file system.
write()
Write the complete file back to the file system (including comments and random content) ...
setLessVariablesFile($less_variables_file)
Capsules data of a less category in the variables to less file.
addItem(ilSystemStyleLessItem $item)
getReferencesToVariableAsString($variable_name)
if(!is_dir( $entity_dir)) exit("Fatal Error ([A-Za-z0-9]+)\+" &#(? foreach( $entity_files as $file) $output
Class for advanced editing exception handling in ILIAS.
Create styles array
The data for the language used.
Abstracts content of a less file.
Capsules all data which is neither part of a variable or category structure in the less file...