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\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  use function is_array;
  28  use function is_bool;
  29  use function is_integer;
  30  use function is_object;
  31  
  32  /**
  33   * Wrapper for the listCollections command.
  34   *
  35   * @internal
  36   * @see http://docs.mongodb.org/manual/reference/command/listCollections/
  37   */
  38  class ListCollections implements Executable
  39  {
  40      /** @var string */
  41      private $databaseName;
  42  
  43      /** @var array */
  44      private $options;
  45  
  46      /**
  47       * Constructs a listCollections command.
  48       *
  49       * Supported options:
  50       *
  51       *  * filter (document): Query by which to filter collections.
  52       *
  53       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  54       *    run.
  55       *
  56       *  * nameOnly (boolean): A flag to indicate whether the command should
  57       *    return just the collection/view names and type or return both the name
  58       *    and other information.
  59       *
  60       *  * session (MongoDB\Driver\Session): Client session.
  61       *
  62       *    Sessions are not supported for server versions < 3.6.
  63       *
  64       * @param string $databaseName Database name
  65       * @param array  $options      Command options
  66       * @throws InvalidArgumentException for parameter/option parsing errors
  67       */
  68      public function __construct($databaseName, array $options = [])
  69      {
  70          if (isset($options['filter']) && ! is_array($options['filter']) && ! is_object($options['filter'])) {
  71              throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], 'array or object');
  72          }
  73  
  74          if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
  75              throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
  76          }
  77  
  78          if (isset($options['nameOnly']) && ! is_bool($options['nameOnly'])) {
  79              throw InvalidArgumentException::invalidType('"nameOnly" option', $options['nameOnly'], 'boolean');
  80          }
  81  
  82          if (isset($options['session']) && ! $options['session'] instanceof Session) {
  83              throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
  84          }
  85  
  86          $this->databaseName = (string) $databaseName;
  87          $this->options = $options;
  88      }
  89  
  90      /**
  91       * Execute the operation.
  92       *
  93       * @see Executable::execute()
  94       * @param Server $server
  95       * @return CachingIterator
  96       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
  97       */
  98      public function execute(Server $server)
  99      {
 100          $cmd = ['listCollections' => 1];
 101  
 102          if (! empty($this->options['filter'])) {
 103              $cmd['filter'] = (object) $this->options['filter'];
 104          }
 105  
 106          if (isset($this->options['maxTimeMS'])) {
 107              $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
 108          }
 109  
 110          if (isset($this->options['nameOnly'])) {
 111              $cmd['nameOnly'] = $this->options['nameOnly'];
 112          }
 113  
 114          $cursor = $server->executeReadCommand($this->databaseName, new Command($cmd), $this->createOptions());
 115          $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
 116  
 117          return new CachingIterator($cursor);
 118      }
 119  
 120      /**
 121       * Create options for executing the command.
 122       *
 123       * Note: read preference is intentionally omitted, as the spec requires that
 124       * the command be executed on the primary.
 125       *
 126       * @see http://php.net/manual/en/mongodb-driver-server.executecommand.php
 127       * @return array
 128       */
 129      private function createOptions()
 130      {
 131          $options = [];
 132  
 133          if (isset($this->options['session'])) {
 134              $options['session'] = $this->options['session'];
 135          }
 136  
 137          return $options;
 138      }
 139  }