From a0901c4b7f2db488cb4fb3be2dd921a0308f4659 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:36:40 +0200 Subject: Adding upstream version 1.0.2. Signed-off-by: Daniel Baumann --- .../Hook/ExtensionHook/BaseExtensionHook.php | 146 +++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 library/Icingadb/Hook/ExtensionHook/BaseExtensionHook.php (limited to 'library/Icingadb/Hook/ExtensionHook/BaseExtensionHook.php') diff --git a/library/Icingadb/Hook/ExtensionHook/BaseExtensionHook.php b/library/Icingadb/Hook/ExtensionHook/BaseExtensionHook.php new file mode 100644 index 0000000..dfefdcd --- /dev/null +++ b/library/Icingadb/Hook/ExtensionHook/BaseExtensionHook.php @@ -0,0 +1,146 @@ + + */ + const BASE_LOCATIONS = [ + self::OUTPUT_SECTION => 1000, + self::GRAPH_SECTION => 1100, + self::DETAIL_SECTION => 1200, + self::ACTION_SECTION => 1300, + self::PROBLEM_SECTION => 1400, + self::RELATED_SECTION => 1500, + self::STATE_SECTION => 1600, + self::CONFIG_SECTION => 1700 + ]; + + /** @var int This hook's location */ + private $location = self::IDENTIFY_LOCATION_BY_SECTION; + + /** @var string This hook's section */ + private $section = self::DETAIL_SECTION; + + /** + * Set this hook's location + * + * Note that setting the location explicitly may override other widgets using the same location. But beware that + * this applies to this hook's widget as well. + * + * Also, while the sections are guaranteed to always refer to the same general location, this guarantee is lost + * when setting a location explicitly. The core and base locations may change at any time and any explicitly set + * location will **not** adjust accordingly. + * + * @param int $location + * + * @return void + */ + final public function setLocation(int $location) + { + $this->location = $location; + } + + /** + * Get this hook's location + * + * @return int + */ + final public function getLocation(): int + { + return $this->location; + } + + /** + * Set this hook's section + * + * Sections are used to place widgets loosely in a general location. Using e.g. the `state` section this hook's + * widget will always appear after the check statistics and performance data widgets. + * + * @param string $section + * + * @return void + */ + final public function setSection(string $section) + { + $this->section = $section; + } + + /** + * Get this hook's section + * + * @return string + */ + final public function getSection(): string + { + return $this->section; + } + + /** + * Union both arrays and sort the result by key + * + * @param array $coreElements + * @param array $extensions + * + * @return array + */ + final public static function injectExtensions(array $coreElements, array $extensions): array + { + $extensions += $coreElements; + + uksort($extensions, function ($a, $b) { + if ($a < 1000 && $b >= 1000) { + $b -= 1000; + if (abs($a - $b) < 10 && abs($a % 100 - $b % 100) < 10) { + return -1; + } + } elseif ($b < 1000 && $a >= 1000) { + $a -= 1000; + if (abs($a - $b) < 10 && abs($a % 100 - $b % 100) < 10) { + return 1; + } + } + + return $a < $b ? -1 : ($a > $b ? 1 : 0); + }); + + return $extensions; + } +} -- cgit v1.2.3