From 99b620983ae875cb868f359585bd90eceaf8ee16 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 13 Nov 2020 11:04:30 +0100 Subject: [PATCH] Manager: Add method `has($name, $version = null)` --- .../Icinga/Application/Modules/Manager.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/library/Icinga/Application/Modules/Manager.php b/library/Icinga/Application/Modules/Manager.php index 1437f39a0..52f337975 100644 --- a/library/Icinga/Application/Modules/Manager.php +++ b/library/Icinga/Application/Modules/Manager.php @@ -430,6 +430,34 @@ class Manager return array_key_exists($name, $this->loadedModules); } + /** + * Check if a module with the given name is enabled + * + * Passing a version constraint also verifies that the module's version matches. + * + * @param string $name + * @param string $version + * + * @return bool + */ + public function has($name, $version = null) + { + if (! $this->hasEnabled($name)) { + return false; + } elseif ($version === null) { + return true; + } + + $operator = '='; + if (preg_match('/^([<>=]{1,2})\s*v?((?:[\d.]+)(?:\D+)?)$/', $version, $match)) { + $operator = $match[1]; + $version = $match[2]; + } + + $modVersion = ltrim($this->getModule($name)->getVersion(), 'v'); + return version_compare($modVersion, $version, $operator); + } + /** * Get the currently loaded modules *