ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilDatabaseSetupConfig.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 use ILIAS\Setup;
23 
24 class ilDatabaseSetupConfig implements Setup\Config
25 {
26  public const DEFAULT_COLLATION = "utf8_general_ci";
27  public const DEFAULT_PATH_TO_DB_DUMP = "./setup/sql/ilias3.sql";
28 
29  protected string $type;
30 
31  protected string $host;
32 
33  protected ?int $port = null;
34 
35  protected string $database;
36 
37  protected bool $create_database;
38 
39  protected string $collation;
40 
41  protected string $user;
42 
43  protected ?\ILIAS\Data\Password $password = null;
44 
45  protected string $path_to_db_dump;
46 
48 
49  public function __construct(
50  string $type,
51  string $host,
52  string $database,
53  string $user,
54  Password $password = null,
55  bool $create_database = true,
56  string $collation = null,
57  int $port = null,
58  string $path_to_db_dump = null
59  ) {
60  if (!in_array($type, \ilDBConstants::getInstallableTypes())) {
61  throw new \InvalidArgumentException(
62  "Unknown database type: $type"
63  );
64  }
65  if ($collation && !in_array(trim($collation), \ilDBConstants::getAvailableCollations())) {
66  throw new \InvalidArgumentException(
67  "Unknown collation: $collation"
68  );
69  }
70  $this->type = trim($type);
71  $this->host = trim($host);
72  $this->database = trim($database);
73  $this->user = trim($user);
74  $this->password = $password;
75  $this->create_database = $create_database;
76  $this->collation = $collation ? trim($collation) : self::DEFAULT_COLLATION;
77  $this->port = $port;
78  $this->path_to_db_dump = $path_to_db_dump ?? self::DEFAULT_PATH_TO_DB_DUMP;
79  }
80 
81  public function getType(): string
82  {
83  return $this->type;
84  }
85 
86  public function getHost(): string
87  {
88  return $this->host;
89  }
90 
91  public function getPort(): ?int
92  {
93  return $this->port;
94  }
95 
96  public function getDatabase(): string
97  {
98  return $this->database;
99  }
100 
101  public function getCreateDatabase(): bool
102  {
103  return $this->create_database;
104  }
105 
106  public function getCollation(): string
107  {
108  return $this->collation;
109  }
110 
111  public function getUser(): string
112  {
113  return $this->user;
114  }
115 
116  public function getPassword(): ?Password
117  {
118  return $this->password;
119  }
120 
121  public function getPathToDBDump(): string
122  {
123  return $this->path_to_db_dump;
124  }
125 
129  public function toMockIniFile(): \ilIniFile
130  {
131  return new class ($this) extends \ilIniFile {
132  protected ilDatabaseSetupConfig $config;
140  public function readVariable(string $a_group, string $a_var_name): string
141  {
142  if ($a_group !== "db") {
143  throw new \LogicException(
144  "Can only access db-config via this mock."
145  );
146  }
147  switch ($a_var_name) {
148  case "user":
149  return $this->config->getUser();
150  case "host":
151  return $this->config->getHost();
152  case "port":
153  return (string) $this->config->getPort();
154  case "pass":
155  $pw = $this->config->getPassword();
156  return $pw ? $pw->toString() : "";
157  case "name":
158  return $this->config->getDatabase();
159  case "type":
160  return $this->config->getType();
161  default:
162  throw new \LogicException(
163  "Cannot provide variable '$a_var_name'"
164  );
165  }
166  }
167 
168  public function __construct(\ilDatabaseSetupConfig $config)
169  {
170  $this->config = $config;
171  }
172  public function read(): bool
173  {
174  throw new \LogicException("Just a mock here...");
175  }
176  public function parse(): bool
177  {
178  throw new \LogicException("Just a mock here...");
179  }
180  public function fixIniFile(): void
181  {
182  throw new \LogicException("Just a mock here...");
183  }
184  public function write(): bool
185  {
186  throw new \LogicException("Just a mock here...");
187  }
188  public function show(): string
189  {
190  throw new \LogicException("Just a mock here...");
191  }
192  public function getGroupCount(): int
193  {
194  throw new \LogicException("Just a mock here...");
195  }
196  public function readGroups(): array
197  {
198  throw new \LogicException("Just a mock here...");
199  }
200  public function groupExists(string $a_group_name): bool
201  {
202  throw new \LogicException("Just a mock here...");
203  }
204  public function readGroup(string $a_group_name): array
205  {
206  throw new \LogicException("Just a mock here...");
207  }
208  public function addGroup(string $a_group_name): bool
209  {
210  throw new \LogicException("Just a mock here...");
211  }
212  public function removeGroup(string $a_group_name): bool
213  {
214  throw new \LogicException("Just a mock here...");
215  }
216  public function variableExists(string $a_group, string $a_var_name): bool
217  {
218  throw new \LogicException("Just a mock here...");
219  }
220  public function setVariable(string $a_group_name, string $a_var_name, string $a_var_value): bool
221  {
222  throw new \LogicException("Just a mock here...");
223  }
224  public function error(string $a_errmsg): bool
225  {
226  throw new \LogicException("Just a mock here...");
227  }
228  public function getError(): string
229  {
230  throw new \LogicException("Just a mock here...");
231  }
232  };
233  }
234 }
toMockIniFile()
Adapter to current database-handling via a mock of .
A password is used as part of credentials for authentication.
Definition: Password.php:16
static getAvailableCollations()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(string $type, string $host, string $database, string $user, Password $password=null, bool $create_database=true, string $collation=null, int $port=null, string $path_to_db_dump=null)
static getInstallableTypes()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...