123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace Elgg\Database;
- class Config {
- const READ = 'read';
- const WRITE = 'write';
- const READ_WRITE = 'readwrite';
-
- protected $config;
-
- public function __construct(\stdClass $config) {
- $this->config = $config;
- }
-
- public function getTablePrefix() {
- return $this->config->dbprefix;
- }
-
- public function isQueryCacheEnabled() {
- if (isset($this->config->db_disable_query_cache)) {
- return !$this->config->db_disable_query_cache;
- }
- return true;
- }
-
- public function isDatabaseSplit() {
- if (isset($this->config->db) && isset($this->config->db['split'])) {
- return $this->config->db['split'];
- }
-
- if (isset($this->config->db) && isset($this->config->db->split)) {
- return $this->config->db->split;
- }
- return false;
- }
-
- public function getConnectionConfig($type = self::READ_WRITE) {
- $config = array();
- switch ($type) {
- case self::READ:
- case self::WRITE:
- $config = $this->getParticularConnectionConfig($type);
- break;
- default:
- $config = $this->getGeneralConnectionConfig();
- break;
- }
- return $config;
- }
-
- protected function getGeneralConnectionConfig() {
- return array(
- 'host' => $this->config->dbhost,
- 'user' => $this->config->dbuser,
- 'password' => $this->config->dbpass,
- 'database' => $this->config->dbname,
- );
- }
-
- protected function getParticularConnectionConfig($type) {
- if (is_object($this->config->db[$type])) {
-
- $config = array(
- 'host' => $this->config->db[$type]->dbhost,
- 'user' => $this->config->db[$type]->dbuser,
- 'password' => $this->config->db[$type]->dbpass,
- 'database' => $this->config->db[$type]->dbname,
- );
- } else if (array_key_exists('dbhost', $this->config->db[$type])) {
-
- $config = array(
- 'host' => $this->config->db[$type]['dbhost'],
- 'user' => $this->config->db[$type]['dbuser'],
- 'password' => $this->config->db[$type]['dbpass'],
- 'database' => $this->config->db[$type]['dbname'],
- );
- } else if (is_object(current($this->config->db[$type]))) {
-
- $index = array_rand($this->config->db[$type]);
- $config = array(
- 'host' => $this->config->db[$type][$index]->dbhost,
- 'user' => $this->config->db[$type][$index]->dbuser,
- 'password' => $this->config->db[$type][$index]->dbpass,
- 'database' => $this->config->db[$type][$index]->dbname,
- );
- } else {
-
- $index = array_rand($this->config->db[$type]);
- $config = array(
- 'host' => $this->config->db[$type][$index]['dbhost'],
- 'user' => $this->config->db[$type][$index]['dbuser'],
- 'password' => $this->config->db[$type][$index]['dbpass'],
- 'database' => $this->config->db[$type][$index]['dbname'],
- );
- }
- return $config;
- }
- }
|