From 0f485f12570041d6e54f60b74b1f06d55bc28714 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 13 Nov 2020 11:03:28 +0100 Subject: [PATCH] Libraries: Add method `has($name, $version = null)` --- library/Icinga/Application/Libraries.php | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/library/Icinga/Application/Libraries.php b/library/Icinga/Application/Libraries.php index afbe7fe3e..83268d24b 100644 --- a/library/Icinga/Application/Libraries.php +++ b/library/Icinga/Application/Libraries.php @@ -36,4 +36,39 @@ class Libraries implements IteratorAggregate return $library; } + + /** + * Check if a library with the given name has been registered + * + * Passing a version constraint also verifies that the library's version matches. + * + * @param string $name + * @param string $version + * + * @return bool + */ + public function has($name, $version = null) + { + $libVersion = null; + foreach ($this->libraries as $library) { + if ($library->getName() === $name) { + $libVersion = $library->getVersion(); + break; + } + } + + if ($libVersion === null) { + 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]; + } + + return version_compare($libVersion, $version, $operator); + } }