ILIAS  release_8 Revision v8.24
class.ilFileSystemHelper.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
31 protected ilLanguage $lng;
32
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, bool $is_linked)
92 {
93 $absolut_dir = $skin_dir . $dir;
94
95 if (file_exists($absolut_dir)) {
96 if (!$is_linked) {
97 self::recursiveRemoveDir($skin_dir . $dir);
98 $this->getMessageStack()->addMessage(
100 $this->lng->txt('dir_deleted') . ' ' . $dir,
102 )
103 );
104 } else {
105 $this->getMessageStack()->addMessage(
107 $this->lng->txt('dir_preserved_linked') . ' ' . $dir,
109 )
110 );
111 }
112 }
113 }
114
119 public function createResourceDirectory(string $source, string $target): void
120 {
121 mkdir($target, 0775, true);
122
123 if ($source != '') {
124 $this->recursiveCopy($source, $target);
125 $this->getMessageStack()->addMessage(
127 $this->lng->txt('dir_created') . $target,
129 )
130 );
131 }
132 }
133
138 public function changeResourceDirectory(string $skin_dir, string $new_dir, string $old_dir, bool $has_references): void
139 {
140 $absolut_new_dir = $skin_dir . $new_dir;
141 $absolut_old_dir = $skin_dir . $old_dir;
142
143 if (file_exists($absolut_new_dir)) {
144 $this->getMessageStack()->addMessage(
146 $this->lng->txt('dir_changed_to') . ' ' . $absolut_new_dir,
148 )
149 );
150 $this->getMessageStack()->addMessage(
152 $this->lng->txt('dir_preserved_backup') . ' ' . $absolut_old_dir,
154 )
155 );
156 } else {
157 mkdir($absolut_new_dir, 0775, true);
158 $this->recursiveCopy($absolut_old_dir, $absolut_new_dir);
159 $this->getMessageStack()->addMessage(
161 $this->lng->txt('dir_copied_from') . ' ' . $absolut_old_dir . ' ' . $this->lng->txt('sty_copy_to') . ' ' . $absolut_new_dir,
163 )
164 );
165 if (!$has_references) {
166 $this->recursiveRemoveDir($skin_dir . $old_dir);
167 $this->getMessageStack()->addMessage(
169 $this->lng->txt('dir_deleted') . ' ' . $absolut_old_dir,
171 )
172 );
173 } else {
174 $this->getMessageStack()->addMessage(
176 $this->lng->txt('dir_preserved_linked') . ' ' . $absolut_old_dir,
178 )
179 );
180 }
181 }
182 }
183
188 public function recursiveCopy(string $src, string $dest): void
189 {
190 foreach (scandir($src) as $file) {
191 $src_file = rtrim($src, '/') . '/' . $file;
192 $dest_file = rtrim($dest, '/') . '/' . $file;
193 if (!is_readable($src_file)) {
195 }
196 if (substr($file, 0, 1) != '.') {
197 if (is_dir($src_file)) {
198 if (!file_exists($dest_file)) {
199 try {
200 mkdir($dest_file);
201 } catch (Exception $e) {
202 throw new ilSystemStyleException(
204 'Copy ' . $src_file . ' to ' . $dest_file . ' Error: ' . $e
205 );
206 }
207 }
208 $this->recursiveCopy($src_file, $dest_file);
209 } else {
210 try {
211 copy($src_file, $dest_file);
212 } catch (Exception $e) {
213 throw new ilSystemStyleException(
215 'Copy ' . $src_file . ' to ' . $dest_file . ' Error: ' . $e
216 );
217 }
218 }
219 }
220 }
221 }
222
223
224
226 {
228 }
229
231 {
232 $this->message_stack = $message_stack;
233 }
234}
File System Helper, to reduce deps.
createResourceDirectory(string $source, string $target)
Creates a resource directory (sound, images or fonts) by copying from the source (mostly delos)
removeResourceDirectory(string $skin_dir, string $dir, bool $is_linked)
Deletes a resource directory.
setMessageStack(ilSystemStyleMessageStack $message_stack)
move(string $from, string $to)
Used to move a complete directory of a skin.
__construct(ilLanguage $lng, ilSystemStyleMessageStack $message_stack)
recursiveRemoveDir(string $dir)
Recursive delete of a folder.
changeResourceDirectory(string $skin_dir, string $new_dir, string $old_dir, bool $has_references)
Alters the name/path of a resource directory.
saveDeleteFile(string $file_path)
Deletes a given file in the container.
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.
language handling
Class for advanced editing exception handling in ILIAS.
Used to stack messages to be shown to the user.
$source
Definition: metadata.php:93