|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2020-present 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\Command; |
| 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\Operation\Executable; |
| 27 | +use function current; |
| 28 | +use function is_array; |
| 29 | +use function is_bool; |
| 30 | +use function is_integer; |
| 31 | +use function is_object; |
| 32 | + |
| 33 | +/** |
| 34 | + * Wrapper for the ListDatabases command. |
| 35 | + * |
| 36 | + * @internal |
| 37 | + * @see http://docs.mongodb.org/manual/reference/command/listDatabases/ |
| 38 | + */ |
| 39 | +class ListDatabases implements Executable |
| 40 | +{ |
| 41 | + /** @var array */ |
| 42 | + private $options; |
| 43 | + |
| 44 | + /** |
| 45 | + * Constructs a listDatabases command. |
| 46 | + * |
| 47 | + * Supported options: |
| 48 | + * |
| 49 | + * * authorizedDatabases (boolean): Determines which databases are returned |
| 50 | + * based on the user privileges. |
| 51 | + * |
| 52 | + * For servers < 4.0.5, this option is ignored. |
| 53 | + * |
| 54 | + * * filter (document): Query by which to filter databases. |
| 55 | + * |
| 56 | + * For servers < 3.6, this option is ignored. |
| 57 | + * |
| 58 | + * * maxTimeMS (integer): The maximum amount of time to allow the query to |
| 59 | + * run. |
| 60 | + * |
| 61 | + * * nameOnly (boolean): A flag to indicate whether the command should |
| 62 | + * return just the database names, or return both database names and size |
| 63 | + * information. |
| 64 | + * |
| 65 | + * * session (MongoDB\Driver\Session): Client session. |
| 66 | + * |
| 67 | + * Sessions are not supported for server versions < 3.6. |
| 68 | + * |
| 69 | + * @param array $options Command options |
| 70 | + * @throws InvalidArgumentException for parameter/option parsing errors |
| 71 | + */ |
| 72 | + public function __construct(array $options = []) |
| 73 | + { |
| 74 | + if (isset($options['authorizedDatabases']) && ! is_bool($options['authorizedDatabases'])) { |
| 75 | + throw InvalidArgumentException::invalidType('"authorizedDatabases" option', $options['authorizedDatabases'], 'boolean'); |
| 76 | + } |
| 77 | + |
| 78 | + if (isset($options['filter']) && ! is_array($options['filter']) && ! is_object($options['filter'])) { |
| 79 | + throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], ['array', 'object']); |
| 80 | + } |
| 81 | + |
| 82 | + if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) { |
| 83 | + throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer'); |
| 84 | + } |
| 85 | + |
| 86 | + if (isset($options['nameOnly']) && ! is_bool($options['nameOnly'])) { |
| 87 | + throw InvalidArgumentException::invalidType('"nameOnly" option', $options['nameOnly'], 'boolean'); |
| 88 | + } |
| 89 | + |
| 90 | + if (isset($options['session']) && ! $options['session'] instanceof Session) { |
| 91 | + throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class); |
| 92 | + } |
| 93 | + |
| 94 | + $this->options = $options; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Execute the operation. |
| 99 | + * |
| 100 | + * @see Executable::execute() |
| 101 | + * @param Server $server |
| 102 | + * @return array An array of database info structures |
| 103 | + * @throws UnexpectedValueException if the command response was malformed |
| 104 | + * @throws DriverRuntimeException for other driver errors (e.g. connection errors) |
| 105 | + */ |
| 106 | + public function execute(Server $server) |
| 107 | + { |
| 108 | + $cmd = ['listDatabases' => 1]; |
| 109 | + |
| 110 | + if (isset($this->options['authorizedDatabases'])) { |
| 111 | + $cmd['authorizedDatabases'] = $this->options['authorizedDatabases']; |
| 112 | + } |
| 113 | + |
| 114 | + if (! empty($this->options['filter'])) { |
| 115 | + $cmd['filter'] = (object) $this->options['filter']; |
| 116 | + } |
| 117 | + |
| 118 | + if (isset($this->options['maxTimeMS'])) { |
| 119 | + $cmd['maxTimeMS'] = $this->options['maxTimeMS']; |
| 120 | + } |
| 121 | + |
| 122 | + if (isset($this->options['nameOnly'])) { |
| 123 | + $cmd['nameOnly'] = $this->options['nameOnly']; |
| 124 | + } |
| 125 | + |
| 126 | + $cursor = $server->executeReadCommand('admin', new Command($cmd), $this->createOptions()); |
| 127 | + $cursor->setTypeMap(['root' => 'array', 'document' => 'array']); |
| 128 | + $result = current($cursor->toArray()); |
| 129 | + |
| 130 | + if (! isset($result['databases']) || ! is_array($result['databases'])) { |
| 131 | + throw new UnexpectedValueException('listDatabases command did not return a "databases" array'); |
| 132 | + } |
| 133 | + |
| 134 | + return $result['databases']; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Create options for executing the command. |
| 139 | + * |
| 140 | + * Note: read preference is intentionally omitted, as the spec requires that |
| 141 | + * the command be executed on the primary. |
| 142 | + * |
| 143 | + * @see http://php.net/manual/en/mongodb-driver-server.executecommand.php |
| 144 | + * @return array |
| 145 | + */ |
| 146 | + private function createOptions() |
| 147 | + { |
| 148 | + $options = []; |
| 149 | + |
| 150 | + if (isset($this->options['session'])) { |
| 151 | + $options['session'] = $this->options['session']; |
| 152 | + } |
| 153 | + |
| 154 | + return $options; |
| 155 | + } |
| 156 | +} |
0 commit comments