Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  /*
   3   * Copyright 2015-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\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\Driver\WriteConcern;
  25  use MongoDB\Exception\InvalidArgumentException;
  26  
  27  use function current;
  28  use function is_array;
  29  
  30  /**
  31   * Operation for the dropDatabase command.
  32   *
  33   * @api
  34   * @see \MongoDB\Client::dropDatabase()
  35   * @see \MongoDB\Database::drop()
  36   * @see https://mongodb.com/docs/manual/reference/command/dropDatabase/
  37   */
  38  class DropDatabase implements Executable
  39  {
  40      /** @var string */
  41      private $databaseName;
  42  
  43      /** @var array */
  44      private $options;
  45  
  46      /**
  47       * Constructs a dropDatabase command.
  48       *
  49       * Supported options:
  50       *
  51       *  * comment (mixed): BSON value to attach as a comment to this command.
  52       *
  53       *    This is not supported for servers versions < 4.4.
  54       *
  55       *  * session (MongoDB\Driver\Session): Client session.
  56       *
  57       *  * typeMap (array): Type map for BSON deserialization. This will be used
  58       *    for the returned command result document.
  59       *
  60       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  61       *
  62       * @param string $databaseName Database name
  63       * @param array  $options      Command options
  64       * @throws InvalidArgumentException for parameter/option parsing errors
  65       */
  66      public function __construct(string $databaseName, array $options = [])
  67      {
  68          if (isset($options['session']) && ! $options['session'] instanceof Session) {
  69              throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
  70          }
  71  
  72          if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
  73              throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
  74          }
  75  
  76          if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
  77              throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
  78          }
  79  
  80          if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
  81              unset($options['writeConcern']);
  82          }
  83  
  84          $this->databaseName = $databaseName;
  85          $this->options = $options;
  86      }
  87  
  88      /**
  89       * Execute the operation.
  90       *
  91       * @see Executable::execute()
  92       * @return array|object Command result document
  93       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
  94       */
  95      public function execute(Server $server)
  96      {
  97          $cursor = $server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions());
  98  
  99          if (isset($this->options['typeMap'])) {
 100              $cursor->setTypeMap($this->options['typeMap']);
 101          }
 102  
 103          return current($cursor->toArray());
 104      }
 105  
 106      /**
 107       * Create the dropDatabase command.
 108       */
 109      private function createCommand(): Command
 110      {
 111          $cmd = ['dropDatabase' => 1];
 112  
 113          if (isset($this->options['comment'])) {
 114              $cmd['comment'] = $this->options['comment'];
 115          }
 116  
 117          return new Command($cmd);
 118      }
 119  
 120      /**
 121       * Create options for executing the command.
 122       *
 123       * @see https://php.net/manual/en/mongodb-driver-server.executewritecommand.php
 124       */
 125      private function createOptions(): array
 126      {
 127          $options = [];
 128  
 129          if (isset($this->options['session'])) {
 130              $options['session'] = $this->options['session'];
 131          }
 132  
 133          if (isset($this->options['writeConcern'])) {
 134              $options['writeConcern'] = $this->options['writeConcern'];
 135          }
 136  
 137          return $options;
 138      }
 139  }