ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilSystemStyleScssSettingsFile.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 /***
22  * This data abstracts a complete Scss file. A Scss file is composed of categories, variables and random comments
23  * (unclassified information)
24  */
26 {
31  protected array $items = [];
32 
36  protected array $variables_ids = [];
37 
41  protected array $categories_ids = [];
42 
47 
48  protected string $file_name;
49 
50  public function __construct(string $scss_variables_settings_path, string $file_name)
51  {
52  $this->file_name = $file_name;
53  $this->scss_variables_settings_path = $scss_variables_settings_path;
54  $this->openFile($scss_variables_settings_path);
55  }
56 
57  protected function openFile(string $scss_variables_settings_path): void
58  {
59  if (is_file($scss_variables_settings_path.'/'.$this->file_name)) {
60  $this->readFile($scss_variables_settings_path);
61  } else {
62  throw new ilSystemStyleException(
64  $scss_variables_settings_path
65  );
66  }
67  }
72  protected function readFile(string $scss_variables_settings_path): void
73  {
74  $last_variable_comment = '';
75  $last_category_id = '';
76  $last_category_name = '';
77 
78  $regex_category = '/\/\/==\s(.*)/'; //Matches //== Category Name
79  $regex_category_by_line = '/^\/\/[\s]?$/'; //Matches // at the end of the line with not comment
80  $regex_category_comment = '/\/\/##\s(.*)/'; //Matches Matches //## Category Description
81  $regex_variable = '/^\$(.*)/'; //Matches @VariableName value;
82  $regex_variable_comment = '/\/\/\*\*\s(.*)/'; //Matches //** Variable Comment
83  $regex_variable_name = '/(?:\$)(.*?)(?:\:)/'; //Matches @variableName
84  $regex_variable_value = '/(?::)(.*)(?:;)/'; //Matches value;
85  $regex_variable_references = '/(?:\$)([a-zA-Z0-9_-]*)/'; //Matches references in value
86 
87  try {
88  $handle = fopen($scss_variables_settings_path."/".$this->file_name, 'r');
89  } catch (Exception $e) {
90  throw new ilSystemStyleException(
92  $scss_variables_settings_path
93  );
94  }
95 
96  if ($handle) {
97  $line_number = 1;
98  $last_line_is_category = false;
99  //Reads file line by line
100  while (($line = fgets($handle)) !== false) {
101  //This might be part of the categories structure, if so, ignore
102  if ($last_line_is_category && preg_match($regex_category_by_line, $line, $out)) {
103  $line = fgets($handle);
104  }
105  $last_line_is_category = false;
106  if (preg_match($regex_category, $line, $out)) {
107  //Check Category
108  $last_category_id = $this->addItem(new ilSystemStyleScssCategory($out[1]));
109  $last_category_name = $out[1] ?: '';
110  $last_line_is_category = true;
111  } elseif (preg_match($regex_category_comment, $line, $out)) {
112  //Check Comment Category
113  $last_category = $this->getItemById($last_category_id);
114  $last_category->setComment($out[1]);
115  } elseif (preg_match($regex_variable_comment, $line, $out)) {
116  //Check Variables Comment
117  $last_variable_comment = $out[1];
118  } elseif (preg_match($regex_variable, $line, $out)) {
119  //Check Variables
120  //Name
121  preg_match($regex_variable_name, $out[0], $variable);
122 
123  //Value
124  preg_match($regex_variable_value, $line, $value);
125 
126  //References
127  $temp_value = $value[0];
128  $references = [];
129  while (preg_match($regex_variable_references, $temp_value, $reference)) {
130  $references[] = $reference[1];
131  $temp_value = str_replace($reference, '', $temp_value);
132  }
133 
134  //do not store the !default attribute in the variable
135  $value = str_replace(' !default', '', ltrim($value[1]));
136 
138  $variable[1],
139  $value,
140  $last_variable_comment,
141  $last_category_name,
142  $references
143  ));
144  $last_variable_comment = '';
145  } else {
146  $this->addItem(new ilSystemStyleScssComment($line));
147  }
148 
149  $line_number++;
150  }
151  fclose($handle);
152  } else {
154  }
155  }
156 
160  public function write(string $scss_variables_settings_path = ""): void
161  {
162  if ($scss_variables_settings_path == "") {
164  } else {
166  }
167  file_put_contents($path . $this->file_name, $this->getContent());
168  }
169 
170  public function getContent(): string
171  {
172  $output = '';
173 
174  foreach ($this->items as $item) {
175  $output .= $item->__toString();
176  }
177  return $output;
178  }
179 
180  public function addItem(ilSystemStyleScssItem $item): int
181  {
182  $id = array_push($this->items, $item) - 1;
183 
184  if (get_class($item) == ilSystemStyleScssCategory::class) {
185  $this->categories_ids[] = $id;
186  } elseif (get_class($item) == ilSystemStyleScssVariable::class) {
187  $this->variables_ids[] = $id;
188  }
189 
190  return $id;
191  }
192 
196  public function getCategories(): array
197  {
198  $categories = [];
199 
200  foreach ($this->categories_ids as $category_id) {
201  $category = $this->items[$category_id];
202  $categories[$category->getName()] = $category;
203  }
204 
205  return $categories;
206  }
207 
211  public function getVariables(): array
212  {
213  $variables = [];
214 
215  foreach ($this->variables_ids as $variable_id) {
216  $variable = $this->items[$variable_id];
217  $variables[$variable->getName()] = $variable;
218  }
219 
220  return $variables;
221  }
222 
223  protected function getItemById(int $id): ilSystemStyleScssItem
224  {
225  return $this->items[$id];
226  }
227 
228  public function getItems(): array
229  {
230  return $this->items;
231  }
232 
236  public function getScssVariablesSettingsPath(): string
237  {
239  }
240 }
readFile(string $scss_variables_settings_path)
Reads the file from the file system.
array $categories_ids
Separated array with all category ids (performance reasons)
Abstracts content of a scss file.
Capsules data of a Scss category in the variables to Scss file.
write(string $scss_variables_settings_path="")
Write the complete file back to the file system (including comments and random content) ...
__construct(string $scss_variables_settings_path, string $file_name)
$path
Definition: ltiservices.php:32
Capsules all data which is neither part of a variable or category structure in the Scss file...
array $variables_ids
Separated array with all variable ids (performance reasons)
$out
Definition: buildRTE.php:24
string $scss_variables_settings_path
Complete path to the settings file.
openFile(string $scss_variables_settings_path)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23