Skip to content

Commit 2a39fb4

Browse files
committed
Extract Database::listCollections() to an operation class
1 parent b90062c commit 2a39fb4

File tree

2 files changed

+127
-62
lines changed

2 files changed

+127
-62
lines changed

src/Database.php

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
use MongoDB\Driver\WriteConcern;
1313
use MongoDB\Exception\InvalidArgumentException;
1414
use MongoDB\Model\CollectionInfoIterator;
15-
use MongoDB\Model\CollectionInfoCommandIterator;
16-
use MongoDB\Model\CollectionInfoLegacyIterator;
15+
use MongoDB\Operation\ListCollections;
1716

1817
class Database
1918
{
@@ -112,18 +111,16 @@ public function getDatabaseName()
112111
/**
113112
* Returns information for all collections in this database.
114113
*
115-
* @see http://docs.mongodb.org/manual/reference/command/listCollections/
114+
* @see ListCollections::__construct() for supported options
116115
* @param array $options
117116
* @return CollectionInfoIterator
118117
*/
119118
public function listCollections(array $options = array())
120119
{
121-
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
122-
$server = $this->manager->selectServer($readPreference);
120+
$operation = new ListCollections($this->databaseName, $options);
121+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
123122

124-
return (FeatureDetection::isSupported($server, FeatureDetection::API_LISTCOLLECTIONS_CMD))
125-
? $this->listCollectionsCommand($server, $options)
126-
: $this->listCollectionsLegacy($server, $options);
123+
return $operation->execute($server);
127124
}
128125

129126
/**
@@ -145,58 +142,4 @@ public function selectCollection($collectionName, WriteConcern $writeConcern = n
145142

146143
return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
147144
}
148-
149-
/**
150-
* Returns information for all collections in this database using the
151-
* listCollections command.
152-
*
153-
* @param Server $server
154-
* @param array $options
155-
* @return CollectionInfoCommandIterator
156-
*/
157-
private function listCollectionsCommand(Server $server, array $options = array())
158-
{
159-
$command = new Command(array('listCollections' => 1) + $options);
160-
$cursor = $server->executeCommand($this->databaseName, $command);
161-
$cursor->setTypeMap(array('document' => 'array'));
162-
163-
return new CollectionInfoCommandIterator($cursor);
164-
}
165-
166-
/**
167-
* Returns information for all collections in this database by querying the
168-
* "system.namespaces" collection (MongoDB <2.8).
169-
*
170-
* @param Server $server
171-
* @param array $options
172-
* @return CollectionInfoLegacyIterator
173-
* @throws InvalidArgumentException if the filter option is neither an array
174-
* nor object, or if filter.name is not a
175-
* string.
176-
*/
177-
private function listCollectionsLegacy(Server $server, array $options = array())
178-
{
179-
$filter = array_key_exists('filter', $options) ? $options['filter'] : array();
180-
181-
if ( ! is_array($filter) && ! is_object($filter)) {
182-
throw new InvalidArgumentException(sprintf('Expected filter to be array or object, %s given', gettype($filter)));
183-
}
184-
185-
if (array_key_exists('name', (array) $filter)) {
186-
$filter = (array) $filter;
187-
188-
if ( ! is_string($filter['name'])) {
189-
throw new InvalidArgumentException(sprintf('Filter "name" must be a string for MongoDB <2.8, %s given', gettype($filter['name'])));
190-
}
191-
192-
$filter['name'] = $this->databaseName . '.' . $filter['name'];
193-
}
194-
195-
$namespace = $this->databaseName . '.system.namespaces';
196-
$query = new Query($filter);
197-
$cursor = $server->executeQuery($namespace, $query);
198-
$cursor->setTypeMap(array('document' => 'array'));
199-
200-
return new CollectionInfoLegacyIterator($cursor);
201-
}
202145
}

src/Operation/ListCollections.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace MongoDB\Operation;
4+
5+
use MongoDB\Driver\Command;
6+
use MongoDB\Driver\Query;
7+
use MongoDB\Driver\Server;
8+
use MongoDB\Exception\RuntimeException;
9+
use MongoDB\Model\CollectionInfoCommandIterator;
10+
use MongoDB\Model\CollectionInfoIterator;
11+
use MongoDB\Model\CollectionInfoLegacyIterator;
12+
13+
/**
14+
* Operation for the listCollections command.
15+
*
16+
* @api
17+
* @see MongoDB\Database::listCollections()
18+
* @see http://docs.mongodb.org/manual/reference/command/listCollections/
19+
*/
20+
class ListCollections implements Executable
21+
{
22+
private static $wireVersionForCommand = 3;
23+
24+
private $databaseName;
25+
private $options;
26+
27+
/**
28+
* Constructs a listCollections command.
29+
*
30+
* Supported options:
31+
*
32+
* * filter (document): Query by which to filter collections.
33+
*
34+
* * maxTimeMS (integer): The maximum amount of time to allow the query to
35+
* run.
36+
*
37+
* @param string $databaseName Database name
38+
* @param array $options Command options
39+
*/
40+
public function __construct($databaseName, array $options = array())
41+
{
42+
if (isset($options['filter']) && ! is_array($options['filter']) && ! is_object($options['filter'])) {
43+
throw new InvalidArgumentTypeException('"filter" option', $options['filter'], 'array or object');
44+
}
45+
46+
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
47+
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
48+
}
49+
50+
$this->databaseName = (string) $databaseName;
51+
$this->options = $options;
52+
}
53+
54+
/**
55+
* Execute the operation.
56+
*
57+
* @see Executable::execute()
58+
* @param Server $server
59+
* @return CollectionInfoIterator
60+
*/
61+
public function execute(Server $server)
62+
{
63+
return \MongoDB\server_supports_feature($server, self::$wireVersionForCommand)
64+
? $this->executeCommand($server)
65+
: $this->executeLegacy($server);
66+
}
67+
68+
/**
69+
* Returns information for all collections in this database using the
70+
* listCollections command.
71+
*
72+
* @param Server $server
73+
* @return CollectionInfoCommandIterator
74+
*/
75+
private function executeCommand(Server $server)
76+
{
77+
$cmd = array('listCollections' => 1);
78+
79+
if ( ! empty($this->options['filter'])) {
80+
$cmd['filter'] = (object) $this->options['filter'];
81+
}
82+
83+
if (isset($this->options['maxTimeMS'])) {
84+
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
85+
}
86+
87+
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
88+
$cursor->setTypeMap(array('document' => 'array'));
89+
90+
return new CollectionInfoCommandIterator($cursor);
91+
}
92+
93+
/**
94+
* Returns information for all collections in this database by querying the
95+
* "system.namespaces" collection (MongoDB <3.0).
96+
*
97+
* @param Server $server
98+
* @return CollectionInfoLegacyIterator
99+
* @throws InvalidArgumentException if filter.name is not a string.
100+
*/
101+
private function executeLegacy(Server $server)
102+
{
103+
$filter = empty($this->options['filter']) ? array() : (array) $this->options['filter'];
104+
105+
if (array_key_exists('name', $filter)) {
106+
if ( ! is_string($filter['name'])) {
107+
throw new InvalidArgumentTypeException('filter name for MongoDB <3.0', $filter['name'], 'string');
108+
}
109+
110+
$filter['name'] = $this->databaseName . '.' . $filter['name'];
111+
}
112+
113+
$options = isset($this->options['maxTimeMS'])
114+
? array('modifiers' => array('$maxTimeMS' => $this->options['maxTimeMS']))
115+
: array();
116+
117+
$cursor = $server->executeQuery($this->databaseName . '.system.namespaces', new Query($filter, $options));
118+
$cursor->setTypeMap(array('document' => 'array'));
119+
120+
return new CollectionInfoLegacyIterator($cursor);
121+
}
122+
}

0 commit comments

Comments
 (0)