Manager: Add method `has($name, $version = null)`

This commit is contained in:
Johannes Meyer 2020-11-13 11:04:30 +01:00
parent 0f485f1257
commit 99b620983a
1 changed files with 28 additions and 0 deletions

View File

@ -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
*