You are here

public function SensorManager::rebuildSensors in Monitoring 8

Rebuild the sensor list.

Automatically creates sensors based on new

File

src/Sensor/SensorManager.php, line 242
Contains \Drupal\monitoring\Sensor\SensorManager.

Class

SensorManager
Manages sensor definitions and settings.

Namespace

Drupal\monitoring\Sensor

Code

public function rebuildSensors() {

  // Declaring a flag for updated sensors.
  $updated_sensors = FALSE;

  // Load .install files
  include DRUPAL_ROOT . '/core/includes/install.inc';
  drupal_load_updates();
  $storage = $this->entityTypeManager
    ->getStorage('monitoring_sensor_config');
  $this->moduleHandler
    ->resetImplementations();

  // Iterate through the installed implemented modules to see if
  // there are any new requirements hook updates and initialize them.
  foreach ($this->moduleHandler
    ->getImplementations('requirements') as $module) {
    if (!$storage
      ->load('core_requirements_' . $module)) {
      if (initialize_requirements_sensors($module)) {
        $this->messenger
          ->addMessage($this
          ->t('The sensor @sensor has been added.', [
          '@sensor' => $storage
            ->load('core_requirements_' . $module)
            ->label(),
        ]));
        $updated_sensors = TRUE;
      }
    }
  }

  // Delete any updated sensors that are not implemented in the requirements
  // hook anymore.
  $sensor_ids = $storage
    ->getQuery()
    ->condition('plugin_id', 'core_requirements')
    ->execute();

  /** @var \Drupal\monitoring\SensorConfigInterface $sensor */
  foreach ($storage
    ->loadMultiple($sensor_ids) as $sensor) {
    $module = $sensor
      ->getSetting('module');
    if (!$this->moduleHandler
      ->implementsHook($module, 'requirements')) {
      $this->messenger
        ->addMessage($this
        ->t('The sensor @sensor has been removed.', [
        '@sensor' => $sensor
          ->label(),
      ]));
      $sensor
        ->delete();
      $updated_sensors = TRUE;

      // Remove the sensor from the list of available sensors.
      $available_sensors = \Drupal::state()
        ->get('monitoring.available_sensors', []);
      unset($available_sensors[$sensor
        ->id()]);
      \Drupal::state()
        ->set('monitoring.available_sensors', $available_sensors);
    }
  }

  /** @var \Drupal\Core\Config\StorageInterface[] $config_storages */
  $config_storages[] = new FileStorage($this->moduleHandler
    ->getModule('monitoring')
    ->getPath() . '/config/install');
  $config_storages[] = new FileStorage($this->moduleHandler
    ->getModule('monitoring')
    ->getPath() . '/config/optional');

  // Rebuilds all non-addable sensors.
  foreach ($this
    ->getDefinitions() as $sensor_definition) {
    if (!$sensor_definition['addable']) {
      if ($sensor_definition['id'] !== 'update_status') {
        $config_ids = [
          $sensor_definition['id'],
        ];
      }
      else {
        $config_ids = [
          'update_core',
          'update_contrib',
        ];
      }
      foreach ($config_ids as $config_id) {

        // Checks if the sensor is not created.
        if (!$storage
          ->load($config_id)) {

          // Check the two directories install and optional for sensors that need to be created.
          foreach ($config_storages as $config_storage) {
            if ($data = $config_storage
              ->read('monitoring.sensor_config.' . $config_id)) {
              $storage
                ->create($data)
                ->trustData()
                ->save();
              $this->messenger
                ->addMessage($this
                ->t('The sensor @sensor has been created.', [
                '@sensor' => (string) $sensor_definition['label'],
              ]));
              $updated_sensors = TRUE;
              break;
            }
          }
        }
      }
    }
  }

  // Set message to inform the user that there were no updated sensors.
  if ($updated_sensors == FALSE) {
    $this->messenger
      ->addMessage($this
      ->t('No changes were made.'));
  }
}