diff --git a/library/Icinga/Application/Config.php b/library/Icinga/Application/Config.php index ccadebace..568d5d701 100644 --- a/library/Icinga/Application/Config.php +++ b/library/Icinga/Application/Config.php @@ -4,10 +4,9 @@ namespace Icinga\Application; +use Iterator; use Countable; use ArrayAccess; -use ArrayIterator; -use IteratorAggregate; use LogicException; use UnexpectedValueException; use Icinga\Exception\NotReadableError; @@ -15,7 +14,7 @@ use Icinga\Exception\NotReadableError; /** * Container for configuration values and global registry of application and module related configuration. */ -class Config implements Countable, ArrayAccess, IteratorAggregate +class Config implements Countable, Iterator, ArrayAccess { /** * Configuration directory where ALL (application and module) configuration is located @@ -121,13 +120,53 @@ class Config implements Countable, ArrayAccess, IteratorAggregate } /** - * Return a iterator for this config's data + * Reset the current position of $this->data * - * @return ArrayIterator + * @return mixed */ - public function getIterator() + public function rewind() { - return new ArrayIterator($this->data); + return reset($this->data); + } + + /** + * Return the section's or property's value of the current iteration + * + * @return mixed + */ + public function current() + { + return current($this->data); + } + + /** + * Return whether the position of the current iteration is valid + * + * @return bool + */ + public function valid() + { + return key($this->data) !== null; + } + + /** + * Return the section's or property's name of the current iteration + * + * @return mixed + */ + public function key() + { + return key($this->data); + } + + /** + * Advance the position of the current iteration and return the new section's or property's value + * + * @return mixed + */ + public function next() + { + return next($this->data); } /** diff --git a/test/php/library/Icinga/Application/ConfigTest.php b/test/php/library/Icinga/Application/ConfigTest.php index 77627dcdb..e74eb06a5 100644 --- a/test/php/library/Icinga/Application/ConfigTest.php +++ b/test/php/library/Icinga/Application/ConfigTest.php @@ -85,7 +85,26 @@ class ConfigTest extends BaseTestCase $config = new Config(array('a' => 'b', 'c' => array('d' => 'e'))); $this->assertInstanceOf('Countable', $config, 'Config objects do not implement interface `Countable\''); - $this->assertEquals(2, $config->count(), 'Config objects do not count properties and sections correctly'); + $this->assertEquals(2, count($config), 'Config objects do not count properties and sections correctly'); + } + + public function testWhetherConfigObjectsAreTraversable() + { + $config = new Config(array('a' => 'b', 'c' => 'd')); + $config->e = 'f'; + + $this->assertInstanceOf('Iterator', $config, 'Config objects do not implement interface `Iterator\''); + + $actual = array(); + foreach ($config as $key => $value) { + $actual[$key] = $value; + } + + $this->assertEquals( + array('a' => 'b', 'c' => 'd', 'e' => 'f'), + $actual, + 'Config objects do not iterate properly in the order their values were inserted' + ); } public function testWhetherOneCanCheckWhetherConfigObjectsHaveACertainPropertyOrSection()