public function MonitoringSensorResultResource::get in Monitoring 8
Responds to sensor INFO GET requests.
Parameters
string $id: (optional) The sensor name, returns a list of all sensors when empty.
Return value
\Drupal\rest\ResourceResponse The response containing the sensor config.
Throws
\Symfony\Component\HttpKernel\Exception\HttpException
File
- src/
Plugin/ rest/ resource/ MonitoringSensorResultResource.php, line 113 - Definition of Drupal\monitoring\Plugin\rest\resource\MonitoringSensorInfoResource.
Class
- MonitoringSensorResultResource
- Provides a resource for monitoring sensors results.
Namespace
Drupal\monitoring\Plugin\rest\resourceCode
public function get($id = NULL) {
$request = \Drupal::request();
$format = $request
->getRequestFormat('ĵson');
if ($id) {
try {
$sensor_config[$id] = $this->sensorManager
->getSensorConfigByName($id);
// Some sensors might render or do things that we can not properly
// collect cacheability metadata for. So, run it in our own render
// context. For example, one is the run cron link of the system.module
// requirements hook.
$context = new RenderContext();
$sensor_runner = $this->sensorRunner;
$result = $this->renderer
->executeInRenderContext($context, function () use ($sensor_runner, $sensor_config) {
return $sensor_runner
->runSensors($sensor_config);
});
$response = $result[$id]
->toArray();
$url = Url::fromRoute('rest.monitoring-sensor-result.GET', [
'id' => $id,
'_format' => $format,
])
->setAbsolute()
->toString(TRUE);
$response['uri'] = $url
->getGeneratedUrl();
if ($request
->get('expand') == 'sensor') {
$response['sensor'] = $result[$id]
->getSensorConfig()
->toArray();
}
$response = new ResourceResponse($response);
$response
->addCacheableDependency($result[$id]
->getSensorConfig());
$response
->addCacheableDependency($url);
$response
->addCacheableDependency(CacheableMetadata::createFromRenderArray([
'#cache' => [
'contexts' => [
0 => 'url.query_args',
],
'max-age' => 0,
],
]));
if (!$context
->isEmpty()) {
$response
->addCacheableDependency($context
->pop());
}
return $response;
} catch (NonExistingSensorException $e) {
throw new NotFoundHttpException($e
->getMessage(), $e);
} catch (DisabledSensorException $e) {
throw new NotFoundHttpException($e
->getMessage(), $e);
}
}
else {
$list = array();
$cacheable_metadata = new CacheableMetadata();
// Some sensors might render or do things that we can not properly
// collect cacheability metadata for. So, run it in our own render
// context. For example, one is the run cron link of the system.module
// requirements hook.
$context = new RenderContext();
$sensor_runner = $this->sensorRunner;
$results = \Drupal::service('renderer')
->executeInRenderContext($context, function () use ($sensor_runner) {
return $sensor_runner
->runSensors();
});
foreach ($results as $id => $result) {
$list[$id] = $result
->toArray();
$url = Url::fromRoute('rest.monitoring-sensor-result.GET', [
'id' => $id,
'_format' => $format,
])
->setAbsolute()
->toString(TRUE);
$list[$id]['uri'] = $url
->getGeneratedUrl();
if ($request
->get('expand') == 'sensor') {
$list[$id]['sensor'] = $result
->getSensorConfig()
->toArray();
}
$cacheable_metadata = $cacheable_metadata
->merge($url);
$cacheable_metadata = $cacheable_metadata
->merge(CacheableMetadata::createFromObject($result
->getSensorConfig()));
$cacheable_metadata = $cacheable_metadata
->merge(CacheableMetadata::createFromRenderArray([
'#cache' => [
'max-age' => 0,
],
]));
}
$response = new ResourceResponse($list);
$response
->addCacheableDependency($cacheable_metadata);
if (!$context
->isEmpty()) {
$response
->addCacheableDependency($context
->pop());
}
return $response;
}
}