Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401]
1 <?php 2 /* 3 * Copyright 2015-2017 MongoDB, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 namespace MongoDB\Operation; 19 20 use MongoDB\Driver\Command; 21 use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException; 22 use MongoDB\Driver\Server; 23 use MongoDB\Driver\Session; 24 use MongoDB\Exception\InvalidArgumentException; 25 use MongoDB\Exception\UnexpectedValueException; 26 use MongoDB\Model\DatabaseInfoIterator; 27 use MongoDB\Model\DatabaseInfoLegacyIterator; 28 use function current; 29 use function is_array; 30 use function is_integer; 31 use function is_object; 32 33 /** 34 * Operation for the ListDatabases command. 35 * 36 * @api 37 * @see \MongoDB\Client::listDatabases() 38 * @see http://docs.mongodb.org/manual/reference/command/ListDatabases/ 39 */ 40 class ListDatabases implements Executable 41 { 42 /** @var array */ 43 private $options; 44 45 /** 46 * Constructs a listDatabases command. 47 * 48 * Supported options: 49 * 50 * * filter (document): Query by which to filter databases. 51 * 52 * For servers < 3.6, this option is ignored. 53 * 54 * * maxTimeMS (integer): The maximum amount of time to allow the query to 55 * run. 56 * 57 * * session (MongoDB\Driver\Session): Client session. 58 * 59 * Sessions are not supported for server versions < 3.6. 60 * 61 * @param array $options Command options 62 * @throws InvalidArgumentException for parameter/option parsing errors 63 */ 64 public function __construct(array $options = []) 65 { 66 if (isset($options['filter']) && ! is_array($options['filter']) && ! is_object($options['filter'])) { 67 throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], 'array or object'); 68 } 69 70 if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) { 71 throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer'); 72 } 73 74 if (isset($options['session']) && ! $options['session'] instanceof Session) { 75 throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class); 76 } 77 78 $this->options = $options; 79 } 80 81 /** 82 * Execute the operation. 83 * 84 * @see Executable::execute() 85 * @param Server $server 86 * @return DatabaseInfoIterator 87 * @throws UnexpectedValueException if the command response was malformed 88 * @throws DriverRuntimeException for other driver errors (e.g. connection errors) 89 */ 90 public function execute(Server $server) 91 { 92 $cmd = ['listDatabases' => 1]; 93 94 if (! empty($this->options['filter'])) { 95 $cmd['filter'] = (object) $this->options['filter']; 96 } 97 98 if (isset($this->options['maxTimeMS'])) { 99 $cmd['maxTimeMS'] = $this->options['maxTimeMS']; 100 } 101 102 $cursor = $server->executeReadCommand('admin', new Command($cmd), $this->createOptions()); 103 $cursor->setTypeMap(['root' => 'array', 'document' => 'array']); 104 $result = current($cursor->toArray()); 105 106 if (! isset($result['databases']) || ! is_array($result['databases'])) { 107 throw new UnexpectedValueException('listDatabases command did not return a "databases" array'); 108 } 109 110 /* Return an Iterator instead of an array in case listDatabases is 111 * eventually changed to return a command cursor, like the collection 112 * and index enumeration commands. This makes the "totalSize" command 113 * field inaccessible, but users can manually invoke the command if they 114 * need that value. 115 */ 116 return new DatabaseInfoLegacyIterator($result['databases']); 117 } 118 119 /** 120 * Create options for executing the command. 121 * 122 * Note: read preference is intentionally omitted, as the spec requires that 123 * the command be executed on the primary. 124 * 125 * @see http://php.net/manual/en/mongodb-driver-server.executecommand.php 126 * @return array 127 */ 128 private function createOptions() 129 { 130 $options = []; 131 132 if (isset($this->options['session'])) { 133 $options['session'] = $this->options['session']; 134 } 135 136 return $options; 137 } 138 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body