Skip to content

Commit 0173ead

Browse files
committed
WIP2
1 parent 2d1b93e commit 0173ead

File tree

2 files changed

+22
-48
lines changed

2 files changed

+22
-48
lines changed

library/Icingadb/Hook/CustomVarEnricherHook.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@ abstract class CustomVarEnricherHook
2222
abstract public function getGroups(): array;
2323

2424
/**
25-
* Return a group name for the given variable name
25+
* Return enriched vars in the following format
26+
* [label => enriched custom var]
2627
*
2728
* @param array $vars
2829
*
2930
* @return array
3031
*/
31-
abstract public function enrichCustomVars(array $vars, Model $object): array;
32+
abstract public function enrichCustomVars(array &$vars, Model $object): array;
3233

3334
public static function prepareEnrichedCustomVars(array $vars, Model $object): array
3435
{
3536
$enrichedVars = [];
36-
3737
$groups = [];
38+
3839
foreach (Hook::all('Icingadb/CustomVarEnricher') as $hook) {
3940
/** @var self $hook */
4041
try {
@@ -45,26 +46,8 @@ public static function prepareEnrichedCustomVars(array $vars, Model $object): ar
4546
}
4647
}
4748

48-
$enrichedVars = array_merge([], ...$enrichedVars);
49+
$vars = array_merge($vars, ...$enrichedVars);
4950
$groups = array_merge([], ...$groups);
50-
foreach ($vars as $key => $var) {
51-
if (array_key_exists($key, $enrichedVars)) {
52-
$label = key($enrichedVars[$key]);
53-
$vars[$label] = $enrichedVars[$key][$label];
54-
55-
unset($vars[$key]);
56-
57-
$key = $label;
58-
}
59-
60-
foreach ($groups as $group) {
61-
if (array_key_exists($key, $group)) {
62-
unset($vars[$key]);
63-
64-
break;
65-
}
66-
}
67-
}
6851

6952
return [$vars, $groups];
7053
}

library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,11 @@ class CustomVarEnricher extends CustomVarEnricherHook
2020
{
2121
protected $fieldConfig;
2222

23-
protected $datalistMaps;
23+
protected $datalistMaps = [];
2424

2525
protected $groups = [];
2626

27-
public function prefetchForObject(Model $object): bool
28-
{
29-
return false;
30-
}
31-
32-
public function renderCustomVarKey(string $key)
33-
{
34-
return $key;
35-
}
36-
37-
public function renderCustomVarValue(string $key, $value)
38-
{
39-
return $value;
40-
}
41-
42-
public function identifyCustomVarGroup(string $key): ?string
43-
{
44-
return null;
45-
}
46-
47-
public function enrichCustomVars(array $vars, Model $object): array
27+
public function enrichCustomVars(array &$vars, Model $object): array
4828
{
4929
$directorObject = null;
5030
$connection = Db::fromResourceName(Config::module('director')->get('db', 'resource'));
@@ -62,12 +42,21 @@ public function enrichCustomVars(array $vars, Model $object): array
6242
$this->fieldConfig = (new IcingaObjectFieldLoader($directorObject))->getFields();
6343

6444
$this->buildDataListMap($connection);
45+
6546
if ($directorObject) {
66-
foreach ($vars as $varName => $customVar) {
67-
$newVars[$varName] = $this->resolveCustomVarMapping($varName, $customVar, $connection);
47+
$varsToReplace = json_decode(json_encode($directorObject->getVars()), true)
48+
+ json_decode(json_encode($directorObject->getInheritedVars()), true);
49+
50+
51+
foreach ($varsToReplace as $varName => $customVar) {
52+
if (isset($vars[$varName])) {
53+
$newVars[] = $this->resolveCustomVarMapping($varName, $customVar, $connection);
54+
55+
unset($vars[$varName]);
56+
}
6857
}
69-
} else {
70-
$newVars = $vars;
58+
59+
$newVars = array_merge([], ...$newVars);
7160
}
7261

7362
return $newVars;
@@ -128,6 +117,8 @@ protected function resolveCustomVarMapping(string $name, $val, DbConnection $con
128117
} else {
129118
$this->groups[$field->getCategoryName()][$name] = $val;
130119
}
120+
121+
return [];
131122
}
132123
} elseif (is_array($val)) {
133124
$newValue = [];

0 commit comments

Comments
 (0)