From 5cc7f267282932dca38e279dae97227d24cc9321 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 5 May 2015 15:21:34 +0200 Subject: [PATCH] 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 --- library/Icinga/Application/Config.php | 16 ++++++++++-- library/Icinga/Data/ConfigObject.php | 36 +++++++-------------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/library/Icinga/Application/Config.php b/library/Icinga/Application/Config.php index 8a1746b69..6bad014a6 100644 --- a/library/Icinga/Application/Config.php +++ b/library/Icinga/Application/Config.php @@ -9,13 +9,15 @@ use LogicException; use UnexpectedValueException; use Icinga\Util\File; use Icinga\Data\ConfigObject; +use Icinga\Data\Selectable; +use Icinga\Data\SimpleQuery; use Icinga\File\Ini\IniWriter; use Icinga\Exception\NotReadableError; /** * 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 @@ -85,6 +87,16 @@ class Config implements Countable, Iterator 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 * @@ -92,7 +104,7 @@ class Config implements Countable, Iterator */ public function count() { - return $this->config->count(); + return $this->select()->count(); } /** diff --git a/library/Icinga/Data/ConfigObject.php b/library/Icinga/Data/ConfigObject.php index 641d72846..de00e5b5e 100644 --- a/library/Icinga/Data/ConfigObject.php +++ b/library/Icinga/Data/ConfigObject.php @@ -4,22 +4,15 @@ namespace Icinga\Data; use Iterator; -use Countable; use ArrayAccess; -use LogicException; +use Icinga\Data\DataArray\ArrayDatasource; +use Icinga\Exception\ProgrammingError; /** * 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 * @@ -27,15 +20,14 @@ class ConfigObject implements Countable, Iterator, ArrayAccess */ public function __construct(array $data = array()) { - $this->data = array(); - - foreach ($data as $key => $value) { + // Convert all embedded arrays to ConfigObjects as well + foreach ($data as & $value) { if (is_array($value)) { - $this->data[$key] = new static($value); - } else { - $this->data[$key] = $value; + $value = new static($value); } } + + parent::__construct($data); } /** @@ -55,16 +47,6 @@ class ConfigObject implements Countable, Iterator, ArrayAccess $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 * @@ -197,7 +179,7 @@ class ConfigObject implements Countable, Iterator, ArrayAccess public function offsetSet($key, $value) { 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;