Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 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\Model\CachingIterator;
  26  use MongoDB\Model\CollectionInfoCommandIterator;
  27  use MongoDB\Model\CollectionInfoIterator;
  28  use function is_array;
  29  use function is_integer;
  30  use function is_object;
  31  
  32  /**
  33   * Operation for the listCollections command.
  34   *
  35   * @api
  36   * @see \MongoDB\Database::listCollections()
  37   * @see http://docs.mongodb.org/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       *  * filter (document): Query by which to filter collections.
  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 string $databaseName Database name
  62       * @param array  $options      Command options
  63       * @throws InvalidArgumentException for parameter/option parsing errors
  64       */
  65      public function __construct($databaseName, array $options = [])
  66      {
  67          if (isset($options['filter']) && ! is_array($options['filter']) && ! is_object($options['filter'])) {
  68              throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], 'array or object');
  69          }
  70  
  71          if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
  72              throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
  73          }
  74  
  75          if (isset($options['session']) && ! $options['session'] instanceof Session) {
  76              throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
  77          }
  78  
  79          $this->databaseName = (string) $databaseName;
  80          $this->options = $options;
  81      }
  82  
  83      /**
  84       * Execute the operation.
  85       *
  86       * @see Executable::execute()
  87       * @param Server $server
  88       * @return CollectionInfoIterator
  89       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
  90       */
  91      public function execute(Server $server)
  92      {
  93          return $this->executeCommand($server);
  94      }
  95  
  96      /**
  97       * Create options for executing the command.
  98       *
  99       * Note: read preference is intentionally omitted, as the spec requires that
 100       * the command be executed on the primary.
 101       *
 102       * @see http://php.net/manual/en/mongodb-driver-server.executecommand.php
 103       * @return array
 104       */
 105      private function createOptions()
 106      {
 107          $options = [];
 108  
 109          if (isset($this->options['session'])) {
 110              $options['session'] = $this->options['session'];
 111          }
 112  
 113          return $options;
 114      }
 115  
 116      /**
 117       * Returns information for all collections in this database using the
 118       * listCollections command.
 119       *
 120       * @param Server $server
 121       * @return CollectionInfoCommandIterator
 122       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 123       */
 124      private function executeCommand(Server $server)
 125      {
 126          $cmd = ['listCollections' => 1];
 127  
 128          if (! empty($this->options['filter'])) {
 129              $cmd['filter'] = (object) $this->options['filter'];
 130          }
 131  
 132          if (isset($this->options['maxTimeMS'])) {
 133              $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
 134          }
 135  
 136          $cursor = $server->executeReadCommand($this->databaseName, new Command($cmd), $this->createOptions());
 137          $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
 138  
 139          return new CollectionInfoCommandIterator(new CachingIterator($cursor));
 140      }
 141  }