From b18bc644404e02b57635bfcc8258e85abb141146 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:44:46 +0200 Subject: Adding upstream version 1.1.1. Signed-off-by: Daniel Baumann --- .../ExtensionHook/ObjectDetailExtensionHook.php | 121 +++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 library/Icingadb/Hook/ExtensionHook/ObjectDetailExtensionHook.php (limited to 'library/Icingadb/Hook/ExtensionHook/ObjectDetailExtensionHook.php') diff --git a/library/Icingadb/Hook/ExtensionHook/ObjectDetailExtensionHook.php b/library/Icingadb/Hook/ExtensionHook/ObjectDetailExtensionHook.php new file mode 100644 index 0000000..7f880e8 --- /dev/null +++ b/library/Icingadb/Hook/ExtensionHook/ObjectDetailExtensionHook.php @@ -0,0 +1,121 @@ + + * + * @throws InvalidArgumentException If the given model is not supported + */ + final public static function loadExtensions(Model $object): array + { + switch (true) { + case $object instanceof Host: + $hookName = 'Icingadb\\HostDetailExtension'; + break; + case $object instanceof Service: + $hookName = 'Icingadb\\ServiceDetailExtension'; + break; + case $object instanceof User: + $hookName = 'Icingadb\\UserDetailExtension'; + break; + case $object instanceof Usergroup: + $hookName = 'Icingadb\\UsergroupDetailExtension'; + break; + case $object instanceof History: + $hookName = 'Icingadb\\EventDetailExtension'; + break; + default: + throw new InvalidArgumentException( + sprintf('%s is not a supported object type', get_php_type($object)) + ); + } + + $extensions = []; + $lastUsedLocations = []; + + /** + * @var $hook HostDetailExtensionHook + * @var $hook ServiceDetailExtensionHook + * @var $hook UserDetailExtensionHook + * @var $hook UsergroupDetailExtensionHook + * @var $hook EventDetailExtensionHook + */ + foreach (Hook::all($hookName) as $hook) { + $location = $hook->getLocation(); + if ($location < 0) { + $location = null; + } + + if ($location === null) { + $section = $hook->getSection(); + if (! isset(self::BASE_LOCATIONS[$section])) { + Logger::error('Detail extension %s is using an invalid section: %s', get_class($hook), $section); + $section = self::DETAIL_SECTION; + } + + if (isset($lastUsedLocations[$section])) { + $location = ++$lastUsedLocations[$section]; + } else { + $location = self::BASE_LOCATIONS[$section]; + $lastUsedLocations[$section] = $location; + } + } + + try { + // It may be ValidHtml, but modules shouldn't be able to break our views. + // That's why it needs to be rendered instantly, as any error will then + // be caught here. + $extension = (string) $hook->getHtmlForObject(clone $object); + + $moduleName = $hook->getModule()->getName(); + + $extensions[$location] = new HtmlElement( + 'div', + Attributes::create([ + 'class' => 'icinga-module module-' . $moduleName, + 'data-icinga-module' => $moduleName + ]), + HtmlString::create($extension) + ); + } catch (Exception $e) { + Logger::error("Failed to load detail extension: %s\n%s", $e, $e->getTraceAsString()); + $extensions[$location] = Text::create(IcingaException::describe($e)); + } + } + + return $extensions; + } +} -- cgit v1.2.3