See Release Notes
Long Term Support Release
Differences Between: [Versions 311 and 401] [Versions 400 and 401]
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 * https://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\Model\CachingIterator; 26 use MongoDB\Operation\Executable; 27 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 listCollections command. 35 * 36 * @internal 37 * @see https://mongodb.com/docs/manual/reference/command/listCollections/ 38 */ 39 class ListCollections implements Executable 40 { 41 /** @var string */ 42 private $databaseName; 43 44 /** @var array */ 45 private $options; 46 47 /** 48 * Constructs a listCollections command. 49 * 50 * Supported options: 51 * 52 * * authorizedCollections (boolean): Determines which collections are 53 * returned based on the user privileges. 54 * 55 * For servers < 4.0, this option is ignored. 56 * 57 * * comment (mixed): BSON value to attach as a comment to this command. 58 * 59 * This is not supported for servers versions < 4.4. 60 * 61 * * filter (document): Query by which to filter collections. 62 * 63 * * maxTimeMS (integer): The maximum amount of time to allow the query to 64 * run. 65 * 66 * * nameOnly (boolean): A flag to indicate whether the command should 67 * return just the collection/view names and type or return both the name 68 * and other information. 69 * 70 * * session (MongoDB\Driver\Session): Client session. 71 * 72 * @param string $databaseName Database name 73 * @param array $options Command options 74 * @throws InvalidArgumentException for parameter/option parsing errors 75 */ 76 public function __construct(string $databaseName, array $options = []) 77 { 78 if (isset($options['authorizedCollections']) && ! is_bool($options['authorizedCollections'])) { 79 throw InvalidArgumentException::invalidType('"authorizedCollections" option', $options['authorizedCollections'], 'boolean'); 80 } 81 82 if (isset($options['filter']) && ! is_array($options['filter']) && ! is_object($options['filter'])) { 83 throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], 'array or object'); 84 } 85 86 if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) { 87 throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer'); 88 } 89 90 if (isset($options['nameOnly']) && ! is_bool($options['nameOnly'])) { 91 throw InvalidArgumentException::invalidType('"nameOnly" option', $options['nameOnly'], 'boolean'); 92 } 93 94 if (isset($options['session']) && ! $options['session'] instanceof Session) { 95 throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class); 96 } 97 98 $this->databaseName = $databaseName; 99 $this->options = $options; 100 } 101 102 /** 103 * Execute the operation. 104 * 105 * @see Executable::execute() 106 * @throws DriverRuntimeException for other driver errors (e.g. connection errors) 107 */ 108 public function execute(Server $server): CachingIterator 109 { 110 $cursor = $server->executeReadCommand($this->databaseName, $this->createCommand(), $this->createOptions()); 111 $cursor->setTypeMap(['root' => 'array', 'document' => 'array']); 112 113 return new CachingIterator($cursor); 114 } 115 116 /** 117 * Create the listCollections command. 118 */ 119 private function createCommand(): Command 120 { 121 $cmd = ['listCollections' => 1]; 122 123 if (! empty($this->options['filter'])) { 124 $cmd['filter'] = (object) $this->options['filter']; 125 } 126 127 foreach (['authorizedCollections', 'comment', 'maxTimeMS', 'nameOnly'] as $option) { 128 if (isset($this->options[$option])) { 129 $cmd[$option] = $this->options[$option]; 130 } 131 } 132 133 return new Command($cmd); 134 } 135 136 /** 137 * Create options for executing the command. 138 * 139 * Note: read preference is intentionally omitted, as the spec requires that 140 * the command be executed on the primary. 141 * 142 * @see https://php.net/manual/en/mongodb-driver-server.executecommand.php 143 */ 144 private function createOptions(): array 145 { 146 $options = []; 147 148 if (isset($this->options['session'])) { 149 $options['session'] = $this->options['session']; 150 } 151 152 return $options; 153 } 154 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body