Elgg  Version 3.0
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 $dbuser;
23  protected $dbpass;
24  protected $dbname;
25  protected $dbencoding;
26 
39  public function __construct(\stdClass $config) {
40  foreach (array_keys(get_class_vars(__CLASS__)) as $prop) {
41  $this->{$prop} = isset($config->{$prop}) ? $config->{$prop} : null;
42  }
43  }
44 
52  public static function fromElggConfig(Config $config) {
53  $obj = new \stdClass();
54  foreach (array_keys(get_class_vars(__CLASS__)) as $prop) {
55  $obj->{$prop} = $config->{$prop};
56  }
57  return new self($obj);
58  }
59 
65  public function getTablePrefix() {
66  return $this->dbprefix;
67  }
68 
74  public function isDatabaseSplit() {
75  if (isset($this->db['split'])) {
76  return $this->db['split'];
77  }
78 
79  // this was the recommend structure from Elgg 1.0 to 1.8
80  if (isset($this->db->split)) {
81  return $this->db->split;
82  }
83 
84  return false;
85  }
86 
105  public function getConnectionConfig($type = self::READ_WRITE) {
106  switch ($type) {
107  case self::READ:
108  case self::WRITE:
110  break;
111  default:
113  break;
114  }
115 
116  $config['encoding'] = $this->dbencoding ? $this->dbencoding : 'utf8';
117  $config['prefix'] = $this->dbprefix;
118 
119  return $config;
120  }
121 
127  protected function getGeneralConnectionConfig() {
128  return [
129  'host' => $this->dbhost,
130  'user' => $this->dbuser,
131  'password' => $this->dbpass,
132  'database' => $this->dbname,
133  ];
134  }
135 
142  protected function getParticularConnectionConfig($type) {
143  if (is_object($this->db[$type])) {
144  // old style single connection (Elgg < 1.9)
145  $config = [
146  'host' => $this->db[$type]->dbhost,
147  'user' => $this->db[$type]->dbuser,
148  'password' => $this->db[$type]->dbpass,
149  'database' => $this->db[$type]->dbname,
150  ];
151  } else if (array_key_exists('dbhost', $this->db[$type])) {
152  // new style single connection
153  $config = [
154  'host' => $this->db[$type]['dbhost'],
155  'user' => $this->db[$type]['dbuser'],
156  'password' => $this->db[$type]['dbpass'],
157  'database' => $this->db[$type]['dbname'],
158  ];
159  } else if (is_object(current($this->db[$type]))) {
160  // old style multiple connections
161  $index = array_rand($this->db[$type]);
162  $config = [
163  'host' => $this->db[$type][$index]->dbhost,
164  'user' => $this->db[$type][$index]->dbuser,
165  'password' => $this->db[$type][$index]->dbpass,
166  'database' => $this->db[$type][$index]->dbname,
167  ];
168  } else {
169  // new style multiple connections
170  $index = array_rand($this->db[$type]);
171  $config = [
172  'host' => $this->db[$type][$index]['dbhost'],
173  'user' => $this->db[$type][$index]['dbuser'],
174  'password' => $this->db[$type][$index]['dbpass'],
175  'database' => $this->db[$type][$index]['dbname'],
176  ];
177  }
178 
179  return $config;
180  }
181 }
WARNING: API IN FLUX.
Definition: DbConfig.php:13
getGeneralConnectionConfig()
Get the read/write database connection information.
Definition: DbConfig.php:127
static fromElggConfig(Config $config)
Construct from an Elgg Config.
Definition: DbConfig.php:52
getConnectionConfig($type=self::READ_WRITE)
Get the connection configuration.
Definition: DbConfig.php:105
getParticularConnectionConfig($type)
Get connection information for reading or writing.
Definition: DbConfig.php:142
$type
Definition: delete.php:21
isDatabaseSplit()
Are the read and write connections separate?
Definition: DbConfig.php:74
$CONFIG dbencoding
The database encoding.
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
getTablePrefix()
Get the database table prefix.
Definition: DbConfig.php:65
__construct(\stdClass $config)
Constructor.
Definition: DbConfig.php:39
$index
Definition: gallery.php:47