MongoClient.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 Alcaeus\MongoDbAdapter\Helper;
  16. use MongoDB\Client;
  17. /**
  18. * A connection between PHP and MongoDB. This class is used to create and manage connections
  19. * See MongoClient::__construct() and the section on connecting for more information about creating connections.
  20. * @link http://www.php.net/manual/en/class.mongoclient.php
  21. */
  22. class MongoClient
  23. {
  24. use Helper\ReadPreference;
  25. use Helper\WriteConcern;
  26. const VERSION = '1.6.12';
  27. const DEFAULT_HOST = "localhost" ;
  28. const DEFAULT_PORT = 27017 ;
  29. const RP_PRIMARY = "primary" ;
  30. const RP_PRIMARY_PREFERRED = "primaryPreferred" ;
  31. const RP_SECONDARY = "secondary" ;
  32. const RP_SECONDARY_PREFERRED = "secondaryPreferred" ;
  33. const RP_NEAREST = "nearest" ;
  34. /**
  35. * @var bool
  36. * @deprecated This will not properly work as the underlying driver connects lazily
  37. */
  38. public $connected = false;
  39. /**
  40. * @var
  41. */
  42. public $status;
  43. /**
  44. * @var string
  45. */
  46. protected $server;
  47. /**
  48. * @var
  49. */
  50. protected $persistent;
  51. /**
  52. * @var Client
  53. */
  54. private $client;
  55. /**
  56. * Creates a new database connection object
  57. *
  58. * @link http://php.net/manual/en/mongo.construct.php
  59. * @param string $server The server name.
  60. * @param array $options An array of options for the connection.
  61. * @param array $driverOptions An array of options for the MongoDB driver.
  62. * @throws MongoConnectionException
  63. */
  64. public function __construct($server = 'default', array $options = ["connect" => true], array $driverOptions = [])
  65. {
  66. if ($server === 'default') {
  67. $server = 'mongodb://' . self::DEFAULT_HOST . ':' . self::DEFAULT_PORT;
  68. }
  69. $this->server = $server;
  70. $this->client = new Client($server, $options, $driverOptions);
  71. if (isset($options['connect']) && $options['connect']) {
  72. $this->connect();
  73. }
  74. }
  75. /**
  76. * Closes this database connection
  77. *
  78. * @link http://www.php.net/manual/en/mongoclient.close.php
  79. * @param boolean|string $connection
  80. * @return boolean If the connection was successfully closed.
  81. */
  82. public function close($connection = null)
  83. {
  84. $this->connected = false;
  85. return false;
  86. }
  87. /**
  88. * Connects to a database server
  89. *
  90. * @link http://www.php.net/manual/en/mongoclient.connect.php
  91. *
  92. * @throws MongoConnectionException
  93. * @return boolean If the connection was successful.
  94. */
  95. public function connect()
  96. {
  97. $this->connected = true;
  98. return true;
  99. }
  100. /**
  101. * Drops a database
  102. *
  103. * @link http://www.php.net/manual/en/mongoclient.dropdb.php
  104. * @param mixed $db The database to drop. Can be a MongoDB object or the name of the database.
  105. * @return array The database response.
  106. * @deprecated Use MongoDB::drop() instead.
  107. */
  108. public function dropDB($db)
  109. {
  110. return $this->selectDB($db)->drop();
  111. }
  112. /**
  113. * Gets a database
  114. *
  115. * @link http://php.net/manual/en/mongoclient.get.php
  116. * @param string $dbname The database name.
  117. * @return MongoDB The database name.
  118. */
  119. public function __get($dbname)
  120. {
  121. return $this->selectDB($dbname);
  122. }
  123. /**
  124. * Gets the client for this object
  125. *
  126. * @internal This part is not of the ext-mongo API and should not be used
  127. * @return Client
  128. */
  129. public function getClient()
  130. {
  131. return $this->client;
  132. }
  133. /**
  134. * Get connections
  135. *
  136. * Returns an array of all open connections, and information about each of the servers
  137. *
  138. * @return array
  139. */
  140. static public function getConnections()
  141. {
  142. return [];
  143. }
  144. /**
  145. * Get hosts
  146. *
  147. * This method is only useful with a connection to a replica set. It returns the status of all of the hosts in the
  148. * set. Without a replica set, it will just return an array with one element containing the host that you are
  149. * connected to.
  150. *
  151. * @return array
  152. */
  153. public function getHosts()
  154. {
  155. return [];
  156. }
  157. /**
  158. * Kills a specific cursor on the server
  159. *
  160. * @link http://www.php.net/manual/en/mongoclient.killcursor.php
  161. * @param string $server_hash The server hash that has the cursor. This can be obtained through
  162. * {@link http://www.php.net/manual/en/mongocursor.info.php MongoCursor::info()}.
  163. * @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}
  164. * containing the 64 bit cursor ID, or an object of the
  165. * {@link http://www.php.net/manual/en/class.mongoint64.php MongoInt64} class. The latter is necessary on 32
  166. * bit platforms (and Windows).
  167. */
  168. public function killCursor($server_hash , $id)
  169. {
  170. }
  171. /**
  172. * Lists all of the databases available
  173. *
  174. * @link http://php.net/manual/en/mongoclient.listdbs.php
  175. * @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.
  176. */
  177. public function listDBs()
  178. {
  179. return $this->client->listDatabases();
  180. }
  181. /**
  182. * Gets a database collection
  183. *
  184. * @link http://www.php.net/manual/en/mongoclient.selectcollection.php
  185. * @param string $db The database name.
  186. * @param string $collection The collection name.
  187. * @return MongoCollection Returns a new collection object.
  188. * @throws Exception Throws Exception if the database or collection name is invalid.
  189. */
  190. public function selectCollection($db, $collection)
  191. {
  192. return new MongoCollection($this->selectDB($db), $collection);
  193. }
  194. /**
  195. * Gets a database
  196. *
  197. * @link http://www.php.net/manual/en/mongo.selectdb.php
  198. * @param string $name The database name.
  199. * @return MongoDB Returns a new db object.
  200. * @throws InvalidArgumentException
  201. */
  202. public function selectDB($name)
  203. {
  204. return new MongoDB($this, $name);
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function setReadPreference($readPreference, $tags = null)
  210. {
  211. return $this->setReadPreferenceFromParameters($readPreference, $tags);
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function setWriteConcern($wstring, $wtimeout = 0)
  217. {
  218. return $this->setWriteConcernFromParameters($wstring, $wtimeout);
  219. }
  220. /**
  221. * Choose a new secondary for slaveOkay reads
  222. *
  223. * @link www.php.net/manual/en/mongo.switchslave.php
  224. * @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.
  225. * @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).
  226. */
  227. public function switchSlave()
  228. {
  229. return $this->server;
  230. }
  231. /**
  232. * String representation of this connection
  233. *
  234. * @link http://www.php.net/manual/en/mongoclient.tostring.php
  235. * @return string Returns hostname and port for this connection.
  236. */
  237. public function __toString()
  238. {
  239. return $this->server;
  240. }
  241. }