Elgg  Version master
DbConfig.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Config;
6 
13 class DbConfig {
14 
15  const READ = 'read';
16  const WRITE = 'write';
17  const READ_WRITE = 'readwrite';
18 
22  protected ?array $db;
23 
27  protected ?string $dbprefix;
28 
32  protected ?string $dbhost;
33 
37  protected ?int $dbport;
38 
42  protected ?string $dbuser;
43 
47  protected ?string $dbpass;
48 
52  protected ?string $dbname;
53 
57  protected ?string $dbencoding;
58 
72  public function __construct(\stdClass $config) {
73  foreach (array_keys(get_class_vars(__CLASS__)) as $prop) {
74  $this->{$prop} = $config->{$prop} ?? null;
75  }
76  }
77 
85  public static function fromElggConfig(Config $config): DbConfig {
86  $obj = new \stdClass();
87  foreach (array_keys(get_class_vars(__CLASS__)) as $prop) {
88  $obj->{$prop} = $config->{$prop};
89  }
90 
91  return new self($obj);
92  }
93 
99  public function getTablePrefix(): string {
100  return (string) $this->dbprefix;
101  }
102 
108  public function isDatabaseSplit(): bool {
109  return $this->db['split'] ?? false;
110  }
111 
131  public function getConnectionConfig(string $type = self::READ_WRITE): array {
132  switch ($type) {
133  case self::READ:
134  case self::WRITE:
136  break;
137  default:
139  break;
140  }
141 
142  $config['encoding'] = $this->dbencoding ?: 'utf8';
143  $config['prefix'] = $this->dbprefix;
144 
145  return $config;
146  }
147 
153  protected function getGeneralConnectionConfig(): array {
154  return [
155  'host' => $this->dbhost,
156  'port' => $this->dbport,
157  'user' => $this->dbuser,
158  'password' => $this->dbpass,
159  'database' => $this->dbname,
160  ];
161  }
162 
170  protected function getParticularConnectionConfig(string $type): array {
171  if (array_key_exists('dbhost', $this->db[$type])) {
172  // single connection
173  return [
174  'host' => $this->db[$type]['dbhost'],
175  'port' => $this->db[$type]['dbport'],
176  'user' => $this->db[$type]['dbuser'],
177  'password' => $this->db[$type]['dbpass'],
178  'database' => $this->db[$type]['dbname'],
179  ];
180  }
181 
182  // new style multiple connections
183  $index = array_rand($this->db[$type]);
184  return [
185  'host' => $this->db[$type][$index]['dbhost'],
186  'port' => $this->db[$type][$index]['dbport'],
187  'user' => $this->db[$type][$index]['dbuser'],
188  'password' => $this->db[$type][$index]['dbpass'],
189  'database' => $this->db[$type][$index]['dbname'],
190  ];
191  }
192 }
Database configuration service.
Definition: DbConfig.php:13
getGeneralConnectionConfig()
Get the read/write database connection information.
Definition: DbConfig.php:153
static fromElggConfig(Config $config)
Construct from an Elgg Config.
Definition: DbConfig.php:85
$type
Definition: delete.php:21
isDatabaseSplit()
Are the read and write connections separate?
Definition: DbConfig.php:108
$CONFIG dbencoding
The database encoding.
getConnectionConfig(string $type=self::READ_WRITE)
Get the connection configuration.
Definition: DbConfig.php:131
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
getTablePrefix()
Get the database table prefix.
Definition: DbConfig.php:99
getParticularConnectionConfig(string $type)
Get connection information for reading or writing.
Definition: DbConfig.php:170
__construct(\stdClass $config)
Constructor.
Definition: DbConfig.php:72
$index
Definition: gallery.php:40