MongoClient.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 = 'default', array $options = ["connect" => true], array $driverOptions = [])
  62. {
  63. if ($server === 'default') {
  64. $server = 'mongodb://' . self::DEFAULT_HOST . ':' . self::DEFAULT_PORT;
  65. }
  66. $this->server = $server;
  67. $this->client = new Client($server, $options, $driverOptions);
  68. if (isset($options['connect']) && $options['connect']) {
  69. $this->connect();
  70. }
  71. }
  72. /**
  73. * Closes this database connection
  74. *
  75. * @link http://www.php.net/manual/en/mongoclient.close.php
  76. * @param boolean|string $connection
  77. * @return boolean If the connection was successfully closed.
  78. */
  79. public function close($connection = null)
  80. {
  81. $this->connected = false;
  82. return false;
  83. }
  84. /**
  85. * Connects to a database server
  86. *
  87. * @link http://www.php.net/manual/en/mongoclient.connect.php
  88. *
  89. * @throws MongoConnectionException
  90. * @return boolean If the connection was successful.
  91. */
  92. public function connect()
  93. {
  94. $this->connected = true;
  95. return true;
  96. }
  97. /**
  98. * Drops a database
  99. *
  100. * @link http://www.php.net/manual/en/mongoclient.dropdb.php
  101. * @param mixed $db The database to drop. Can be a MongoDB object or the name of the database.
  102. * @return array The database response.
  103. * @deprecated Use MongoDB::drop() instead.
  104. */
  105. public function dropDB($db)
  106. {
  107. return $this->selectDB($db)->drop();
  108. }
  109. /**
  110. * Gets a database
  111. *
  112. * @link http://php.net/manual/en/mongoclient.get.php
  113. * @param string $dbname The database name.
  114. * @return MongoDB The database name.
  115. */
  116. public function __get($dbname)
  117. {
  118. return $this->selectDB($dbname);
  119. }
  120. /**
  121. * Gets the client for this object
  122. *
  123. * @internal This part is not of the ext-mongo API and should not be used
  124. * @return Client
  125. */
  126. public function getClient()
  127. {
  128. return $this->client;
  129. }
  130. /**
  131. * Get connections
  132. *
  133. * Returns an array of all open connections, and information about each of the servers
  134. *
  135. * @return array
  136. */
  137. static public function getConnections()
  138. {
  139. return [];
  140. }
  141. /**
  142. * Get hosts
  143. *
  144. * This method is only useful with a connection to a replica set. It returns the status of all of the hosts in the
  145. * set. Without a replica set, it will just return an array with one element containing the host that you are
  146. * connected to.
  147. *
  148. * @return array
  149. */
  150. public function getHosts()
  151. {
  152. return [];
  153. }
  154. /**
  155. * Get the read preference for this connection
  156. *
  157. * @return array
  158. */
  159. public function getReadPreference()
  160. {
  161. return [];
  162. }
  163. /**
  164. * Get the write concern for this connection
  165. *
  166. * @return array Returns an array describing the write concern.
  167. */
  168. public function getWriteConcern()
  169. {
  170. return [];
  171. }
  172. /**
  173. * Kills a specific cursor on the server
  174. *
  175. * @link http://www.php.net/manual/en/mongoclient.killcursor.php
  176. * @param string $server_hash The server hash that has the cursor. This can be obtained through
  177. * {@link http://www.php.net/manual/en/mongocursor.info.php MongoCursor::info()}.
  178. * @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}
  179. * containing the 64 bit cursor ID, or an object of the
  180. * {@link http://www.php.net/manual/en/class.mongoint64.php MongoInt64} class. The latter is necessary on 32
  181. * bit platforms (and Windows).
  182. */
  183. public function killCursor($server_hash , $id)
  184. {
  185. }
  186. /**
  187. * Lists all of the databases available
  188. *
  189. * @link http://php.net/manual/en/mongoclient.listdbs.php
  190. * @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.
  191. */
  192. public function listDBs()
  193. {
  194. return $this->client->listDatabases();
  195. }
  196. /**
  197. * Gets a database collection
  198. *
  199. * @link http://www.php.net/manual/en/mongoclient.selectcollection.php
  200. * @param string $db The database name.
  201. * @param string $collection The collection name.
  202. * @return MongoCollection Returns a new collection object.
  203. * @throws Exception Throws Exception if the database or collection name is invalid.
  204. */
  205. public function selectCollection($db, $collection)
  206. {
  207. return new MongoCollection($this->selectDB($db), $collection);
  208. }
  209. /**
  210. * Gets a database
  211. *
  212. * @link http://www.php.net/manual/en/mongo.selectdb.php
  213. * @param string $name The database name.
  214. * @return MongoDB Returns a new db object.
  215. * @throws InvalidArgumentException
  216. */
  217. public function selectDB($name)
  218. {
  219. return new MongoDB($this, $name);
  220. }
  221. /**
  222. * Set read preference
  223. *
  224. * @param string $readPreference
  225. * @param array $tags
  226. * @return bool
  227. */
  228. public function setReadPreference($readPreference, $tags = null)
  229. {
  230. return false;
  231. }
  232. /**
  233. * Choose a new secondary for slaveOkay reads
  234. *
  235. * @link www.php.net/manual/en/mongo.switchslave.php
  236. * @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.
  237. * @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).
  238. */
  239. public function switchSlave()
  240. {
  241. return $this->server;
  242. }
  243. /**
  244. * String representation of this connection
  245. *
  246. * @link http://www.php.net/manual/en/mongoclient.tostring.php
  247. * @return string Returns hostname and port for this connection.
  248. */
  249. public function __toString()
  250. {
  251. return $this->server;
  252. }
  253. }