MongoClient.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. */
  15. use MongoDB\Client;
  16. /**
  17. * A connection between PHP and MongoDB. This class is used to create and manage connections
  18. * See MongoClient::__construct() and the section on connecting for more information about creating connections.
  19. * @link http://www.php.net/manual/en/class.mongoclient.php
  20. */
  21. class MongoClient
  22. {
  23. const VERSION = '1.6.12';
  24. const DEFAULT_HOST = "localhost" ;
  25. const DEFAULT_PORT = 27017 ;
  26. const RP_PRIMARY = "primary" ;
  27. const RP_PRIMARY_PREFERRED = "primaryPreferred" ;
  28. const RP_SECONDARY = "secondary" ;
  29. const RP_SECONDARY_PREFERRED = "secondaryPreferred" ;
  30. const RP_NEAREST = "nearest" ;
  31. /**
  32. * @var bool
  33. * @deprecated This will not properly work as the underlying driver connects lazily
  34. */
  35. public $connected = false;
  36. /**
  37. * @var
  38. */
  39. public $status;
  40. /**
  41. * @var string
  42. */
  43. protected $server;
  44. /**
  45. * @var
  46. */
  47. protected $persistent;
  48. /**
  49. * @var Client
  50. */
  51. private $client;
  52. /**
  53. * Creates a new database connection object
  54. *
  55. * @link http://php.net/manual/en/mongo.construct.php
  56. * @param string $server The server name.
  57. * @param array $options An array of options for the connection.
  58. * @param array $driverOptions An array of options for the MongoDB driver.
  59. * @throws MongoConnectionException
  60. */
  61. public function __construct($server = 'mongodb://' . self::DEFAULT_HOST . ':' . self::DEFAULT_PORT, array $options = ["connect" => true], array $driverOptions = [])
  62. {
  63. $this->server = $server;
  64. $this->client = new Client($server, $options, $driverOptions);
  65. if (isset($options['connect']) && $options['connect']) {
  66. $this->connect();
  67. }
  68. }
  69. /**
  70. * Closes this database connection
  71. *
  72. * @link http://www.php.net/manual/en/mongoclient.close.php
  73. * @param boolean|string $connection
  74. * @return boolean If the connection was successfully closed.
  75. */
  76. public function close($connection = null)
  77. {
  78. $this->connected = false;
  79. return false;
  80. }
  81. /**
  82. * Connects to a database server
  83. *
  84. * @link http://www.php.net/manual/en/mongoclient.connect.php
  85. *
  86. * @throws MongoConnectionException
  87. * @return boolean If the connection was successful.
  88. */
  89. public function connect()
  90. {
  91. $this->connected = true;
  92. return true;
  93. }
  94. /**
  95. * Drops a database
  96. *
  97. * @link http://www.php.net/manual/en/mongoclient.dropdb.php
  98. * @param mixed $db The database to drop. Can be a MongoDB object or the name of the database.
  99. * @return array The database response.
  100. * @deprecated Use MongoDB::drop() instead.
  101. */
  102. public function dropDB($db)
  103. {
  104. return $this->selectDB($db)->drop();
  105. }
  106. /**
  107. * Gets a database
  108. *
  109. * @link http://php.net/manual/en/mongoclient.get.php
  110. * @param string $dbname The database name.
  111. * @return MongoDB The database name.
  112. */
  113. public function __get($dbname)
  114. {
  115. return $this->selectDB($dbname);
  116. }
  117. /**
  118. * Gets the client for this object
  119. *
  120. * @internal This part is not of the ext-mongo API and should not be used
  121. * @return Client
  122. */
  123. public function getClient()
  124. {
  125. return $this->client;
  126. }
  127. /**
  128. * Get connections
  129. *
  130. * Returns an array of all open connections, and information about each of the servers
  131. *
  132. * @return array
  133. */
  134. static public function getConnections()
  135. {
  136. return [];
  137. }
  138. /**
  139. * Get hosts
  140. *
  141. * This method is only useful with a connection to a replica set. It returns the status of all of the hosts in the
  142. * set. Without a replica set, it will just return an array with one element containing the host that you are
  143. * connected to.
  144. *
  145. * @return array
  146. */
  147. public function getHosts()
  148. {
  149. return [];
  150. }
  151. /**
  152. * Get the read preference for this connection
  153. *
  154. * @return array
  155. */
  156. public function getReadPreference()
  157. {
  158. return [];
  159. }
  160. /**
  161. * Get the write concern for this connection
  162. *
  163. * @return array Returns an array describing the write concern.
  164. */
  165. public function getWriteConcern()
  166. {
  167. return [];
  168. }
  169. /**
  170. * Kills a specific cursor on the server
  171. *
  172. * @link http://www.php.net/manual/en/mongoclient.killcursor.php
  173. * @param string $server_hash The server hash that has the cursor. This can be obtained through
  174. * {@link http://www.php.net/manual/en/mongocursor.info.php MongoCursor::info()}.
  175. * @param int|MongoInt64 $id The ID of the cursor to kill. You can either supply an {@link http://www.php.net/manual/en/language.types.integer.php int}
  176. * containing the 64 bit cursor ID, or an object of the
  177. * {@link http://www.php.net/manual/en/class.mongoint64.php MongoInt64} class. The latter is necessary on 32
  178. * bit platforms (and Windows).
  179. */
  180. public function killCursor($server_hash , $id)
  181. {
  182. }
  183. /**
  184. * Lists all of the databases available
  185. *
  186. * @link http://php.net/manual/en/mongoclient.listdbs.php
  187. * @return array Returns an associative array containing three fields. The first field is databases, which in turn contains an array. Each element of the array is an associative array corresponding to a database, giving the database's name, size, and if it's empty. The other two fields are totalSize (in bytes) and ok, which is 1 if this method ran successfully.
  188. */
  189. public function listDBs()
  190. {
  191. return $this->client->listDatabases();
  192. }
  193. /**
  194. * Gets a database collection
  195. *
  196. * @link http://www.php.net/manual/en/mongoclient.selectcollection.php
  197. * @param string $db The database name.
  198. * @param string $collection The collection name.
  199. * @return MongoCollection Returns a new collection object.
  200. * @throws Exception Throws Exception if the database or collection name is invalid.
  201. */
  202. public function selectCollection($db, $collection)
  203. {
  204. return new MongoCollection($this->selectDB($db), $collection);
  205. }
  206. /**
  207. * Gets a database
  208. *
  209. * @link http://www.php.net/manual/en/mongo.selectdb.php
  210. * @param string $name The database name.
  211. * @return MongoDB Returns a new db object.
  212. * @throws InvalidArgumentException
  213. */
  214. public function selectDB($name)
  215. {
  216. return new MongoDB($this, $name);
  217. }
  218. /**
  219. * Set read preference
  220. *
  221. * @param string $readPreference
  222. * @param array $tags
  223. * @return bool
  224. */
  225. public function setReadPreference($readPreference, $tags = null)
  226. {
  227. return false;
  228. }
  229. /**
  230. * Choose a new secondary for slaveOkay reads
  231. *
  232. * @link www.php.net/manual/en/mongo.switchslave.php
  233. * @return string The address of the secondary this connection is using for reads. This may be the same as the previous address as addresses are randomly chosen. It may return only one address if only one secondary (or only the primary) is available.
  234. * @throws MongoException (error code 15) if it is called on a non-replica-set connection. It will also throw MongoExceptions if it cannot find anyone (primary or secondary) to read from (error code 16).
  235. */
  236. public function switchSlave()
  237. {
  238. return $this->server;
  239. }
  240. /**
  241. * String representation of this connection
  242. *
  243. * @link http://www.php.net/manual/en/mongoclient.tostring.php
  244. * @return string Returns hostname and port for this connection.
  245. */
  246. public function __toString()
  247. {
  248. return $this->server;
  249. }
  250. }