Libraries: Add method `get($name)`

This commit is contained in:
Johannes Meyer 2020-11-13 17:43:17 +01:00
parent 01a08d60e1
commit 40c97d6a3e
1 changed files with 19 additions and 10 deletions

View File

@ -49,15 +49,8 @@ class Libraries implements IteratorAggregate
*/ */
public function has($name, $version = null) public function has($name, $version = null)
{ {
$libVersion = null; $library = $this->get($name);
foreach ($this->libraries as $library) { if ($library === null) {
if ($library->getName() === $name) {
$libVersion = $library->getVersion();
break;
}
}
if ($libVersion === null) {
return false; return false;
} elseif ($version === null) { } elseif ($version === null) {
return true; return true;
@ -69,6 +62,22 @@ class Libraries implements IteratorAggregate
$version = $match[2]; $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;
}
}
} }
} }