CustomVariable: added new playground and prototypes

This commit is contained in:
Thomas Gelf 2015-06-11 08:15:51 +02:00
parent db29f4b6c6
commit 8669d4be38
5 changed files with 299 additions and 0 deletions

View File

@ -0,0 +1,119 @@
<?php
namespace Icinga\Module\Director\CustomVariable;
use Icinga\Exception\ProgrammingError;
abstract class CustomVariable
{
protected $key;
protected $value;
protected $storedValue;
protected $type;
protected $modified = false;
protected function __construct($key, $value = null)
{
$this->key = $key;
$this->setValue($value);
}
public function is($type)
{
return $this->getType() === $type;
}
public function getType()
{
if ($this->type === null) {
$parts = explode('\\', get_class($this));
$class = end($parts);
// strlen('CustomVariable') === 9
$this->type = substr(end($parts), 9);
}
return $this->type;
}
public function setValue($value)
{
if ($value instanceof CustomVariable) {
if (! $this->equals($value)) {
$this->reallySet($value);
}
} elseif ($value !== $this->value) {
$this->reallySet($value);
}
return $this;
}
protected function reallySetValue($value)
{
$this->modified = true;
$this->value = $value;
}
public function hasBeenModified()
{
return $this->modified;
}
public function setUnmodified()
{
$this->modified = false;
$this->storedValue = clone($this->value);
return $this;
}
abstract public function equals(CustomVariable $var);
abstract public function toConfigString();
public function differsFrom(CustomVariable $var)
{
return ! $this->equals($var);
}
public static function create($key, $value)
{
if (is_string($value)) {
return new CustomVariableString($key, $value);
} elseif (is_array($value)) {
foreach (array_keys($value) as & $key) {
if (! is_int($key) || ctype_digit($key)) {
return new CustomVariableDictionary($key, $value);
}
}
return new CustomVariableArray($key, array_values($value));
} elseif (is_object($value)) {
// TODO: check for specific class/stdClass/interface?
return new CustomVariableDictionary($key, $value);
} else {
throw new ProgrammingError();
}
}
public function __toString()
{
try {
return $this->toConfigString();
} catch (Exception $e) {
trigger_error($e);
$previousHandler = set_exception_handler(function () {});
restore_error_handler();
call_user_func($previousHandler, $e);
die();
}
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Icinga\Module\Director\CustomVariable;
class CustomVariableArray extends CustomVariable
{
public function equals(CustomVariable $var)
{
if (! $var instanceof CustomVariableArray) {
return false;
}
return $var->getValue() === $this->getValue();
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace Icinga\Module\Director\CustomVariable;
class CustomVariableDictionary extends CustomVariable implements Countable
{
public function equals(CustomVariable $var)
{
if (! $var instanceof CustomVariableDictionary) {
return false;
}
$myKeys = $this->listKeys();
$foreignKeys = $var->listKeys();
if ($myKeys !== $foreignKeys) {
return false;
}
foreach ($this->value as $key => $value) {
if ($this->$key->differsFrom($value)->$key) {
return false;
}
}
return true;
}
public function listKeys()
{
$keys = array_keys($this->value);
ksort($keys);
return $keys;
}
public function count()
{
return count($this->value);
}
public function __clone()
{
foreach ($this->value as $key => $value) {
$this->value->$key = clone($value);
}
}
public function __get($key)
{
// ...
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Icinga\Module\Director\CustomVariable;
use Icinga\Module\Director\IcingaConfigHelper as c;
class CustomVariableString extends CustomVariable
{
public function equals(CustomVariable $var)
{
return $var->getValue() === $this->getValue();
}
public function toConfigString()
{
return c::renderKeyValue(
c::escapeIfReserved($this->getKey()),
c::renderString($this->getValue()
);
}
}

View File

@ -0,0 +1,93 @@
<?php
namespace Icinga\Module\Director\CustomVariable;
class CustomVariables
{
protected $storedVars = array();
protected $vars = array();
protected $modified = false;
/**
* Generic setter
*
* @param string $property
* @param mixed $value
*
* @return array
*/
public function set($key, $value)
{
$key = (string) $key;
if ($value === null) {
unset($this->$key);
}
if ($value === $this->get($key)) {
return $this;
}
$this->vars[$key] = $value;
$this->modified = true;
return $this;
}
public function hasBeenModified()
{
return $this->modifiec
}
public function setUnmodified()
{
$this->modified = false;
$this->storedVars = $this->vars;
return $this;
}
public function __get($key)
{
return $this->get($key);
}
/**
* Magic setter
*
* @param string $key Key
* @param mixed $val Value
*
* @return void
*/
public function __set($key, $val)
{
$this->set($key, $val);
}
/**
* Magic isset check
*
* @return boolean
*/
public function __isset($key)
{
return array_key_exists($key, $this->properties);
}
/**
* Magic unsetter
*
* @return void
*/
public function __unset($key)
{
if (! array_key_exists($key, $this->properties)) {
throw new Exception('Trying to unset invalid key');
}
$this->properties[$key] = $this->defaultProperties[$key];
}
}