Config.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Elgg\Database;
  3. /**
  4. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * @access private
  7. *
  8. * @package Elgg.Core
  9. * @subpackage Database
  10. * @since 1.9.0
  11. */
  12. class Config {
  13. const READ = 'read';
  14. const WRITE = 'write';
  15. const READ_WRITE = 'readwrite';
  16. /** @var \stdClass $config Elgg's config object */
  17. protected $config;
  18. /**
  19. * Constructor
  20. *
  21. * @param \stdClass $config Elgg's $CONFIG object
  22. */
  23. public function __construct(\stdClass $config) {
  24. $this->config = $config;
  25. }
  26. /**
  27. * Get the database table prefix
  28. *
  29. * @return string
  30. */
  31. public function getTablePrefix() {
  32. return $this->config->dbprefix;
  33. }
  34. /**
  35. * Is the query cache enabled?
  36. *
  37. * @return bool
  38. */
  39. public function isQueryCacheEnabled() {
  40. if (isset($this->config->db_disable_query_cache)) {
  41. return !$this->config->db_disable_query_cache;
  42. }
  43. return true;
  44. }
  45. /**
  46. * Are the read and write connections separate?
  47. *
  48. * @return bool
  49. */
  50. public function isDatabaseSplit() {
  51. if (isset($this->config->db) && isset($this->config->db['split'])) {
  52. return $this->config->db['split'];
  53. }
  54. // this was the recommend structure from Elgg 1.0 to 1.8
  55. if (isset($this->config->db) && isset($this->config->db->split)) {
  56. return $this->config->db->split;
  57. }
  58. return false;
  59. }
  60. /**
  61. * Get the connection configuration
  62. *
  63. * The parameters are in an array like this:
  64. * array(
  65. * 'host' => 'xxx',
  66. * 'user' => 'xxx',
  67. * 'password' => 'xxx',
  68. * 'database' => 'xxx',
  69. * )
  70. *
  71. * @param int $type The connection type: READ, WRITE, READ_WRITE
  72. * @return array
  73. */
  74. public function getConnectionConfig($type = self::READ_WRITE) {
  75. $config = array();
  76. switch ($type) {
  77. case self::READ:
  78. case self::WRITE:
  79. $config = $this->getParticularConnectionConfig($type);
  80. break;
  81. default:
  82. $config = $this->getGeneralConnectionConfig();
  83. break;
  84. }
  85. return $config;
  86. }
  87. /**
  88. * Get the read/write database connection information
  89. *
  90. * @return array
  91. */
  92. protected function getGeneralConnectionConfig() {
  93. return array(
  94. 'host' => $this->config->dbhost,
  95. 'user' => $this->config->dbuser,
  96. 'password' => $this->config->dbpass,
  97. 'database' => $this->config->dbname,
  98. );
  99. }
  100. /**
  101. * Get connection information for reading or writing
  102. *
  103. * @param string $type Connection type: 'write' or 'read'
  104. * @return array
  105. */
  106. protected function getParticularConnectionConfig($type) {
  107. if (is_object($this->config->db[$type])) {
  108. // old style single connection (Elgg < 1.9)
  109. $config = array(
  110. 'host' => $this->config->db[$type]->dbhost,
  111. 'user' => $this->config->db[$type]->dbuser,
  112. 'password' => $this->config->db[$type]->dbpass,
  113. 'database' => $this->config->db[$type]->dbname,
  114. );
  115. } else if (array_key_exists('dbhost', $this->config->db[$type])) {
  116. // new style single connection
  117. $config = array(
  118. 'host' => $this->config->db[$type]['dbhost'],
  119. 'user' => $this->config->db[$type]['dbuser'],
  120. 'password' => $this->config->db[$type]['dbpass'],
  121. 'database' => $this->config->db[$type]['dbname'],
  122. );
  123. } else if (is_object(current($this->config->db[$type]))) {
  124. // old style multiple connections
  125. $index = array_rand($this->config->db[$type]);
  126. $config = array(
  127. 'host' => $this->config->db[$type][$index]->dbhost,
  128. 'user' => $this->config->db[$type][$index]->dbuser,
  129. 'password' => $this->config->db[$type][$index]->dbpass,
  130. 'database' => $this->config->db[$type][$index]->dbname,
  131. );
  132. } else {
  133. // new style multiple connections
  134. $index = array_rand($this->config->db[$type]);
  135. $config = array(
  136. 'host' => $this->config->db[$type][$index]['dbhost'],
  137. 'user' => $this->config->db[$type][$index]['dbuser'],
  138. 'password' => $this->config->db[$type][$index]['dbpass'],
  139. 'database' => $this->config->db[$type][$index]['dbname'],
  140. );
  141. }
  142. return $config;
  143. }
  144. }