Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 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   *   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 ArrayIterator;
  21  use Iterator;
  22  use MongoDB\Command\ListDatabases as ListDatabasesCommand;
  23  use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
  24  use MongoDB\Driver\Server;
  25  use MongoDB\Exception\InvalidArgumentException;
  26  use MongoDB\Exception\UnexpectedValueException;
  27  use function array_column;
  28  
  29  /**
  30   * Operation for the ListDatabases command, returning only database names.
  31   *
  32   * @api
  33   * @see \MongoDB\Client::listDatabaseNames()
  34   * @see http://docs.mongodb.org/manual/reference/command/ListDatabases/
  35   */
  36  class ListDatabaseNames implements Executable
  37  {
  38      /** @var ListDatabasesCommand */
  39      private $listDatabases;
  40  
  41      /**
  42       * Constructs a listDatabases command.
  43       *
  44       * Supported options:
  45       *
  46       *  * authorizedDatabases (boolean): Determines which databases are returned
  47       *    based on the user privileges.
  48       *
  49       *    For servers < 4.0.5, this option is ignored.
  50       *
  51       *  * filter (document): Query by which to filter databases.
  52       *
  53       *    For servers < 3.6, this option is ignored.
  54       *
  55       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  56       *    run.
  57       *
  58       *  * session (MongoDB\Driver\Session): Client session.
  59       *
  60       *    Sessions are not supported for server versions < 3.6.
  61       *
  62       * @param array $options Command options
  63       * @throws InvalidArgumentException for parameter/option parsing errors
  64       */
  65      public function __construct(array $options = [])
  66      {
  67          $this->listDatabases = new ListDatabasesCommand(['nameOnly' => true] + $options);
  68      }
  69  
  70      /**
  71       * Execute the operation.
  72       *
  73       * @see Executable::execute()
  74       * @param Server $server
  75       * @return Iterator
  76       * @throws UnexpectedValueException if the command response was malformed
  77       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
  78       */
  79      public function execute(Server $server) : Iterator
  80      {
  81          $result = $this->listDatabases->execute($server);
  82  
  83          return new ArrayIterator(array_column($result, 'name'));
  84      }
  85  }