ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilFileSystemDirectoryCopiedRecursivelyObjective.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
25 {
34  public function __construct(protected string $source_folder, protected string $target_folder, protected bool $data_dir = false, protected bool $bare = false)
35  {
36  }
37 
41  public function getHash(): string
42  {
43  return hash("sha256", self::class . $this->getSourceName($this->source_folder) . $this->target_folder);
44  }
45 
49  public function getLabel(): string
50  {
51  $source = $this->getSourceName($this->source_folder);
52  return "Copy directory from $source to $this->target_folder.";
53  }
54 
58  public function isNotable(): bool
59  {
60  return true;
61  }
62 
66  public function getPreconditions(Environment $environment): array
67  {
68  return [
70  ];
71  }
72 
76  public function achieve(Environment $environment): Environment
77  {
78  $source = $this->source_folder;
79 
80  if (!$this->bare) {
82  $ini = $environment->getResource(Environment::RESOURCE_ILIAS_INI);
83 
84  $root = $ini->readVariable("server", "absolute_path");
85 
86  if ($this->data_dir) {
87  $root = $ini->readVariable("clients", "datadir");
88  }
89 
90  $source = $root . DIRECTORY_SEPARATOR . $this->source_folder;
91  }
92 
93  if (file_exists($this->target_folder)) {
94  $this->deleteDirRecursive($this->target_folder);
95  } else {
96  mkdir($this->target_folder, 0755);
97  }
98 
99  $iterator = new RecursiveIteratorIterator(
100  new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS),
101  RecursiveIteratorIterator::SELF_FIRST
102  );
103 
105  foreach ($iterator as $item) {
106  if ($item->isDir()) {
107  mkdir($this->target_folder . DIRECTORY_SEPARATOR . $iterator->getSubPathname());
108  } else {
109  copy($item->getPathname(), $this->target_folder . DIRECTORY_SEPARATOR . $iterator->getSubPathname());
110  }
111  }
112 
113  return $environment;
114  }
115 
119  public function isApplicable(Environment $environment): bool
120  {
121  $source = $this->source_folder;
122 
123  if (!$this->bare) {
125  $ini = $environment->getResource(Environment::RESOURCE_ILIAS_INI);
126 
127  $root = $ini->readVariable("server", "absolute_path");
128  if ($this->data_dir) {
129  $root = $ini->readVariable("clients", "datadir");
130  }
131 
132  $source = $root . DIRECTORY_SEPARATOR . $this->source_folder;
133  }
134 
135  return
136  file_exists($source) &&
137  is_writable(pathinfo($this->target_folder, PATHINFO_DIRNAME))
138  ;
139  }
140 
141  protected function deleteDirRecursive(string $path): void
142  {
143  $files = new RecursiveIteratorIterator(
144  new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
145  RecursiveIteratorIterator::CHILD_FIRST
146  );
147 
148  foreach ($files as $file_info) {
149  if ($file_info->isDir()) {
150  rmdir($file_info->getRealPath());
151  continue;
152  }
153  unlink($file_info->getRealPath());
154  }
155  }
156 
157  protected function getSourceName(string $source): string
158  {
159  if ($source !== "") {
160  return $source;
161  }
162 
163  if ($this->data_dir) {
164  return "ilias data dir";
165  }
166 
167  return "ilias root";
168  }
169 }
isNotable()
Get to know if this is an interesting objective for a human.
getPreconditions(Environment $environment)
Objectives might depend on other objectives.if the objective is not achievableObjective[] ...
An objective is a desired state of the system that is supposed to be created by the setup...
Definition: Objective.php:30
isApplicable(Environment $environment)
Get to know whether the objective is applicable.
$path
Definition: ltiservices.php:29
getHash()
Get a hash for this objective.The hash of two objectives must be the same, if they are the same objec...
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
achieve(Environment $environment)
Objectives can be achieved.
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
__construct(protected string $source_folder, protected string $target_folder, protected bool $data_dir=false, protected 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:20