ConfigObject: Extend ArrayDatasource

This makes it possible to use a ini file as repository!!!1
One thing is missing: Section names are currently ignored and should be
mapped to a virtual column.

refs #8826
This commit is contained in:
Johannes Meyer 2015-05-05 15:21:34 +02:00
parent de68d78938
commit 5cc7f26728
2 changed files with 23 additions and 29 deletions

View File

@ -9,13 +9,15 @@ use LogicException;
use UnexpectedValueException; use UnexpectedValueException;
use Icinga\Util\File; use Icinga\Util\File;
use Icinga\Data\ConfigObject; use Icinga\Data\ConfigObject;
use Icinga\Data\Selectable;
use Icinga\Data\SimpleQuery;
use Icinga\File\Ini\IniWriter; use Icinga\File\Ini\IniWriter;
use Icinga\Exception\NotReadableError; use Icinga\Exception\NotReadableError;
/** /**
* Container for INI like configuration and global registry of application and module related configuration. * Container for INI like configuration and global registry of application and module related configuration.
*/ */
class Config implements Countable, Iterator class Config implements Countable, Iterator, Selectable
{ {
/** /**
* Configuration directory where ALL (application and module) configuration is located * Configuration directory where ALL (application and module) configuration is located
@ -85,6 +87,16 @@ class Config implements Countable, Iterator
return $this; return $this;
} }
/**
* Provide a query for the internal config object
*
* @return SimpleQuery
*/
public function select()
{
return $this->config->select();
}
/** /**
* Return the count of available sections * Return the count of available sections
* *
@ -92,7 +104,7 @@ class Config implements Countable, Iterator
*/ */
public function count() public function count()
{ {
return $this->config->count(); return $this->select()->count();
} }
/** /**

View File

@ -4,22 +4,15 @@
namespace Icinga\Data; namespace Icinga\Data;
use Iterator; use Iterator;
use Countable;
use ArrayAccess; use ArrayAccess;
use LogicException; use Icinga\Data\DataArray\ArrayDatasource;
use Icinga\Exception\ProgrammingError;
/** /**
* Container for configuration values * Container for configuration values
*/ */
class ConfigObject implements Countable, Iterator, ArrayAccess class ConfigObject extends ArrayDatasource implements Iterator, ArrayAccess
{ {
/**
* This config's data
*
* @var array
*/
protected $data;
/** /**
* Create a new config * Create a new config
* *
@ -27,15 +20,14 @@ class ConfigObject implements Countable, Iterator, ArrayAccess
*/ */
public function __construct(array $data = array()) public function __construct(array $data = array())
{ {
$this->data = array(); // Convert all embedded arrays to ConfigObjects as well
foreach ($data as & $value) {
foreach ($data as $key => $value) {
if (is_array($value)) { if (is_array($value)) {
$this->data[$key] = new static($value); $value = new static($value);
} else {
$this->data[$key] = $value;
} }
} }
parent::__construct($data);
} }
/** /**
@ -55,16 +47,6 @@ class ConfigObject implements Countable, Iterator, ArrayAccess
$this->data = $array; $this->data = $array;
} }
/**
* Return the count of available sections and properties
*
* @return int
*/
public function count()
{
return count($this->data);
}
/** /**
* Reset the current position of $this->data * Reset the current position of $this->data
* *
@ -197,7 +179,7 @@ class ConfigObject implements Countable, Iterator, ArrayAccess
public function offsetSet($key, $value) public function offsetSet($key, $value)
{ {
if ($key === null) { if ($key === null) {
throw new LogicException('Appending values without an explicit key is not supported'); throw new ProgrammingError('Appending values without an explicit key is not supported');
} }
$this->$key = $value; $this->$key = $value;