ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilFileSystemDirectoryCopiedRecursivelyObjective.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 use ILIAS\Setup;
22 
23 class ilFileSystemDirectoryCopiedRecursivelyObjective implements Setup\Objective
24 {
25  protected string $source_folder;
26  protected string $target_folder;
27  protected bool $data_dir;
28  protected bool $bare;
29 
38  public function __construct(
39  string $source_folder,
40  string $target_folder,
41  bool $data_dir = false,
42  bool $bare = false
43  ) {
44  $this->source_folder = $source_folder;
45  $this->target_folder = $target_folder;
46  $this->data_dir = $data_dir;
47  $this->bare = $bare;
48  }
49 
53  public function getHash(): string
54  {
55  return hash("sha256", self::class . $this->getSourceName($this->source_folder) . $this->target_folder);
56  }
57 
61  public function getLabel(): string
62  {
63  $source = $this->getSourceName($this->source_folder);
64  return "Copy directory from $source to $this->target_folder.";
65  }
66 
70  public function isNotable(): bool
71  {
72  return true;
73  }
74 
78  public function getPreconditions(Setup\Environment $environment): array
79  {
80  return [
82  ];
83  }
84 
88  public function achieve(Setup\Environment $environment): Setup\Environment
89  {
90  $source = $this->source_folder;
91 
92  if (!$this->bare) {
94  $ini = $environment->getResource(Setup\Environment::RESOURCE_ILIAS_INI);
95 
96  $root = $ini->readVariable("server", "absolute_path");
97 
98  if ($this->data_dir) {
99  $root = $ini->readVariable("clients", "datadir");
100  }
101 
102  $source = $root . DIRECTORY_SEPARATOR . $this->source_folder;
103  }
104 
105  if (file_exists($this->target_folder)) {
106  $this->deleteDirRecursive($this->target_folder);
107  } else {
108  mkdir($this->target_folder, 0755);
109  }
110 
111  $iterator = new RecursiveIteratorIterator(
112  new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS),
113  RecursiveIteratorIterator::SELF_FIRST
114  );
115 
117  foreach ($iterator as $item) {
118  if ($item->isDir()) {
119  mkdir($this->target_folder . DIRECTORY_SEPARATOR . $iterator->getSubPathname());
120  } else {
121  copy($item->getPathname(), $this->target_folder . DIRECTORY_SEPARATOR . $iterator->getSubPathname());
122  }
123  }
124 
125  return $environment;
126  }
127 
131  public function isApplicable(Setup\Environment $environment): bool
132  {
133  $source = $this->source_folder;
134 
135  if (!$this->bare) {
137  $ini = $environment->getResource(Setup\Environment::RESOURCE_ILIAS_INI);
138 
139  $root = $ini->readVariable("server", "absolute_path");
140  if ($this->data_dir) {
141  $root = $ini->readVariable("clients", "datadir");
142  }
143 
144  $source = $root . DIRECTORY_SEPARATOR . $this->source_folder;
145  }
146 
147  return
148  file_exists($source) &&
149  is_writable(pathinfo($this->target_folder, PATHINFO_DIRNAME))
150  ;
151  }
152 
153  protected function deleteDirRecursive(string $path): void
154  {
155  $files = new RecursiveIteratorIterator(
156  new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
157  RecursiveIteratorIterator::CHILD_FIRST
158  );
159 
160  foreach ($files as $file_info) {
161  if ($file_info->isDir()) {
162  rmdir($file_info->getRealPath());
163  continue;
164  }
165  unlink($file_info->getRealPath());
166  }
167  }
168 
169  protected function getSourceName(string $source): string
170  {
171  if ($source !== "") {
172  return $source;
173  }
174 
175  if ($this->data_dir) {
176  return "ilias data dir";
177  }
178 
179  return "ilias root";
180  }
181 }
$path
Definition: ltiservices.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
__construct(string $source_folder, string $target_folder, bool $data_dir=false, bool $bare=false)
Copies a directory from ILIAS root or from the outer ILIAS data directory depending on the flag $data...
$ini
Definition: raiseError.php:4