Connection.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. $_connectionHost = "127.0.0.1";
  21. if (is_null($connectionString)) $connectionString = $_connectionHost;
  22. // Force mongo to connect only when we need to
  23. $options['connect'] = false;
  24. $connectionInfo = self::parseConnectionString($connectionString);
  25. $this->_connectionInfo = array_merge($options, $connectionInfo);
  26. return parent::__construct($connectionString, $options);
  27. }
  28. /**
  29. * Get some info about this connection
  30. *
  31. * @return array
  32. */
  33. public function getConnectionInfo()
  34. {
  35. return $this->_connectionInfo;
  36. }
  37. /**
  38. * Get the actual connection string used for this connection. This differs from __toString in
  39. * that __toString returns a string representation of the connection, not the connection string used
  40. *
  41. * @return array
  42. */
  43. public function getActualConnectionString()
  44. {
  45. return $this->_connectionInfo['connectionString'];
  46. }
  47. /**
  48. * Get the database this connection is connection to
  49. *
  50. * @return string
  51. */
  52. public function getDatabase()
  53. {
  54. if (!isset($this->_connectionInfo['database'])) return null;
  55. return $this->_connectionInfo['database'];
  56. }
  57. /**
  58. * Get a list of the hosts this connection is connection to
  59. *
  60. * @return array
  61. */
  62. public function getHosts()
  63. {
  64. return $this->_connectionInfo['hosts'];
  65. }
  66. /**
  67. * Parse the connection string
  68. *
  69. * @param $connectionString
  70. * @return array
  71. */
  72. public static function parseConnectionString($connectionString)
  73. {
  74. $connectionInfo = array(
  75. 'connectionString' => $connectionString
  76. );
  77. // Remove mongodb protocol string
  78. if (substr($connectionString, 0, 10) == 'mongodb://') {
  79. $connectionString = substr($connectionString, 10);
  80. }
  81. // Is there a database name
  82. if ($pos = strrpos($connectionString, '/')) {
  83. $connectionInfo['database'] = substr($connectionString, $pos+1);
  84. $connectionString = substr($connectionString, 0, $pos);
  85. }
  86. // Fetch Hosts
  87. if (!empty($connectionString)) {
  88. $hostStrings = explode(',', $connectionString);
  89. $connectionInfo['hosts'] = array();
  90. foreach ($hostStrings as $hostString) {
  91. $connectionInfo['hosts'][] = self::parseHostString($hostString);
  92. }
  93. }
  94. return $connectionInfo;
  95. }
  96. /**
  97. * Parse a host string
  98. *
  99. * @param $hostString
  100. * @return array
  101. */
  102. public static function parseHostString($hostString)
  103. {
  104. $hostInfo = array();
  105. // Are we authenticating with a username and password?
  106. if ($pos = strpos($hostString, '@')) {
  107. $authString = substr($hostString, 0, $pos);
  108. $data = explode(':', $authString);
  109. $hostInfo['username'] = $data[0];
  110. if (count($data) > 1) {
  111. $hostInfo['password'] = $data[1];
  112. }
  113. $hostString = substr($hostString, $pos+1);
  114. }
  115. // Grab host and port
  116. $data = explode(':', $hostString);
  117. $hostInfo['host'] = $data[0];
  118. if (count($data) > 1) {
  119. $hostInfo['port'] = $data[1];
  120. }
  121. return $hostInfo;
  122. }
  123. /**
  124. * Get available options
  125. *
  126. * @return array
  127. */
  128. static public function getAvailableOptions()
  129. {
  130. return static::$_availableOptions;
  131. }
  132. }