2014-09-29 12:25:29 +02:00
|
|
|
<?php
|
2015-02-03 16:27:59 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | http://www.gnu.org/licenses/gpl-2.0.txt */
|
2014-09-29 12:25:29 +02:00
|
|
|
|
2014-11-10 16:31:40 +01:00
|
|
|
namespace Icinga\Module\Setup;
|
2014-09-29 12:25:29 +02:00
|
|
|
|
|
|
|
use ArrayIterator;
|
|
|
|
use IteratorAggregate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Container to store and handle requirements
|
2015-01-22 12:47:18 +01:00
|
|
|
*
|
|
|
|
* TODO: Requirements should be registered as objects with a specific purpose (PhpModRequirement, PhpIniRequirement, ..)
|
|
|
|
* so that it's not necessary to define unique identifiers which may differ between different modules.
|
2014-09-29 12:25:29 +02:00
|
|
|
*/
|
|
|
|
class Requirements implements IteratorAggregate
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Identifier representing the state OK
|
|
|
|
*/
|
|
|
|
const STATE_OK = 2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Identifier representing the state OPTIONAL
|
|
|
|
*/
|
|
|
|
const STATE_OPTIONAL = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Identifier representing the state MANDATORY
|
|
|
|
*/
|
|
|
|
const STATE_MANDATORY = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The registered requirements
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $requirements = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a requirement
|
|
|
|
*
|
|
|
|
* @param object $requirement The requirement to add
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2015-01-22 12:47:18 +01:00
|
|
|
public function add($name, $requirement)
|
2014-09-29 12:25:29 +02:00
|
|
|
{
|
2015-01-22 12:47:18 +01:00
|
|
|
$this->requirements[$name] = array_key_exists($name, $this->requirements)
|
|
|
|
? $this->combine($this->requirements[$name], $requirement)
|
|
|
|
: $requirement;
|
2014-09-29 12:25:29 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-01-22 12:47:18 +01:00
|
|
|
/**
|
|
|
|
* Combine the two given requirements
|
|
|
|
*
|
|
|
|
* Returns the most important requirement with the description from the other one being added.
|
|
|
|
*
|
|
|
|
* @param object $oldRequirement
|
|
|
|
* @param object $newRequirement
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
protected function combine($oldRequirement, $newRequirement)
|
|
|
|
{
|
|
|
|
if ($newRequirement->state === static::STATE_MANDATORY && $oldRequirement->state === static::STATE_OPTIONAL) {
|
|
|
|
$tempRequirement = $oldRequirement;
|
|
|
|
$oldRequirement = $newRequirement;
|
|
|
|
$newRequirement = $tempRequirement;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! is_array($oldRequirement->description)) {
|
|
|
|
$oldRequirement->description = array($oldRequirement->description);
|
|
|
|
}
|
|
|
|
|
|
|
|
$oldRequirement->description[] = $newRequirement->description;
|
|
|
|
return $oldRequirement;
|
|
|
|
}
|
|
|
|
|
2014-09-29 12:25:29 +02:00
|
|
|
/**
|
|
|
|
* Return all registered requirements
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAll()
|
|
|
|
{
|
|
|
|
return $this->requirements;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an iterator of all registered requirements
|
|
|
|
*
|
|
|
|
* @return ArrayIterator
|
|
|
|
*/
|
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
return new ArrayIterator($this->getAll());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register an optional requirement
|
|
|
|
*
|
2015-01-22 12:47:18 +01:00
|
|
|
* @param string $name
|
2014-09-29 12:25:29 +02:00
|
|
|
* @param string $title
|
|
|
|
* @param string $description
|
|
|
|
* @param bool $state
|
|
|
|
* @param string $message
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2015-01-22 12:47:18 +01:00
|
|
|
public function addOptional($name, $title, $description, $state, $message)
|
2014-09-29 12:25:29 +02:00
|
|
|
{
|
2015-01-22 12:47:18 +01:00
|
|
|
$this->add(
|
|
|
|
$name,
|
|
|
|
(object) array(
|
|
|
|
'title' => $title,
|
|
|
|
'message' => $message,
|
|
|
|
'description' => $description,
|
|
|
|
'state' => (bool) $state ? static::STATE_OK : static::STATE_OPTIONAL
|
|
|
|
)
|
|
|
|
);
|
2014-09-29 12:25:29 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a mandatory requirement
|
|
|
|
*
|
2015-01-22 12:47:18 +01:00
|
|
|
* @param string $name
|
2014-09-29 12:25:29 +02:00
|
|
|
* @param string $title
|
|
|
|
* @param string $description
|
|
|
|
* @param bool $state
|
|
|
|
* @param string $message
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2015-01-22 12:47:18 +01:00
|
|
|
public function addMandatory($name, $title, $description, $state, $message)
|
2014-09-29 12:25:29 +02:00
|
|
|
{
|
2015-01-22 12:47:18 +01:00
|
|
|
$this->add(
|
|
|
|
$name,
|
|
|
|
(object) array(
|
|
|
|
'title' => $title,
|
|
|
|
'message' => $message,
|
|
|
|
'description' => $description,
|
|
|
|
'state' => (bool) $state ? static::STATE_OK : static::STATE_MANDATORY
|
|
|
|
)
|
|
|
|
);
|
2014-09-29 12:25:29 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the given requirements
|
|
|
|
*
|
|
|
|
* @param Requirements $requirements The requirements to register
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function merge(Requirements $requirements)
|
|
|
|
{
|
2015-01-22 12:47:18 +01:00
|
|
|
foreach ($requirements->getAll() as $name => $requirement) {
|
|
|
|
$this->add($name, $requirement);
|
2014-09-29 12:25:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make all registered requirements being optional
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function allOptional()
|
|
|
|
{
|
|
|
|
foreach ($this->getAll() as $requirement) {
|
|
|
|
if ($requirement->state === static::STATE_MANDATORY) {
|
|
|
|
$requirement->state = static::STATE_OPTIONAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether all mandatory requirements are fulfilled
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function fulfilled()
|
|
|
|
{
|
|
|
|
foreach ($this->getAll() as $requirement) {
|
|
|
|
if ($requirement->state === static::STATE_MANDATORY) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|