Skip to content

Commit 7146455

Browse files
committed
Merge pull request #6
2 parents 86dab09 + 192d950 commit 7146455

19 files changed

+830
-189
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ php:
77
- 5.6
88

99
env:
10-
- MONGODB_VERSION=0.2.0
10+
- MONGODB_VERSION=alpha
1111

1212
services: mongodb
1313

1414
before_script:
15-
- pecl -q install -f mongodb-${MONGODB_VERSION}
15+
- mongo --eval 'tojson(db.runCommand({buildInfo:1}))'
16+
- pecl install -f mongodb-${MONGODB_VERSION}
1617
- php --ri mongodb
1718
- composer install --dev --no-interaction --prefer-source

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
{ "name": "Derick Rethans", "email": "github@derickrethans.nl" }
1111
],
1212
"require": {
13-
"ext-mongodb": "^0.2"
13+
"ext-mongodb": "*"
1414
},
1515
"require-dev": {
1616
"fzaninotto/faker": "~1.0"

phpunit.xml.dist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
convertWarningsToExceptions="true"
1212
stopOnFailure="false"
1313
syntaxCheck="false"
14-
bootstrap="vendor/autoload.php"
14+
bootstrap="tests/bootstrap.php"
1515
>
1616

1717
<php>
1818
<ini name="error_reporting" value="-1"/>
19+
<env name="MONGODB_URI" value="mongodb://127.0.0.1:27017"/>
20+
<env name="MONGODB_DATABASE" value="phplib_test"/>
1921
</php>
2022

2123
<testsuites>

src/Client.php

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,125 @@
22

33
namespace MongoDB;
44

5-
use MongoDB\Collection;
6-
use MongoDB\Database;
5+
use MongoDB\Driver\Command;
6+
use MongoDB\Driver\Cursor;
77
use MongoDB\Driver\Manager;
8-
use MongoDB\Driver\Result;
8+
use MongoDB\Driver\ReadPreference;
9+
use MongoDB\Driver\WriteConcern;
10+
use ArrayIterator;
11+
use stdClass;
12+
use UnexpectedValueException;
913

1014
class Client
1115
{
1216
private $manager;
13-
private $wc;
14-
private $rp;
15-
17+
private $readPreference;
18+
private $writeConcern;
1619

1720
/**
18-
* Constructs new Client instance
21+
* Constructs a new Client instance.
1922
*
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.
23+
* This is the preferred class for connecting to a MongoDB server or
24+
* cluster of servers. It serves as a gateway for accessing individual
25+
* databases and collections.
2326
*
24-
* @param Manager $uri The MongoDB URI to connect to
25-
* @param WriteConcern $options URI Options
26-
* @param ReadPreference $driverOptions Driver specific options
27+
* @see http://docs.mongodb.org/manual/reference/connection-string/
28+
* @param string $uri MongoDB connection string
29+
* @param array $options Additional connection string options
30+
* @param array $driverOptions Driver-specific options
2731
*/
28-
public function __construct($uri, $options, $driverOptions)
32+
public function __construct($uri, array $options = array(), array $driverOptions = array())
2933
{
3034
$this->manager = new Manager($uri, $options, $driverOptions);
3135
}
3236

3337
/**
3438
* Drop a database.
3539
*
40+
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
3641
* @param string $databaseName
37-
* @return Result
42+
* @return Cursor
3843
*/
3944
public function dropDatabase($databaseName)
4045
{
41-
// TODO
46+
$databaseName = (string) $databaseName;
47+
$command = new Command(array('dropDatabase' => 1));
48+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
49+
50+
return $this->manager->executeCommand($databaseName, $command, $readPreference);
4251
}
4352

4453
/**
45-
* Select a database
54+
* List databases.
4655
*
47-
* It acts as a bridge to access specific database commands
48-
*
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
56+
* @see http://docs.mongodb.org/manual/reference/command/listDatabases/
57+
* @return Traversable
58+
* @throws UnexpectedValueException if the command result is malformed
5259
*/
53-
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreferences = null)
60+
public function listDatabases()
5461
{
55-
return new Database($this->manager, $databaseName, $writeConcern, $readPreferences);
62+
$command = new Command(array('listDatabases' => 1));
63+
64+
$cursor = $this->manager->executeCommand('admin', $command);
65+
$result = current($cursor->toArray());
66+
67+
if ( ! isset($result['databases']) || ! is_array($result['databases'])) {
68+
throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
69+
}
70+
71+
$databases = array_map(
72+
function(stdClass $database) { return (array) $database; },
73+
$result['databases']
74+
);
75+
76+
/* Return a Traversable instead of an array in case listDatabases is
77+
* eventually changed to return a command cursor, like the collection
78+
* and index enumeration commands. This makes the "totalSize" command
79+
* field inaccessible, but users can manually invoke the command if they
80+
* need that value.
81+
*/
82+
return new ArrayIterator($databases);
5683
}
5784

5885
/**
59-
* Select a specific collection in a database
86+
* Select a database.
6087
*
61-
* It acts as a bridge to access specific collection commands
88+
* If a write concern or read preference is not specified, the write concern
89+
* or read preference of the Client will be applied, respectively.
6290
*
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
91+
* @param string $databaseName Name of the database to select
92+
* @param WriteConcern $writeConcern Default write concern to apply
93+
* @param ReadPreference $readPreference Default read preference to apply
94+
* @return Database
6795
*/
68-
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreferences = null)
96+
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
6997
{
70-
return new Collection($this->manager, "{$databaseName}.{$collectionName}", $writeConcern, $readPreferences);
98+
// TODO: inherit from Manager options once PHPC-196 is implemented
99+
$writeConcern = $writeConcern ?: $this->writeConcern;
100+
$readPreference = $readPreference ?: $this->readPreference;
101+
102+
return new Database($this->manager, $databaseName, $writeConcern, $readPreference);
71103
}
72104

73-
}
105+
/**
106+
* Select a collection.
107+
*
108+
* If a write concern or read preference is not specified, the write concern
109+
* or read preference of the Client will be applied, respectively.
110+
*
111+
* @param string $databaseName Name of the database containing the collection
112+
* @param string $collectionName Name of the collection to select
113+
* @param WriteConcern $writeConcern Default write concern to apply
114+
* @param ReadPreference $readPreference Default read preference to apply
115+
* @return Collection
116+
*/
117+
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
118+
{
119+
$namespace = $databaseName . '.' . $collectionName;
120+
// TODO: inherit from Manager options once PHPC-196 is implemented
121+
$writeConcern = $writeConcern ?: $this->writeConcern;
122+
$readPreference = $readPreference ?: $this->readPreference;
74123

124+
return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
125+
}
126+
}

src/Collection.php

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace MongoDB;
44

55
use MongoDB\Driver\Command;
6+
use MongoDB\Driver\Cursor;
67
use MongoDB\Driver\Manager;
78
use MongoDB\Driver\Query;
89
use MongoDB\Driver\ReadPreference;
9-
use MongoDB\Driver\Result;
1010
use MongoDB\Driver\BulkWrite;
1111
use MongoDB\Driver\WriteConcern;
1212

@@ -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 = (string) $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
/**
@@ -87,14 +86,16 @@ public function aggregate(array $pipeline, array $options = array())
8786
"pipeline" => $pipeline,
8887
) + $options;
8988

90-
$result = $this->_runCommand($this->dbname, $cmd);
91-
$doc = $result->toArray();
89+
$cursor = $this->_runCommand($this->dbname, $cmd);
90+
9291
if (isset($cmd["cursor"]) && $cmd["cursor"]) {
93-
return $result;
94-
} else {
95-
if ($doc["ok"]) {
96-
return new \ArrayIterator($doc["result"]);
97-
}
92+
return $cursor;
93+
}
94+
95+
$doc = current($cursor->toArray());
96+
97+
if ($doc["ok"]) {
98+
return new \ArrayIterator($doc["result"]);
9899
}
99100

100101
throw $this->_generateCommandException($doc);
@@ -235,7 +236,7 @@ public function count(array $filter = array(), array $options = array())
235236
"query" => $filter,
236237
) + $options;
237238

238-
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
239+
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
239240
if ($doc["ok"]) {
240241
return $doc["n"];
241242
}
@@ -325,7 +326,7 @@ public function distinct($fieldName, array $filter = array(), array $options = a
325326
"query" => $filter,
326327
) + $options;
327328

328-
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
329+
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
329330
if ($doc["ok"]) {
330331
return $doc["values"];
331332
}
@@ -335,11 +336,15 @@ public function distinct($fieldName, array $filter = array(), array $options = a
335336
/**
336337
* Drop this collection.
337338
*
338-
* @return Result
339+
* @see http://docs.mongodb.org/manual/reference/command/drop/
340+
* @return Cursor
339341
*/
340342
public function drop()
341343
{
342-
// TODO
344+
$command = new Command(array('drop' => $this->collname));
345+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
346+
347+
return $this->manager->executeCommand($this->dbname, $command, $readPreference);
343348
}
344349

345350
/**
@@ -348,7 +353,7 @@ public function drop()
348353
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
349354
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/
350355
* @param string $indexName
351-
* @return Result
356+
* @return Cursor
352357
* @throws InvalidArgumentException if "*" is specified
353358
*/
354359
public function dropIndex($indexName)
@@ -361,7 +366,7 @@ public function dropIndex($indexName)
361366
*
362367
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
363368
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndexes/
364-
* @return Result
369+
* @return Cursor
365370
*/
366371
public function dropIndexes()
367372
{
@@ -376,7 +381,7 @@ public function dropIndexes()
376381
*
377382
* @param array $filter The find query to execute
378383
* @param array $options Additional options
379-
* @return Result
384+
* @return Cursor
380385
*/
381386
public function find(array $filter = array(), array $options = array())
382387
{
@@ -434,7 +439,7 @@ public function findOneAndDelete(array $filter, array $options = array())
434439
"query" => $filter,
435440
) + $options;
436441

437-
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
442+
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
438443
if ($doc["ok"]) {
439444
return $doc["value"];
440445
}
@@ -471,7 +476,7 @@ public function findOneAndReplace(array $filter, array $replacement, array $opti
471476
"query" => $filter,
472477
) + $options;
473478

474-
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
479+
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
475480
if ($doc["ok"]) {
476481
return $doc["value"];
477482
}
@@ -509,7 +514,7 @@ public function findOneAndUpdate(array $filter, array $update, array $options =
509514
"query" => $filter,
510515
) + $options;
511516

512-
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
517+
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
513518
if ($doc["ok"]) {
514519
return $doc["value"];
515520
}
@@ -948,7 +953,7 @@ public function insertOne(array $document)
948953
*
949954
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
950955
* @see http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/
951-
* @return Result
956+
* @return Cursor
952957
*/
953958
public function listIndexes()
954959
{

0 commit comments

Comments
 (0)