Skip to content

Commit c2f4e5e

Browse files
committed
Merge pull request #29
2 parents 705698a + f47db78 commit c2f4e5e

20 files changed

+205
-34
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ env:
1212
- MONGO_REPO_TYPE="precise/mongodb-enterprise/"
1313
- SOURCES_LOC="/etc/apt/sources.list.d/mongodb.list"
1414
matrix:
15-
- DRIVER_VERSION=alpha SERVER_VERSION=2.4
16-
- DRIVER_VERSION=alpha SERVER_VERSION=2.6
17-
- DRIVER_VERSION=alpha SERVER_VERSION=3.0
15+
- DRIVER_VERSION=beta SERVER_VERSION=2.4
16+
- DRIVER_VERSION=beta SERVER_VERSION=2.6
17+
- DRIVER_VERSION=beta SERVER_VERSION=3.0
1818

1919
before_install:
2020
- sudo apt-key adv --keyserver ${KEY_SERVER} --recv 7F0CEB10

src/Client.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public function listDatabases(array $options = array())
7575
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
7676
{
7777
$namespace = $databaseName . '.' . $collectionName;
78-
$writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
79-
$readPreference = $readPreference ?: $this->manager->getReadPreference();
78+
$writeConcern = $writeConcern ?: \MongoDB\get_manager_write_concern($this->manager);
79+
$readPreference = $readPreference ?: \MongoDB\get_manager_read_preference($this->manager);
8080

8181
return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
8282
}
@@ -94,8 +94,8 @@ public function selectCollection($databaseName, $collectionName, WriteConcern $w
9494
*/
9595
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
9696
{
97-
$writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
98-
$readPreference = $readPreference ?: $this->manager->getReadPreference();
97+
$writeConcern = $writeConcern ?: \MongoDB\get_manager_write_concern($this->manager);
98+
$readPreference = $readPreference ?: \MongoDB\get_manager_read_preference($this->manager);
9999

100100
return new Database($this->manager, $databaseName, $writeConcern, $readPreference);
101101
}

src/Collection.php

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public function __construct(Manager $manager, $namespace, WriteConcern $writeCon
6666
$this->collectionName = $parts[1];
6767

6868
$this->manager = $manager;
69-
$this->writeConcern = $writeConcern;
70-
$this->readPreference = $readPreference;
69+
$this->writeConcern = $writeConcern ?: \MongoDB\get_manager_write_concern($this->manager);
70+
$this->readPreference = $readPreference ?: \MongoDB\get_manager_read_preference($this->manager);
7171
}
7272

7373
/**
@@ -95,9 +95,16 @@ public function __toString()
9595
*/
9696
public function aggregate(array $pipeline, array $options = array())
9797
{
98-
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
99-
$server = $this->manager->selectServer($readPreference);
98+
if ( ! isset($options['readPreference'])) {
99+
$options['readPreference'] = $this->readPreference;
100+
}
101+
102+
if (\MongoDB\is_last_pipeline_operator_out($pipeline)) {
103+
$options['readPreference'] = new ReadPreference(ReadPreference::RP_PRIMARY);
104+
}
105+
100106
$operation = new Aggregate($this->databaseName, $this->collectionName, $pipeline, $options);
107+
$server = $this->manager->selectServer($options['readPreference']);
101108

102109
return $operation->execute($server);
103110
}
@@ -112,7 +119,7 @@ public function aggregate(array $pipeline, array $options = array())
112119
*/
113120
public function bulkWrite(array $operations, array $options = array())
114121
{
115-
if ( ! isset($options['writeConcern']) && isset($this->writeConcern)) {
122+
if ( ! isset($options['writeConcern'])) {
116123
$options['writeConcern'] = $this->writeConcern;
117124
}
118125

@@ -132,8 +139,12 @@ public function bulkWrite(array $operations, array $options = array())
132139
*/
133140
public function count($filter = array(), array $options = array())
134141
{
142+
if ( ! isset($options['readPreference'])) {
143+
$options['readPreference'] = $this->readPreference;
144+
}
145+
135146
$operation = new Count($this->databaseName, $this->collectionName, $filter, $options);
136-
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
147+
$server = $this->manager->selectServer($options['readPreference']);
137148

138149
return $operation->execute($server);
139150
}
@@ -194,7 +205,7 @@ public function createIndexes(array $indexes)
194205
*/
195206
public function deleteMany($filter, array $options = array())
196207
{
197-
if ( ! isset($options['writeConcern']) && isset($this->writeConcern)) {
208+
if ( ! isset($options['writeConcern'])) {
198209
$options['writeConcern'] = $this->writeConcern;
199210
}
200211

@@ -215,7 +226,7 @@ public function deleteMany($filter, array $options = array())
215226
*/
216227
public function deleteOne($filter, array $options = array())
217228
{
218-
if ( ! isset($options['writeConcern']) && isset($this->writeConcern)) {
229+
if ( ! isset($options['writeConcern'])) {
219230
$options['writeConcern'] = $this->writeConcern;
220231
}
221232

@@ -236,8 +247,12 @@ public function deleteOne($filter, array $options = array())
236247
*/
237248
public function distinct($fieldName, $filter = array(), array $options = array())
238249
{
250+
if ( ! isset($options['readPreference'])) {
251+
$options['readPreference'] = $this->readPreference;
252+
}
253+
239254
$operation = new Distinct($this->databaseName, $this->collectionName, $fieldName, $filter, $options);
240-
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
255+
$server = $this->manager->selectServer($options['readPreference']);
241256

242257
return $operation->execute($server);
243258
}
@@ -300,8 +315,12 @@ public function dropIndexes()
300315
*/
301316
public function find($filter = array(), array $options = array())
302317
{
318+
if ( ! isset($options['readPreference'])) {
319+
$options['readPreference'] = $this->readPreference;
320+
}
321+
303322
$operation = new Find($this->databaseName, $this->collectionName, $filter, $options);
304-
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
323+
$server = $this->manager->selectServer($options['readPreference']);
305324

306325
return $operation->execute($server);
307326
}
@@ -317,8 +336,12 @@ public function find($filter = array(), array $options = array())
317336
*/
318337
public function findOne($filter = array(), array $options = array())
319338
{
339+
if ( ! isset($options['readPreference'])) {
340+
$options['readPreference'] = $this->readPreference;
341+
}
342+
320343
$operation = new FindOne($this->databaseName, $this->collectionName, $filter, $options);
321-
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
344+
$server = $this->manager->selectServer($options['readPreference']);
322345

323346
return $operation->execute($server);
324347
}
@@ -430,7 +453,7 @@ public function getNamespace()
430453
*/
431454
public function insertMany(array $documents, array $options = array())
432455
{
433-
if ( ! isset($options['writeConcern']) && isset($this->writeConcern)) {
456+
if ( ! isset($options['writeConcern'])) {
434457
$options['writeConcern'] = $this->writeConcern;
435458
}
436459

@@ -451,7 +474,7 @@ public function insertMany(array $documents, array $options = array())
451474
*/
452475
public function insertOne($document, array $options = array())
453476
{
454-
if ( ! isset($options['writeConcern']) && isset($this->writeConcern)) {
477+
if ( ! isset($options['writeConcern'])) {
455478
$options['writeConcern'] = $this->writeConcern;
456479
}
457480

@@ -487,7 +510,7 @@ public function listIndexes(array $options = array())
487510
*/
488511
public function replaceOne($filter, $replacement, array $options = array())
489512
{
490-
if ( ! isset($options['writeConcern']) && isset($this->writeConcern)) {
513+
if ( ! isset($options['writeConcern'])) {
491514
$options['writeConcern'] = $this->writeConcern;
492515
}
493516

@@ -509,7 +532,7 @@ public function replaceOne($filter, $replacement, array $options = array())
509532
*/
510533
public function updateMany($filter, $update, array $options = array())
511534
{
512-
if ( ! isset($options['writeConcern']) && isset($this->writeConcern)) {
535+
if ( ! isset($options['writeConcern'])) {
513536
$options['writeConcern'] = $this->writeConcern;
514537
}
515538

@@ -531,7 +554,7 @@ public function updateMany($filter, $update, array $options = array())
531554
*/
532555
public function updateOne($filter, $update, array $options = array())
533556
{
534-
if ( ! isset($options['writeConcern']) && isset($this->writeConcern)) {
557+
if ( ! isset($options['writeConcern'])) {
535558
$options['writeConcern'] = $this->writeConcern;
536559
}
537560

src/Database.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public function __construct(Manager $manager, $databaseName, WriteConcern $write
4444

4545
$this->manager = $manager;
4646
$this->databaseName = (string) $databaseName;
47-
$this->writeConcern = $writeConcern;
48-
$this->readPreference = $readPreference;
47+
$this->writeConcern = $writeConcern ?: \MongoDB\get_manager_write_concern($this->manager);
48+
$this->readPreference = $readPreference ?: \MongoDB\get_manager_read_preference($this->manager);
4949
}
5050

5151
/**

src/Operation/Aggregate.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MongoDB\Operation;
44

55
use MongoDB\Driver\Command;
6+
use MongoDB\Driver\ReadPreference;
67
use MongoDB\Driver\Server;
78
use MongoDB\Exception\InvalidArgumentException;
89
use MongoDB\Exception\InvalidArgumentTypeException;
@@ -42,6 +43,8 @@ class Aggregate implements Executable
4243
* * maxTimeMS (integer): The maximum amount of time to allow the query to
4344
* run.
4445
*
46+
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
47+
*
4548
* * useCursor (boolean): Indicates whether the command will request that
4649
* the server provide results using a cursor. The default is true.
4750
*
@@ -94,6 +97,10 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
9497
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
9598
}
9699

100+
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
101+
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
102+
}
103+
97104
if ( ! is_bool($options['useCursor'])) {
98105
throw new InvalidArgumentTypeException('"useCursor" option', $options['useCursor'], 'boolean');
99106
}
@@ -118,9 +125,10 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
118125
public function execute(Server $server)
119126
{
120127
$isCursorSupported = \MongoDB\server_supports_feature($server, self::$wireVersionForCursor);
128+
$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
121129

122130
$command = $this->createCommand($server, $isCursorSupported);
123-
$cursor = $server->executeCommand($this->databaseName, $command);
131+
$cursor = $server->executeCommand($this->databaseName, $command, $readPreference);
124132

125133
if ($isCursorSupported && $this->options['useCursor']) {
126134
return $cursor;

src/Operation/BulkWrite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function __construct($databaseName, $collectionName, array $operations, a
188188
throw new InvalidArgumentTypeException('"ordered" option', $options['ordered'], 'boolean');
189189
}
190190

191-
if (array_key_exists('writeConcern', $options) && ! $options['writeConcern'] instanceof WriteConcern) {
191+
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
192192
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
193193
}
194194

src/Operation/Count.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MongoDB\Operation;
44

55
use MongoDB\Driver\Command;
6+
use MongoDB\Driver\ReadPreference;
67
use MongoDB\Driver\Server;
78
use MongoDB\Exception\InvalidArgumentException;
89
use MongoDB\Exception\InvalidArgumentTypeException;
@@ -36,6 +37,8 @@ class Count implements Executable
3637
* * maxTimeMS (integer): The maximum amount of time to allow the query to
3738
* run.
3839
*
40+
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
41+
*
3942
* * skip (integer): The number of documents to skip before returning the
4043
* documents.
4144
*
@@ -69,6 +72,10 @@ public function __construct($databaseName, $collectionName, $filter = array(), a
6972
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
7073
}
7174

75+
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
76+
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
77+
}
78+
7279
if (isset($options['skip']) && ! is_integer($options['skip'])) {
7380
throw new InvalidArgumentTypeException('"skip" option', $options['skip'], 'integer');
7481
}
@@ -88,7 +95,9 @@ public function __construct($databaseName, $collectionName, $filter = array(), a
8895
*/
8996
public function execute(Server $server)
9097
{
91-
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
98+
$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
99+
100+
$cursor = $server->executeCommand($this->databaseName, $this->createCommand(), $readPreference);
92101
$result = current($cursor->toArray());
93102

94103
if (empty($result->ok)) {

src/Operation/Delete.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct($databaseName, $collectionName, $filter, $limit, arr
5252
throw new InvalidArgumentException('$limit must be 0 or 1');
5353
}
5454

55-
if (array_key_exists('writeConcern', $options) && ! $options['writeConcern'] instanceof WriteConcern) {
55+
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
5656
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
5757
}
5858

src/Operation/Distinct.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MongoDB\Operation;
44

55
use MongoDB\Driver\Command;
6+
use MongoDB\Driver\ReadPreference;
67
use MongoDB\Driver\Server;
78
use MongoDB\Exception\InvalidArgumentException;
89
use MongoDB\Exception\InvalidArgumentTypeException;
@@ -32,6 +33,8 @@ class Distinct implements Executable
3233
* * maxTimeMS (integer): The maximum amount of time to allow the query to
3334
* run.
3435
*
36+
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
37+
*
3538
* @param string $databaseName Database name
3639
* @param string $collectionName Collection name
3740
* @param string $fieldName Field for which to return distinct values
@@ -49,6 +52,10 @@ public function __construct($databaseName, $collectionName, $fieldName, $filter
4952
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
5053
}
5154

55+
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
56+
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
57+
}
58+
5259
$this->databaseName = (string) $databaseName;
5360
$this->collectionName = (string) $collectionName;
5461
$this->fieldName = (string) $fieldName;
@@ -65,7 +72,9 @@ public function __construct($databaseName, $collectionName, $fieldName, $filter
6572
*/
6673
public function execute(Server $server)
6774
{
68-
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
75+
$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
76+
77+
$cursor = $server->executeCommand($this->databaseName, $this->createCommand(), $readPreference);
6978
$result = current($cursor->toArray());
7079

7180
if (empty($result->ok)) {

src/Operation/Find.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MongoDB\Operation;
44

55
use MongoDB\Driver\Query;
6+
use MongoDB\Driver\ReadPreference;
67
use MongoDB\Driver\Server;
78
use MongoDB\Exception\InvalidArgumentException;
89
use MongoDB\Exception\InvalidArgumentTypeException;
@@ -64,6 +65,8 @@ class Find implements Executable
6465
* * projection (document): Limits the fields to return for the matching
6566
* document.
6667
*
68+
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
69+
*
6770
* * skip (integer): The number of documents to skip before returning.
6871
*
6972
* * sort (document): The order in which to return matching documents. If
@@ -130,6 +133,10 @@ public function __construct($databaseName, $collectionName, $filter, array $opti
130133
throw new InvalidArgumentTypeException('"projection" option', $options['projection'], 'array or object');
131134
}
132135

136+
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
137+
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
138+
}
139+
133140
if (isset($options['skip']) && ! is_integer($options['skip'])) {
134141
throw new InvalidArgumentTypeException('"skip" option', $options['skip'], 'integer');
135142
}
@@ -153,7 +160,9 @@ public function __construct($databaseName, $collectionName, $filter, array $opti
153160
*/
154161
public function execute(Server $server)
155162
{
156-
return $server->executeQuery($this->databaseName . '.' . $this->collectionName, $this->createQuery());
163+
$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
164+
165+
return $server->executeQuery($this->databaseName . '.' . $this->collectionName, $this->createQuery(), $readPreference);
157166
}
158167

159168
/**

0 commit comments

Comments
 (0)