ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilFileSystemHelper.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
31  protected ilLanguage $lng;
32 
33  public function __construct(ilLanguage $lng, ilSystemStyleMessageStack $message_stack)
34  {
35  $this->setMessageStack($message_stack);
36  $this->lng = $lng;
37  }
38 
42  public function move(string $from, string $to): void
43  {
44  rename($from, $to);
45  }
46 
47  public function delete(string $file_path): void
48  {
49  unlink($file_path);
50  }
51 
55  public function saveDeleteFile(string $file_path): void
56  {
57  if (file_exists($file_path)) {
58  unlink($file_path);
59  $this->getMessageStack()->addMessage(
61  $this->lng->txt('file_deleted') . ' ' . $file_path,
63  )
64  );
65  }
66  }
67 
71  public function recursiveRemoveDir(string $dir): void
72  {
73  if (is_dir($dir)) {
74  $objects = scandir($dir);
75  foreach ($objects as $object) {
76  if ($object != '.' && $object != '..') {
77  if (is_dir($dir . '/' . $object)) {
78  $this->recursiveRemoveDir($dir . '/' . $object);
79  } else {
80  unlink($dir . '/' . $object);
81  }
82  }
83  }
84  rmdir($dir);
85  }
86  }
87 
91  public function removeResourceDirectory(string $skin_dir, string $dir)
92  {
93  $absolut_dir = $skin_dir . $dir;
94 
95  if (file_exists($absolut_dir)) {
96  self::recursiveRemoveDir($absolut_dir);
97  $this->getMessageStack()->addMessage(
99  $this->lng->txt('dir_deleted') . ' ' . $dir,
101  )
102  );
103  }
104  }
105 
110  public function createResourceDirectory(string $source, string $target): void
111  {
112  mkdir($target, 0775, true);
113 
114  if ($source != '') {
115  $this->recursiveCopy($source, $target);
116  $this->getMessageStack()->addMessage(
118  $this->lng->txt('dir_created') . $target,
120  )
121  );
122  }
123  }
124 
129  public function changeResourceDirectory(string $skin_dir, string $new_dir, string $old_dir): void
130  {
131  $absolut_new_dir = $skin_dir . $new_dir;
132  $absolut_old_dir = $skin_dir . $old_dir;
133 
134  if (file_exists($absolut_new_dir)) {
135  $this->getMessageStack()->addMessage(
137  $this->lng->txt('dir_changed_to') . ' ' . $absolut_new_dir,
139  )
140  );
141  $this->getMessageStack()->addMessage(
143  $this->lng->txt('dir_preserved_backup') . ' ' . $absolut_old_dir,
145  )
146  );
147  } else {
148  mkdir($absolut_new_dir, 0775, true);
149  $this->recursiveCopy($absolut_old_dir, $absolut_new_dir);
150  $this->getMessageStack()->addMessage(
152  $this->lng->txt('dir_copied_from') . ' ' . $absolut_old_dir . ' ' . $this->lng->txt('sty_copy_to') . ' ' . $absolut_new_dir,
154  )
155  );
156  $this->recursiveRemoveDir($skin_dir . $old_dir);
157  $this->getMessageStack()->addMessage(
159  $this->lng->txt('dir_deleted') . ' ' . $absolut_old_dir,
161  )
162  );
163  }
164  }
165 
170  public function recursiveCopy(string $src, string $dest): void
171  {
172  foreach (scandir($src) as $file) {
173  $src_file = rtrim($src, '/') . '/' . $file;
174  $dest_file = rtrim($dest, '/') . '/' . $file;
175  if (!is_readable($src_file)) {
177  }
178  if (substr($file, 0, 1) != '.') {
179  if (is_dir($src_file)) {
180  if (!file_exists($dest_file)) {
181  try {
182  mkdir($dest_file);
183  } catch (Exception $e) {
184  throw new ilSystemStyleException(
186  'Copy ' . $src_file . ' to ' . $dest_file . ' Error: ' . $e
187  );
188  }
189  }
190  $this->recursiveCopy($src_file, $dest_file);
191  } else {
192  try {
193  copy($src_file, $dest_file);
194  } catch (Exception $e) {
195  throw new ilSystemStyleException(
197  'Copy ' . $src_file . ' to ' . $dest_file . ' Error: ' . $e
198  );
199  }
200  }
201  }
202  }
203  }
204 
205 
206 
208  {
209  return $this->message_stack;
210  }
211 
212  public function setMessageStack(ilSystemStyleMessageStack $message_stack): void
213  {
214  $this->message_stack = $message_stack;
215  }
216 }
createResourceDirectory(string $source, string $target)
Creates a resource directory (sound, images or fonts) by copying from the source (mostly delos) ...
saveDeleteFile(string $file_path)
Deletes a given file in the container.
__construct(ilLanguage $lng, ilSystemStyleMessageStack $message_stack)
recursiveRemoveDir(string $dir)
Recursive delete of a folder.
changeResourceDirectory(string $skin_dir, string $new_dir, string $old_dir)
Alters the name/path of a resource directory.
File System Helper, to reduce deps.
move(string $from, string $to)
Used to move a complete directory of a skin.
Used to stack messages to be shown to the user.
setMessageStack(ilSystemStyleMessageStack $message_stack)
removeResourceDirectory(string $skin_dir, string $dir)
Deletes a resource directory.
ilSystemStyleMessageStack $message_stack
Used to stack messages to be displayed to the user (mostly reports for failed actions) ...
recursiveCopy(string $src, string $dest)
Recursive copy of a folder.