/src/Sensor/SensorManager.php

https://gitlab.com/Drulenium-bot/monitoring · PHP · 237 lines · 94 code · 24 blank · 119 comment · 9 complexity · 02433f671a3aeee53f5361bf490d4ccd MD5 · raw file

  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\monitoring\Sensor\SensorManager.
  5. */
  6. namespace Drupal\monitoring\Sensor;
  7. use Drupal\Component\Utility\SafeMarkup;
  8. use Drupal\Core\Config\ConfigFactoryInterface;
  9. use Drupal\Core\Extension\ModuleHandlerInterface;
  10. use Drupal\Core\Plugin\DefaultPluginManager;
  11. use Drupal\Core\Cache\CacheBackendInterface;
  12. use Drupal\monitoring\Entity\SensorConfig;
  13. use Drupal\monitoring\SensorPlugin\SensorPluginInterface;
  14. /**
  15. * Manages sensor definitions and settings.
  16. *
  17. * Provides list of enabled sensors.
  18. * Sensors can be listed by category.
  19. *
  20. * Maintains a (non persistent) info cache.
  21. * Enables and disables sensors.
  22. *
  23. */
  24. class SensorManager extends DefaultPluginManager {
  25. /**
  26. * List of sensor definitions.
  27. *
  28. * @var \Drupal\monitoring\Entity\SensorConfig[]
  29. */
  30. protected $sensor_config;
  31. /**
  32. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  33. */
  34. protected $moduleHandler;
  35. /**
  36. * @var \Drupal\Core\Config\ConfigFactoryInterface
  37. */
  38. protected $config;
  39. /**
  40. * Constructs a sensor manager.
  41. *
  42. * @param \Traversable $namespaces
  43. * An object that implements \Traversable which contains the root paths
  44. * keyed by the corresponding namespace to look for plugin implementations.
  45. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  46. * Cache backend instance to use.
  47. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  48. * The module handler to invoke the alter hook with.
  49. */
  50. function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config) {
  51. parent::__construct('Plugin/monitoring/SensorPlugin', $namespaces, $module_handler, '\Drupal\monitoring\SensorPlugin\SensorPluginInterface', 'Drupal\monitoring\Annotation\SensorPlugin');
  52. $this->alterInfo('block');
  53. $this->setCacheBackend($cache_backend, 'monitoring_sensor_plugins');
  54. $this->config = $config;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function createInstance($plugin_id, array $configuration = array()) {
  60. // Configuration contains SensorConfig object. Extracting
  61. // it to use for sensor object creation.
  62. $sensor_config = $configuration['sensor_config'];
  63. $definition = $this->getDefinition($plugin_id);
  64. // SensorPlugin class from the sensor definition.
  65. /** @var \Drupal\monitoring\SensorPlugin\SensorPluginInterface $class */
  66. $class = $definition['class'];
  67. // Creating instance of the sensor. Refer SensorPlugin.php for arguments.
  68. return $class::create(\Drupal::getContainer(), $sensor_config, $plugin_id, $definition);
  69. }
  70. /**
  71. * Returns monitoring sensor config.
  72. *
  73. * @return \Drupal\monitoring\Entity\SensorConfig[]
  74. * List of SensorConfig instances.
  75. */
  76. public function getAllSensorConfig() {
  77. $sensors = SensorConfig::loadMultiple();
  78. // Sort the sensors by category and label.
  79. uasort($sensors, "\Drupal\monitoring\Entity\SensorConfig::sort");
  80. return $sensors;
  81. }
  82. /**
  83. * Returns monitoring sensor config for enabled sensors.
  84. *
  85. * @return \Drupal\monitoring\Entity\SensorConfig[]
  86. * List of SensorConfig instances.
  87. */
  88. public function getEnabledSensorConfig() {
  89. $enabled_sensors = array();
  90. foreach ($this->getAllSensorConfig() as $sensor_config) {
  91. if ($sensor_config->isEnabled()) {
  92. $enabled_sensors[$sensor_config->id()] = $sensor_config;
  93. }
  94. }
  95. return $enabled_sensors;
  96. }
  97. /**
  98. * Returns monitoring sensor config for a given sensor.
  99. *
  100. * Directly use SensorConfig::load($name) if sensor existence assured.
  101. *
  102. * @param string $sensor_name
  103. * Sensor id.
  104. *
  105. * @return \Drupal\monitoring\Entity\SensorConfig
  106. * A single SensorConfig instance.
  107. *
  108. * @throws \Drupal\monitoring\Sensor\NonExistingSensorException
  109. * Thrown if the requested sensor does not exist.
  110. */
  111. public function getSensorConfigByName($sensor_name) {
  112. $sensor_config = SensorConfig::load($sensor_name);
  113. if ($sensor_config == NULL) {
  114. throw new NonExistingSensorException(SafeMarkup::format('Sensor @sensor_name does not exist', array('@sensor_name' => $sensor_name)));
  115. }
  116. return $sensor_config;
  117. }
  118. /**
  119. * Gets sensor config grouped by categories.
  120. *
  121. * @todo: The enabled flag is strange, FALSE should return all?
  122. *
  123. * @param bool $enabled
  124. * Sensor isEnabled flag.
  125. *
  126. * @return \Drupal\monitoring\Entity\SensorConfig[][]
  127. * Sensor config.
  128. */
  129. public function getSensorConfigByCategories($enabled = TRUE) {
  130. $config_by_categories = array();
  131. foreach ($this->getAllSensorConfig() as $sensor_name => $sensor_config) {
  132. if ($sensor_config->isEnabled() != $enabled) {
  133. continue;
  134. }
  135. $config_by_categories[$sensor_config->getCategory()][$sensor_name] = $sensor_config;
  136. }
  137. return $config_by_categories;
  138. }
  139. /**
  140. * Reset the static cache.
  141. */
  142. public function resetCache() {
  143. $this->sensor_config = array();
  144. }
  145. /**
  146. * Enable a sensor.
  147. *
  148. * Checks if the sensor is enabled and enables it if not.
  149. *
  150. * @param string $sensor_name
  151. * Sensor name to be enabled.
  152. *
  153. * @throws \Drupal\monitoring\Sensor\NonExistingSensorException
  154. * Thrown if the requested sensor does not exist.
  155. */
  156. public function enableSensor($sensor_name) {
  157. $sensor_config = $this->getSensorConfigByName($sensor_name);
  158. if (!$sensor_config->isEnabled()) {
  159. $sensor_config->status = TRUE;
  160. $sensor_config->save();
  161. $available_sensors = \Drupal::state()->get('monitoring.available_sensors', array());
  162. if (!isset($available_sensors[$sensor_name])) {
  163. // Use the watchdog message as the disappeared sensor does when new
  164. // sensors are detected.
  165. \Drupal::logger('monitoring')->notice('@count new sensor/s added: @names',
  166. array('@count' => 1, '@names' => $sensor_name));
  167. }
  168. $available_sensors[$sensor_name]['enabled'] = TRUE;
  169. $available_sensors[$sensor_name]['name'] = $sensor_name;
  170. \Drupal::state()->set('monitoring.available_sensors', $available_sensors);
  171. }
  172. }
  173. /**
  174. * Disable a sensor.
  175. *
  176. * Checks if the sensor is enabled and if so it will disable it and remove
  177. * from the active sensor list.
  178. *
  179. * @param string $sensor_name
  180. * Sensor name to be disabled.
  181. *
  182. * @throws \Drupal\monitoring\Sensor\NonExistingSensorException
  183. * Thrown if the requested sensor does not exist.
  184. */
  185. public function disableSensor($sensor_name) {
  186. $sensor_config = $this->getSensorConfigByName($sensor_name);
  187. if ($sensor_config->isEnabled()) {
  188. $sensor_config->status = FALSE;
  189. $sensor_config->save();
  190. $available_sensors = \Drupal::state()->get('monitoring.available_sensors', array());
  191. $available_sensors[$sensor_name]['enabled'] = FALSE;
  192. $available_sensors[$sensor_name]['name'] = $sensor_name;
  193. \Drupal::state()->set('monitoring.available_sensors', $available_sensors);
  194. }
  195. }
  196. /**
  197. * Returns if an array is flat.
  198. *
  199. * @param array $array
  200. * The array to check.
  201. *
  202. * @return bool
  203. * TRUE if the array has no values that are arrays again.
  204. */
  205. protected function isFlatArray(array $array) {
  206. foreach ($array as $value) {
  207. if (is_array($value)) {
  208. return FALSE;
  209. }
  210. }
  211. return TRUE;
  212. }
  213. }