ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilSystemStyleScssSettings.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 {
30  protected array $files = [];
31 
35  protected array $variables = [];
36 
40  protected array $categories = [];
41 
45  protected string $scss_variables_settings_path = '';
46 
47  public function __construct(string $scss_variables_settings_path)
48  {
49  $this->scss_variables_settings_path = $scss_variables_settings_path;
50  $this->readFolder();
51  }
52 
53  public function readFolder(): void
54  {
55  if (is_dir($this->scss_variables_settings_path)) {
56  $files = scandir($this->scss_variables_settings_path, SCANDIR_SORT_ASCENDING);
57  foreach ($files as $file) {
58  $file_path = $this->scss_variables_settings_path . '/' . $file;
59  if ($file != "." && $file != ".." && !is_dir($file_path)) {
60  $this->files[] = new ilSystemStyleScssSettingsFile($this->scss_variables_settings_path, $file);
61  }
62  }
63  } else {
64  throw new ilSystemStyleException(
66  $this->scss_variables_settings_path
67  );
68  }
69  }
70 
71  public function readAndreplaceContentOfFolder(array $replacements): void
72  {
73  if (is_dir($this->scss_variables_settings_path)) {
74  $files = scandir($this->scss_variables_settings_path, SCANDIR_SORT_ASCENDING);
75  foreach ($files as $file) {
76  $file_path = $this->scss_variables_settings_path . '/' . $file;
77  if ($file != "." && $file != ".." && !is_dir($file_path)) {
78  $content = file_get_contents($file_path);
79  foreach ($replacements as $search => $replace) {
80  $content = str_replace($search, $replace, $content);
81  }
82  file_put_contents($file_path, $content);
83  }
84  }
85  }
86  }
87 
91  public function write(string $new_path = ""): void
92  {
93  if ($new_path == "") {
95  } else {
96  $path = $new_path ;
97  }
98  foreach ($this->files as $file) {
99  $file->write($path);
100  }
101  }
102 
103  public function getContent(): string
104  {
105  $output = '';
106 
107  foreach ($this->files as $file) {
108  $output .= $file->getContent();
109  }
110  return $output;
111  }
112 
116  public function getCategories(): array
117  {
118  if (count($this->categories) == 0) {
119  foreach ($this->files as $file) {
120  $this->categories = array_merge($this->categories, $file->getCategories());
121  }
122  }
123  return $this->categories;
124  }
125 
126  public function getCategoryByName(string $name): ilSystemStyleScssCategory
127  {
128  $categories = $this->getCategories();
129  return $categories[$name];
130  }
131 
135  public function getVariables(): array
136  {
137  if (count($this->variables) == 0) {
138  foreach ($this->files as $file) {
139  $this->variables = array_merge($this->variables, $file->getVariables());
140  }
141  }
142  return $this->variables;
143  }
144 
145  public function getVariableByName(string $name): ilSystemStyleScssVariable
146  {
147  $categories = $this->getVariables();
148  return $categories[$name];
149  }
150 
154  public function getVariablesPerCategory(string $category_name = ''): array
155  {
156  $variables = [];
157  foreach ($this->getVariables() as $variable) {
158  if ($variable->getCategoryName() == $category_name) {
159  $variables[] = $variable;
160  }
161  }
162 
163  return $variables;
164  }
165 
166  public function getReferencesToVariable(string $variable_name): array
167  {
168  $references = [];
169 
170  foreach ($this->getVariables() as $variable) {
171  foreach ($variable->getReferences() as $reference) {
172  if ($variable_name == $reference) {
173  $references[] = $variable->getName();
174  }
175  }
176  }
177 
178  return $references;
179  }
180 
181  public function getReferencesToVariableAsString(string $variable_name): string
182  {
183  $references_string = '';
184  foreach ($this->getReferencesToVariable($variable_name) as $reference) {
185  $references_string .= "$reference; ";
186  }
187  return $references_string;
188  }
189 
190  public function getRefAndCommentAsString(string $variable_name, string $refs_wording): string
191  {
192  $references_string = '';
193  foreach ($this->getReferencesToVariable($variable_name) as $reference) {
194  $references_string .= "$reference; ";
195  }
196 
197  $variable = $this->getVariableByName($variable_name);
198 
199  if ($references_string != '') {
200  if ($variable->getComment()) {
201  $info = $variable->getComment() . '</br>' . $refs_wording . ' ' . $references_string;
202  } else {
203  $info = $refs_wording . ' ' . $references_string;
204  }
205  } else {
206  $info = $variable->getComment();
207  }
208 
209  return $info;
210  }
211 
212  public function getVariablesForDelosOverride(): string
213  {
214  $out = "";
215  foreach ($this->getVariables() as $variable) {
216  $out .= $variable->getForDelosOverride();
217  }
218  return $out;
219  }
220 
224  public function getItems(): array
225  {
226  $item = [];
227  foreach ($this->files as $file) {
228  $item = array_merge($item, $file->getItems());
229  }
230  return $item;
231  }
232 }
getRefAndCommentAsString(string $variable_name, string $refs_wording)
__construct(string $scss_variables_settings_path)
getReferencesToVariableAsString(string $variable_name)
string $scss_variables_settings_path
Complete path the the settings folder on the file system.
Capsules data of a Scss category in the variables to Scss file.
write(string $new_path="")
Write the complete files back to the file system (including comments and random content) ...
$path
Definition: ltiservices.php:32
$out
Definition: buildRTE.php:24
readAndreplaceContentOfFolder(array $replacements)
getVariablesPerCategory(string $category_name='')