From 40c97d6a3ee0f50f9d63bd9cfe112b5cd0b638aa Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 13 Nov 2020 17:43:17 +0100 Subject: [PATCH] Libraries: Add method `get($name)` --- library/Icinga/Application/Libraries.php | 29 ++++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/library/Icinga/Application/Libraries.php b/library/Icinga/Application/Libraries.php index 83268d24b..983381d2f 100644 --- a/library/Icinga/Application/Libraries.php +++ b/library/Icinga/Application/Libraries.php @@ -49,15 +49,8 @@ class Libraries implements IteratorAggregate */ public function has($name, $version = null) { - $libVersion = null; - foreach ($this->libraries as $library) { - if ($library->getName() === $name) { - $libVersion = $library->getVersion(); - break; - } - } - - if ($libVersion === null) { + $library = $this->get($name); + if ($library === null) { return false; } elseif ($version === null) { return true; @@ -69,6 +62,22 @@ class Libraries implements IteratorAggregate $version = $match[2]; } - return version_compare($libVersion, $version, $operator); + return version_compare($library->getVersion(), $version, $operator); + } + + /** + * Get a library by name + * + * @param string $name + * + * @return Library|null + */ + public function get($name) + { + foreach ($this->libraries as $library) { + if ($library->getName() === $name) { + return $library; + } + } } }