ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilDatabaseDumpedToDirectoryObjective.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 use ILIAS\Setup;
22 
23 class ilDatabaseDumpedToDirectoryObjective implements Setup\Objective
24 {
25  protected string $target;
26  protected MysqlDumper $dumper;
27 
28  public function __construct(string $target, MysqlDumper $dumper)
29  {
30  $this->target = $target;
31  $this->dumper = $dumper;
32  }
33 
37  public function getHash(): string
38  {
39  return hash("sha256", self::class . $this->target);
40  }
41 
45  public function getLabel(): string
46  {
47  return "Dump database to $this->target/" . MysqlIfsnopDumper::FILE_NAME;
48  }
49 
53  public function isNotable(): bool
54  {
55  return true;
56  }
57 
61  public function getPreconditions(Setup\Environment $environment): array
62  {
63  return [
66  ];
67  }
68 
72  public function achieve(Setup\Environment $environment): Setup\Environment
73  {
74  if (file_exists($this->target)) {
75  $this->deleteDirRecursive($this->target);
76  }
77  mkdir($this->target, 0755);
78 
79  $client_ini = $environment->getResource(Setup\Environment::RESOURCE_CLIENT_INI);
80  $host = $client_ini->readVariable("db", "host");
81  $user = $client_ini->readVariable("db", "user");
82  $password = $client_ini->readVariable("db", "pass");
83  $name = $client_ini->readVariable("db", "name");
84  $port = $client_ini->readVariable("db", "port");
85 
86  $this->dumper->createDump($host, $user, $password, $name, $port, $this->target);
87 
88  return $environment;
89  }
90 
94  public function isApplicable(Setup\Environment $environment): bool
95  {
96  return is_writable(pathinfo($this->target, PATHINFO_DIRNAME));
97  }
98 
99  protected function deleteDirRecursive(string $path): void
100  {
101  $files = new RecursiveIteratorIterator(
102  new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
103  RecursiveIteratorIterator::CHILD_FIRST
104  );
105 
106  foreach ($files as $file_info) {
107  if ($file_info->isDir()) {
108  rmdir($file_info->getRealPath());
109  continue;
110  }
111  unlink($file_info->getRealPath());
112  }
113 
114  rmdir($path);
115  }
116 }
$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