Skip to content

Commit b85b91f

Browse files
committed
PHPLIB-73: Inherit default write concern and read preferences
Inheriting from the Client's URI options is not implemented, as it depends on PHPC-196. This commit also revises the documentation for selectDatabase(), selectCollection(), and the core class constructors.
1 parent 24d34dc commit b85b91f

File tree

3 files changed

+82
-66
lines changed

3 files changed

+82
-66
lines changed

src/Client.php

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@
55
use MongoDB\Collection;
66
use MongoDB\Database;
77
use MongoDB\Driver\Manager;
8+
use MongoDB\Driver\ReadPreference;
89
use MongoDB\Driver\Result;
10+
use MongoDB\Driver\WriteConcern;
911

1012
class Client
1113
{
1214
private $manager;
13-
private $wc;
14-
private $rp;
15-
15+
private $readPreference;
16+
private $writeConcern;
1617

1718
/**
18-
* Constructs new Client instance
19+
* Constructs a new Client instance.
1920
*
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.
2324
*
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
2729
*/
28-
public function __construct($uri, $options, $driverOptions)
30+
public function __construct($uri, array $options = array(), array $driverOptions = array())
2931
{
3032
$this->manager = new Manager($uri, $options, $driverOptions);
3133
}
@@ -42,33 +44,44 @@ public function dropDatabase($databaseName)
4244
}
4345

4446
/**
45-
* Select a database
47+
* Select a database.
4648
*
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.
4851
*
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
5256
*/
53-
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreferences = null)
57+
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
5458
{
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);
5664
}
5765

5866
/**
59-
* Select a specific collection in a database
67+
* Select a collection.
6068
*
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.
6271
*
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
6777
*/
68-
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreferences = null)
78+
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
6979
{
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;
7284

85+
return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
86+
}
7387
}
74-

src/Collection.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,24 @@ class Collection
4343

4444

4545
/**
46-
* Constructs new Collection instance
46+
* Constructs new Collection instance.
4747
*
48-
* This is the suggested CRUD interface when using phongo.
49-
* It implements the MongoDB CRUD specification which is an interface all MongoDB
50-
* supported drivers follow.
48+
* This class provides methods for collection-specific operations, such as
49+
* CRUD (i.e. create, read, update, and delete) and index management.
5150
*
52-
* @param Manager $manager The phongo Manager instance
53-
* @param string $ns Fully Qualified Namespace (dbname.collname)
54-
* @param WriteConcern $wc The WriteConcern to apply to writes
55-
* @param ReadPreference $rp The ReadPreferences to apply to reads
51+
* @param Manager $manager Manager instance from the driver
52+
* @param string $namespace Collection namespace (e.g. "db.collection")
53+
* @param WriteConcern $writeConcern Default write concern to apply
54+
* @param ReadPreference $readPreference Default read preference to apply
5655
*/
57-
public function __construct(Manager $manager, $ns, WriteConcern $wc = null, ReadPreference $rp = null)
56+
public function __construct(Manager $manager, $namespace, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
5857
{
5958
$this->manager = $manager;
60-
$this->ns = $ns;
61-
$this->wc = $wc;
62-
$this->rp = $rp;
59+
$this->ns = $namespace;
60+
$this->wc = $writeConcern;
61+
$this->rp = $readPreference;
6362

64-
list($this->dbname, $this->collname) = explode(".", $ns, 2);
63+
list($this->dbname, $this->collname) = explode(".", $namespace, 2);
6564
}
6665

6766
/**

src/Database.php

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,34 @@
44

55
use MongoDB\Collection;
66
use MongoDB\Driver\Manager;
7+
use MongoDB\Driver\ReadPreference;
78
use MongoDB\Driver\Result;
9+
use MongoDB\Driver\WriteConcern;
810

911
class Database
1012
{
13+
private $databaseName;
1114
private $manager;
12-
private $ns;
13-
private $wc;
14-
private $rp;
15-
16-
private $dbname;
15+
private $readPreference;
16+
private $writeConcern;
1717

1818
/**
19-
* Constructs new Database instance
19+
* Constructs new Database instance.
2020
*
21-
* It acts as a bridge for database specific operations.
21+
* This class provides methods for database-specific operations and serves
22+
* as a gateway for accessing collections.
2223
*
23-
* @param Manager $manager The phongo Manager instance
24-
* @param string $dbname Fully Qualified database name
25-
* @param WriteConcern $wc The WriteConcern to apply to writes
26-
* @param ReadPreference $rp The ReadPreferences to apply to reads
24+
* @param Manager $manager Manager instance from the driver
25+
* @param string $databaseName Database name
26+
* @param WriteConcern $writeConcern Default write concern to apply
27+
* @param ReadPreference $readPreference Default read preference to apply
2728
*/
28-
public function __construct(Manager $manager, $databaseName, WriteConcern $wc = null, ReadPreference $rp = null)
29+
public function __construct(Manager $manager, $databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
2930
{
3031
$this->manager = $manager;
31-
$this->dbname = $dbname;
32-
$this->wc = $wc;
33-
$this->rp = $rp;
32+
$this->databaseName = $databaseName;
33+
$this->writeConcern = $writeConcern;
34+
$this->readPreference = $readPreference;
3435
}
3536

3637
/**
@@ -81,19 +82,22 @@ public function listCollections(array $options = array())
8182
}
8283

8384
/**
84-
* Select a specific collection in this database
85+
* Select a collection within this database.
8586
*
86-
* It acts as a bridge to access specific collection commands
87+
* If a write concern or read preference is not specified, the write concern
88+
* or read preference of the Database will be applied, respectively.
8789
*
88-
* @param string $collectionName The collection to select
89-
* @param WriteConcern $writeConcern Default Write Concern to apply
90-
* @param ReadPreference $readPreferences Default Read Preferences to apply
90+
* @param string $collectionName Name of the collection to select
91+
* @param WriteConcern $writeConcern Default write concern to apply
92+
* @param ReadPreference $readPreference Default read preference to apply
93+
* @return Collection
9194
*/
92-
public function selectCollection($collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreferences = null)
95+
public function selectCollection($collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
9396
{
94-
return new Collection($this->manager, "{$this->dbname}.{$collectionName}", $writeConcern, $readPreferences);
95-
}
97+
$namespace = $this->databaseName . '.' . $collectionName;
98+
$writeConcern = $writeConcern ?: $this->writeConcern;
99+
$readPreference = $readPreference ?: $this->readPreference;
96100

101+
return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
102+
}
97103
}
98-
99-

0 commit comments

Comments
 (0)