|
| 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