You are here

protected function SensorManager::loadSensorInfo in Monitoring 7

Loads sensor info from hooks.

Instantiates a SensorInfo for each sensor with merged settings. Creates also instances for disabled sensors.

Return value

\Drupal\monitoring\Sensor\SensorInfo[] List of SensorInfo instances.

See also

hook_monitoring_sensor_info()

hook_monitoring_sensor_info_alter()

1 call to SensorManager::loadSensorInfo()
SensorManager::getSensorInfo in lib/Drupal/monitoring/Sensor/SensorManager.php
Returns monitoring sensor info.

File

lib/Drupal/monitoring/Sensor/SensorManager.php, line 203
Contains \Drupal\monitoring\Sensor\SensorManager.

Class

SensorManager
Manages sensor definitions and settings.

Namespace

Drupal\monitoring\Sensor

Code

protected function loadSensorInfo() {
  $info = array();

  // A module might provide a separate file with sensor definitions. Try to
  // include it prior to checking if a hook exists.
  // @todo: Use hook_hook_info().
  foreach (module_list() as $module) {
    $sensors_file = drupal_get_path('module', $module) . '/' . $module . '.monitoring_sensors.inc';
    if (file_exists($sensors_file)) {
      require_once $sensors_file;
    }
  }

  // Collect sensors info.
  $custom_implementations = module_implements('monitoring_sensor_info');
  foreach (module_list() as $module) {

    // Favor custom implementation.
    if (in_array($module, $custom_implementations)) {
      $result = module_invoke($module, 'monitoring_sensor_info');
      $info = drupal_array_merge_deep($info, $result);
    }
    elseif (function_exists('monitoring_' . $module . '_monitoring_sensor_info')) {
      $function = 'monitoring_' . $module . '_monitoring_sensor_info';
      $result = $function();
      if (is_array($result)) {
        $info = drupal_array_merge_deep($info, $result);
      }
    }
  }

  // Allow to alter the collected sensors info.
  drupal_alter('monitoring_sensor_info', $info);

  // Merge in saved sensor settings.
  foreach ($info as $key => &$value) {

    // Set default values.
    $value += array(
      'description' => '',
      'result_class' => 'Drupal\\monitoring\\Result\\SensorResult',
      'numeric' => TRUE,
      'value_label' => NULL,
      'settings' => array(),
    );
    $value['settings'] += array(
      'enabled' => TRUE,
      'caching_time' => 0,
      'category' => 'Other',
    );
    $value['settings'] = $this
      ->mergeSettings($key, $value['settings']);
  }

  // Support variable overrides.
  // @todo This might change in https://drupal.org/node/2170955.
  $info = drupal_array_merge_deep($info, variable_get('monitoring_sensor_info', array()));

  // Convert the arrays into SensorInfo objects.
  foreach ($info as $sensor_name => $sensor_info) {
    $info[$sensor_name] = new SensorInfo($sensor_name, $sensor_info);
  }

  // Sort the sensors by category and label.
  uasort($info, "\\Drupal\\monitoring\\Sensor\\SensorManager::orderSensorInfo");
  return $info;
}