Elgg  Version 4.3
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 
19  protected $db;
20  protected $dbprefix;
21  protected $dbhost;
22  protected $dbport;
23  protected $dbuser;
24  protected $dbpass;
25  protected $dbname;
26  protected $dbencoding;
27 
41  public function __construct(\stdClass $config) {
42  foreach (array_keys(get_class_vars(__CLASS__)) as $prop) {
43  $this->{$prop} = isset($config->{$prop}) ? $config->{$prop} : null;
44  }
45  }
46 
54  public static function fromElggConfig(Config $config) {
55  $obj = new \stdClass();
56  foreach (array_keys(get_class_vars(__CLASS__)) as $prop) {
57  $obj->{$prop} = $config->{$prop};
58  }
59  return new self($obj);
60  }
61 
67  public function getTablePrefix() {
68  return (string) $this->dbprefix;
69  }
70 
76  public function isDatabaseSplit() {
77  if (isset($this->db['split'])) {
78  return $this->db['split'];
79  }
80 
81  // this was the recommend structure from Elgg 1.0 to 1.8
82  if (isset($this->db->split)) {
83  elgg_deprecated_notice('Database configuration should be updated to an array. Check the settings.example.php', '4.3');
84 
85  return $this->db->split;
86  }
87 
88  return false;
89  }
90 
109  public function getConnectionConfig($type = self::READ_WRITE) {
110  switch ($type) {
111  case self::READ:
112  case self::WRITE:
114  break;
115  default:
117  break;
118  }
119 
120  $config['encoding'] = $this->dbencoding ? $this->dbencoding : 'utf8';
121  $config['prefix'] = $this->dbprefix;
122 
123  return $config;
124  }
125 
131  protected function getGeneralConnectionConfig() {
132  return [
133  'host' => $this->dbhost,
134  'port' => $this->dbport,
135  'user' => $this->dbuser,
136  'password' => $this->dbpass,
137  'database' => $this->dbname,
138  ];
139  }
140 
147  protected function getParticularConnectionConfig($type) {
148  if (is_object($this->db[$type])) {
149  elgg_deprecated_notice('Database configuration should be updated to an array. Check the settings.example.php', '4.3');
150 
151  // old style single connection (Elgg < 1.9)
152  $config = [
153  'host' => $this->db[$type]->dbhost,
154  'port' => $this->db[$type]->dbport,
155  'user' => $this->db[$type]->dbuser,
156  'password' => $this->db[$type]->dbpass,
157  'database' => $this->db[$type]->dbname,
158  ];
159  } else if (array_key_exists('dbhost', $this->db[$type])) {
160  // new style single connection
161  $config = [
162  'host' => $this->db[$type]['dbhost'],
163  'port' => $this->db[$type]['dbport'],
164  'user' => $this->db[$type]['dbuser'],
165  'password' => $this->db[$type]['dbpass'],
166  'database' => $this->db[$type]['dbname'],
167  ];
168  } else if (is_object(current($this->db[$type]))) {
169  elgg_deprecated_notice('Database configuration should be updated to an array. Check the settings.example.php', '4.3');
170 
171  // old style multiple connections
172  $index = array_rand($this->db[$type]);
173  $config = [
174  'host' => $this->db[$type][$index]->dbhost,
175  'port' => $this->db[$type][$index]->dbport,
176  'user' => $this->db[$type][$index]->dbuser,
177  'password' => $this->db[$type][$index]->dbpass,
178  'database' => $this->db[$type][$index]->dbname,
179  ];
180  } else {
181  // new style multiple connections
182  $index = array_rand($this->db[$type]);
183  $config = [
184  'host' => $this->db[$type][$index]['dbhost'],
185  'port' => $this->db[$type][$index]['dbport'],
186  'user' => $this->db[$type][$index]['dbuser'],
187  'password' => $this->db[$type][$index]['dbpass'],
188  'database' => $this->db[$type][$index]['dbname'],
189  ];
190  }
191 
192  return $config;
193  }
194 }
Database configuration service.
Definition: DbConfig.php:13
getGeneralConnectionConfig()
Get the read/write database connection information.
Definition: DbConfig.php:131
static fromElggConfig(Config $config)
Construct from an Elgg Config.
Definition: DbConfig.php:54
elgg_deprecated_notice(string $msg, string $dep_version)
Log a notice about deprecated use of a function, view, etc.
Definition: deprecation.php:52
getConnectionConfig($type=self::READ_WRITE)
Get the connection configuration.
Definition: DbConfig.php:109
getParticularConnectionConfig($type)
Get connection information for reading or writing.
Definition: DbConfig.php:147
$type
Definition: delete.php:21
isDatabaseSplit()
Are the read and write connections separate?
Definition: DbConfig.php:76
$CONFIG dbencoding
The database encoding.
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
getTablePrefix()
Get the database table prefix.
Definition: DbConfig.php:67
__construct(\stdClass $config)
Constructor.
Definition: DbConfig.php:41
$index
Definition: gallery.php:40