Connection.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @category Thinkopen
  4. * @package Mooses_Mongodb_Mongo
  5. * @copyright Thinkopen s.r.l.
  6. * @license New BSD License
  7. * @author Coen Hyde
  8. */
  9. class Mooses_Mongodb_Mongo_Connection extends MongoClient
  10. {
  11. static protected $_availableOptions = array(
  12. 'persist',
  13. 'connectTimeoutMS',
  14. 'replicaSet'
  15. );
  16. protected $_connectionInfo = array();
  17. public function __construct($connectionString = null, array $options = array())
  18. {
  19. Mooses_Mongodb_Mongo::init();
  20. // Set the server to local host if one was not provided
  21. $_iniConfig = Top::getConfig();
  22. if(strlen($_iniConfig->mongodb->host) > 0){
  23. $_connectionHost = $_iniConfig->mongodb->host;
  24. } else {
  25. $_connectionHost = "127.0.0.1";
  26. }
  27. if (is_null($connectionString)) $connectionString = $_connectionHost;
  28. // Force mongo to connect only when we need to
  29. $options['connect'] = false;
  30. $connectionInfo = self::parseConnectionString($connectionString);
  31. $this->_connectionInfo = array_merge($options, $connectionInfo);
  32. return parent::__construct($connectionString, $options);
  33. }
  34. /**
  35. * Get some info about this connection
  36. *
  37. * @return array
  38. */
  39. public function getConnectionInfo()
  40. {
  41. return $this->_connectionInfo;
  42. }
  43. /**
  44. * Get the actual connection string used for this connection. This differs from __toString in
  45. * that __toString returns a string representation of the connection, not the connection string used
  46. *
  47. * @return array
  48. */
  49. public function getActualConnectionString()
  50. {
  51. return $this->_connectionInfo['connectionString'];
  52. }
  53. /**
  54. * Get the database this connection is connection to
  55. *
  56. * @return string
  57. */
  58. public function getDatabase()
  59. {
  60. if (!isset($this->_connectionInfo['database'])) return null;
  61. return $this->_connectionInfo['database'];
  62. }
  63. /**
  64. * Get a list of the hosts this connection is connection to
  65. *
  66. * @return array
  67. */
  68. public function getHosts()
  69. {
  70. return $this->_connectionInfo['hosts'];
  71. }
  72. /**
  73. * Parse the connection string
  74. *
  75. * @param $connectionString
  76. * @return array
  77. */
  78. public static function parseConnectionString($connectionString)
  79. {
  80. $connectionInfo = array(
  81. 'connectionString' => $connectionString
  82. );
  83. // Remove mongodb protocol string
  84. if (substr($connectionString, 0, 10) == 'mongodb://') {
  85. $connectionString = substr($connectionString, 10);
  86. }
  87. // Is there a database name
  88. if ($pos = strrpos($connectionString, '/')) {
  89. $connectionInfo['database'] = substr($connectionString, $pos+1);
  90. $connectionString = substr($connectionString, 0, $pos);
  91. }
  92. // Fetch Hosts
  93. if (!empty($connectionString)) {
  94. $hostStrings = explode(',', $connectionString);
  95. $connectionInfo['hosts'] = array();
  96. foreach ($hostStrings as $hostString) {
  97. $connectionInfo['hosts'][] = self::parseHostString($hostString);
  98. }
  99. }
  100. return $connectionInfo;
  101. }
  102. /**
  103. * Parse a host string
  104. *
  105. * @param $hostString
  106. * @return array
  107. */
  108. public static function parseHostString($hostString)
  109. {
  110. $hostInfo = array();
  111. // Are we authenticating with a username and password?
  112. if ($pos = strpos($hostString, '@')) {
  113. $authString = substr($hostString, 0, $pos);
  114. $data = explode(':', $authString);
  115. $hostInfo['username'] = $data[0];
  116. if (count($data) > 1) {
  117. $hostInfo['password'] = $data[1];
  118. }
  119. $hostString = substr($hostString, $pos+1);
  120. }
  121. // Grab host and port
  122. $data = explode(':', $hostString);
  123. $hostInfo['host'] = $data[0];
  124. if (count($data) > 1) {
  125. $hostInfo['port'] = $data[1];
  126. }
  127. return $hostInfo;
  128. }
  129. /**
  130. * Get available options
  131. *
  132. * @return array
  133. */
  134. static public function getAvailableOptions()
  135. {
  136. return static::$_availableOptions;
  137. }
  138. }