Skip to content

Commit 7191886

Browse files
committed
Use type map for database and collection enumeration
This obviates the need to handle stdClass instances within the results.
1 parent 982889c commit 7191886

File tree

3 files changed

+5
-7
lines changed

3 files changed

+5
-7
lines changed

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": "[email protected]" }
1111
],
1212
"require": {
13-
"ext-mongodb": "*"
13+
"ext-mongodb": ">=0.5.0"
1414
},
1515
"require-dev": {
1616
"fzaninotto/faker": "~1.0"

src/Client.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,20 @@ public function listDatabases()
6363
$command = new Command(array('listDatabases' => 1));
6464

6565
$cursor = $this->manager->executeCommand('admin', $command);
66+
$cursor->setTypeMap(array('document' => 'array'));
6667
$result = current($cursor->toArray());
6768

6869
if ( ! isset($result['databases']) || ! is_array($result['databases'])) {
6970
throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
7071
}
7172

72-
$databases = array_map(
73-
function(stdClass $database) { return (array) $database; },
74-
$result['databases']
75-
);
76-
7773
/* Return an Iterator instead of an array in case listDatabases is
7874
* eventually changed to return a command cursor, like the collection
7975
* and index enumeration commands. This makes the "totalSize" command
8076
* field inaccessible, but users can manually invoke the command if they
8177
* need that value.
8278
*/
83-
return new DatabaseInfoLegacyIterator($databases);
79+
return new DatabaseInfoLegacyIterator($result['databases']);
8480
}
8581

8682
/**

src/Database.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ private function listCollectionsCommand(Server $server, array $options = array()
141141
{
142142
$command = new Command(array('listCollections' => 1) + $options);
143143
$cursor = $server->executeCommand($this->databaseName, $command);
144+
$cursor->setTypeMap(array('document' => 'array'));
144145

145146
return new CollectionInfoCommandIterator($cursor);
146147
}
@@ -177,6 +178,7 @@ private function listCollectionsLegacy(Server $server, array $options = array())
177178
$namespace = $this->databaseName . '.system.namespaces';
178179
$query = new Query($filter);
179180
$cursor = $server->executeQuery($namespace, $query);
181+
$cursor->setTypeMap(array('document' => 'array'));
180182

181183
return new CollectionInfoLegacyIterator($cursor);
182184
}

0 commit comments

Comments
 (0)