5
5
use MongoDB \Collection ;
6
6
use MongoDB \Database ;
7
7
use MongoDB \Driver \Manager ;
8
+ use MongoDB \Driver \ReadPreference ;
8
9
use MongoDB \Driver \Result ;
10
+ use MongoDB \Driver \WriteConcern ;
9
11
10
12
class Client
11
13
{
12
14
private $ manager ;
13
- private $ wc ;
14
- private $ rp ;
15
-
15
+ private $ readPreference ;
16
+ private $ writeConcern ;
16
17
17
18
/**
18
- * Constructs new Client instance
19
+ * Constructs a new Client instance.
19
20
*
20
- * This is the suggested main entry point using phongo.
21
- * It acts as a bridge to access individual databases and collection tools
22
- * which are provided in this namespace .
21
+ * This is the preferred class for connecting to a MongoDB server or
22
+ * cluster of servers. It serves as a gateway for accessing individual
23
+ * databases and collections .
23
24
*
24
- * @param Manager $uri The MongoDB URI to connect to
25
- * @param WriteConcern $options URI Options
26
- * @param ReadPreference $driverOptions Driver specific options
25
+ * @see http://docs.mongodb.org/manual/reference/connection-string/
26
+ * @param string $uri MongoDB connection string
27
+ * @param array $options Additional connection string options
28
+ * @param array $driverOptions Driver-specific options
27
29
*/
28
- public function __construct ($ uri , $ options, $ driverOptions )
30
+ public function __construct ($ uri , array $ options = array (), array $ driverOptions = array () )
29
31
{
30
32
$ this ->manager = new Manager ($ uri , $ options , $ driverOptions );
31
33
}
@@ -42,33 +44,44 @@ public function dropDatabase($databaseName)
42
44
}
43
45
44
46
/**
45
- * Select a database
47
+ * Select a database.
46
48
*
47
- * It acts as a bridge to access specific database commands
49
+ * If a write concern or read preference is not specified, the write concern
50
+ * or read preference of the Client will be applied, respectively.
48
51
*
49
- * @param string $databaseName The database to select
50
- * @param WriteConcern $writeConcern Default Write Concern to apply
51
- * @param ReadPreference $readPreferences Default Read Preferences to apply
52
+ * @param string $databaseName Name of the database to select
53
+ * @param WriteConcern $writeConcern Default write concern to apply
54
+ * @param ReadPreference $readPreference Default read preference to apply
55
+ * @return Database
52
56
*/
53
- public function selectDatabase ($ databaseName , WriteConcern $ writeConcern = null , ReadPreference $ readPreferences = null )
57
+ public function selectDatabase ($ databaseName , WriteConcern $ writeConcern = null , ReadPreference $ readPreference = null )
54
58
{
55
- return new Database ($ this ->manager , $ databaseName , $ writeConcern , $ readPreferences );
59
+ // TODO: inherit from Manager options once PHPC-196 is implemented
60
+ $ writeConcern = $ writeConcern ?: $ this ->writeConcern ;
61
+ $ readPreference = $ readPreference ?: $ this ->readPreference ;
62
+
63
+ return new Database ($ this ->manager , $ databaseName , $ writeConcern , $ readPreference );
56
64
}
57
65
58
66
/**
59
- * Select a specific collection in a database
67
+ * Select a collection.
60
68
*
61
- * It acts as a bridge to access specific collection commands
69
+ * If a write concern or read preference is not specified, the write concern
70
+ * or read preference of the Client will be applied, respectively.
62
71
*
63
- * @param string $databaseName The database where the $collectionName exists
64
- * @param string $collectionName The collection to select
65
- * @param WriteConcern $writeConcern Default Write Concern to apply
66
- * @param ReadPreference $readPreferences Default Read Preferences to apply
72
+ * @param string $databaseName Name of the database containing the collection
73
+ * @param string $collectionName Name of the collection to select
74
+ * @param WriteConcern $writeConcern Default write concern to apply
75
+ * @param ReadPreference $readPreference Default read preference to apply
76
+ * @return Collection
67
77
*/
68
- public function selectCollection ($ databaseName , $ collectionName , WriteConcern $ writeConcern = null , ReadPreference $ readPreferences = null )
78
+ public function selectCollection ($ databaseName , $ collectionName , WriteConcern $ writeConcern = null , ReadPreference $ readPreference = null )
69
79
{
70
- return new Collection ($ this ->manager , "{$ databaseName }. {$ collectionName }" , $ writeConcern , $ readPreferences );
71
- }
80
+ $ namespace = $ databaseName . '. ' . $ collectionName ;
81
+ // TODO: inherit from Manager options once PHPC-196 is implemented
82
+ $ writeConcern = $ writeConcern ?: $ this ->writeConcern ;
83
+ $ readPreference = $ readPreference ?: $ this ->readPreference ;
72
84
85
+ return new Collection ($ this ->manager , $ namespace , $ writeConcern , $ readPreference );
86
+ }
73
87
}
74
-
0 commit comments