You are here

mongodb_watchdog.install in MongoDB 8.2

MongoDB watchdog install file.

File

modules/mongodb_watchdog/mongodb_watchdog.install
View source
<?php

/**
 * @file
 * MongoDB watchdog install file.
 */
use Drupal\mongodb_watchdog\Install\Requirements;
use Drupal\mongodb_watchdog\Logger;

/**
 * Implements hook_install().
 */
function mongodb_watchdog_install() {

  // Autoloader naming the service is not yet available.
  $name = 'mongodb.logger';

  // Service may have been instantiated with an empty config before this hook,
  // so drop the instance to create a new one now that config is available.
  \Drupal::getContainer()
    ->set($name, NULL);

  /** @var \Drupal\mongodb_watchdog\Logger $logger */
  $logger = \Drupal::service($name);
  $logger
    ->ensureSchema();
}

/**
 * Implements hook_requirements().
 *
 * - Ensure a logger alias
 * - Ensure the logger alias does not point to the same DB as another alias.
 *
 * @see http://blog.riff.org/2015_08_27_drupal_8_tip_of_the_day_autoloaded_code_in_a_module_install_file
 */
function mongodb_watchdog_requirements($phase) {
  if ($phase === 'install') {

    // Dependencies may not be installed yet, and module isn't either.
    require_once __DIR__ . "/../mongodb/mongodb.module";
    require_once __DIR__ . "/../mongodb/src/MongoDb.php";
    require_once __DIR__ . "/src/Logger.php";
    require_once __DIR__ . "/src/Install/Requirements.php";

    // Module is not yet available so its services aren't either.
    $requirements = \Drupal::classResolver()
      ->getInstanceFromDefinition(Requirements::class);
  }
  else {

    // Outside install phase, the whole module is available.

    /** @var \Drupal\mongodb_watchdog\Install\Requirements $requirements */
    $requirements = \Drupal::service(Logger::SERVICE_REQUIREMENTS);

    /** @var \Drupal\mongodb_watchdog\Logger $logger */
    $logger = \Drupal::service(Logger::SERVICE_LOGGER);
    $logger
      ->ensureSchema();
  }
  return $requirements
    ->check($phase);
}

/**
 * Implements hook_uninstall().
 *
 * Drop /all/ the watchdog collections by dropping the logger database.
 */
function mongodb_watchdog_uninstall() {

  /** @var \MongoDB\Tests\Database\ $logger */
  $logger = \Drupal::service('mongodb.watchdog_storage');
  $logger
    ->drop();
}