-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DependencyChecker: new implemenation
- Loading branch information
1 parent
05dfef5
commit babf42c
Showing
9 changed files
with
447 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
namespace Icinga\Module\Director\Application; | ||
|
||
class Dependency | ||
{ | ||
/** @var string */ | ||
protected $name; | ||
|
||
/** @var string|null */ | ||
protected $installedVersion; | ||
|
||
/** @var bool|null */ | ||
protected $enabled; | ||
|
||
/** @var string */ | ||
protected $operator; | ||
|
||
/** @var string */ | ||
protected $requiredVersion; | ||
|
||
/** @var string */ | ||
protected $requirement; | ||
|
||
/** | ||
* Dependency constructor. | ||
* @param string $name Usually a module name | ||
* @param string $requirement e.g. >=1.7.0 | ||
* @param string $installedVersion | ||
* @param bool $enabled | ||
*/ | ||
public function __construct($name, $requirement, $installedVersion = null, $enabled = null) | ||
{ | ||
$this->name = $name; | ||
$this->setRequirement($requirement); | ||
if ($installedVersion !== null) { | ||
$this->setInstalledVersion($installedVersion); | ||
} | ||
if ($enabled !== null) { | ||
$this->setEnabled($enabled); | ||
} | ||
} | ||
|
||
public function setRequirement($requirement) | ||
{ | ||
if (preg_match('/^([<>=]+)\s*v?(\d+\.\d+\.\d+)$/', $requirement, $match)) { | ||
$this->operator = $match[1]; | ||
$this->requiredVersion = $match[2]; | ||
$this->requirement = $requirement; | ||
} else { | ||
throw new \InvalidArgumentException("'$requirement' is not a valid version constraint"); | ||
} | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isInstalled() | ||
{ | ||
return $this->installedVersion !== null; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getInstalledVersion() | ||
{ | ||
return $this->installedVersion; | ||
} | ||
|
||
/** | ||
* @param string $version | ||
*/ | ||
public function setInstalledVersion($version) | ||
{ | ||
$this->installedVersion = ltrim($version, 'v'); // v0.6.0 VS 0.6.0 | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isEnabled() | ||
{ | ||
return $this->enabled === true; | ||
} | ||
|
||
/** | ||
* @param bool $enabled | ||
*/ | ||
public function setEnabled($enabled = true) | ||
{ | ||
$this->enabled = $enabled; | ||
} | ||
|
||
public function isSatisfied() | ||
{ | ||
if (! $this->isInstalled() || ! $this->isEnabled()) { | ||
return false; | ||
} | ||
|
||
return version_compare($this->installedVersion, $this->requiredVersion, $this->operator); | ||
} | ||
|
||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getRequirement() | ||
{ | ||
return $this->requirement; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Icinga\Module\Director\Application; | ||
|
||
use Icinga\Application\ApplicationBootstrap; | ||
use Icinga\Application\Modules\Module; | ||
use Icinga\Application\Version; | ||
|
||
class DependencyChecker | ||
{ | ||
/** @var ApplicationBootstrap */ | ||
protected $app; | ||
|
||
/** @var \Icinga\Application\Modules\Manager */ | ||
protected $modules; | ||
|
||
public function __construct(ApplicationBootstrap $app) | ||
{ | ||
$this->app = $app; | ||
$this->modules = $app->getModuleManager(); | ||
} | ||
|
||
/** | ||
* @param Module $module | ||
* @return Dependency[] | ||
*/ | ||
public function getDependencies(Module $module) | ||
{ | ||
$dependencies = []; | ||
$isV290 = version_compare(Version::VERSION, '2.9.0', '>='); | ||
foreach ($module->getDependencies() as $moduleName => $required) { | ||
if ($isV290 && in_array($moduleName, ['ipl', 'reactbundle'], true)) { | ||
continue; | ||
} | ||
$dependency = new Dependency($moduleName, $required); | ||
$dependency->setEnabled($this->modules->hasEnabled($moduleName)); | ||
if ($this->modules->hasInstalled($moduleName)) { | ||
$dependency->setInstalledVersion($this->modules->getModule($moduleName, false)->getVersion()); | ||
} | ||
$dependencies[] = $dependency; | ||
} | ||
if ($isV290) { | ||
$libs = $this->app->getLibraries(); | ||
foreach ($module->getRequiredLibraries() as $libraryName => $required) { | ||
$dependency = new Dependency($libraryName, $required); | ||
if ($libs->has($libraryName)) { | ||
$dependency->setInstalledVersion($libs->get($libraryName)->getVersion()); | ||
$dependency->setEnabled(); | ||
} | ||
$dependencies[] = $dependency; | ||
} | ||
} | ||
|
||
return $dependencies; | ||
} | ||
|
||
// if (version_compare(Version::VERSION, '2.9.0', 'ge')) { | ||
// } | ||
/** | ||
* @param Module $module | ||
* @return bool | ||
*/ | ||
public function satisfiesDependencies(Module $module) | ||
{ | ||
foreach ($this->getDependencies($module) as $dependency) { | ||
if (! $dependency->isSatisfied()) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.