Skip to content

Commit

Permalink
ConfigObject: Extend ArrayDatasource
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Johannes Meyer committed May 5, 2015
1 parent de68d78 commit 5cc7f26
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
16 changes: 14 additions & 2 deletions library/Icinga/Application/Config.php
Expand Up @@ -9,13 +9,15 @@
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
Expand Down Expand Up @@ -85,14 +87,24 @@ public function setConfigFile($filepath)
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 int
*/
public function count()
{
return $this->config->count();
return $this->select()->count();
}

/**
Expand Down
36 changes: 9 additions & 27 deletions library/Icinga/Data/ConfigObject.php
Expand Up @@ -4,38 +4,30 @@
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
*
* @param array $data The data to initialize the new config with
*/
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);
}

/**
Expand All @@ -55,16 +47,6 @@ public function __clone()
$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
*
Expand Down Expand Up @@ -197,7 +179,7 @@ public function offsetGet($key)
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;
Expand Down

0 comments on commit 5cc7f26

Please sign in to comment.