From d0a8dd8973c385d804e7f6a5368702b3923bb446 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 26 Feb 2015 10:52:39 +0100 Subject: [PATCH] Requirements: Do not implement ArrayIterator but RecursiveIterator refs #8508 --- .../scripts/form/setup-requirements.phtml | 4 +- modules/setup/library/Setup/Requirements.php | 88 +++++++++++++++---- 2 files changed, 75 insertions(+), 17 deletions(-) diff --git a/modules/setup/application/views/scripts/form/setup-requirements.phtml b/modules/setup/application/views/scripts/form/setup-requirements.phtml index 09ca98d7f..9918df008 100644 --- a/modules/setup/application/views/scripts/form/setup-requirements.phtml +++ b/modules/setup/application/views/scripts/form/setup-requirements.phtml @@ -1,13 +1,15 @@ getRequirements(); +$iterator = new RecursiveIteratorIterator($requirements); ?> - +

getTitle(); ?>

diff --git a/modules/setup/library/Setup/Requirements.php b/modules/setup/library/Setup/Requirements.php index aba96c4bc..69892f236 100644 --- a/modules/setup/library/Setup/Requirements.php +++ b/modules/setup/library/Setup/Requirements.php @@ -3,14 +3,13 @@ namespace Icinga\Module\Setup; -use ArrayIterator; use LogicException; -use IteratorAggregate; +use RecursiveIterator; /** * Container to store and handle requirements */ -class Requirements implements IteratorAggregate +class Requirements implements RecursiveIterator { /** * Mode AND (all requirements must met) @@ -94,7 +93,7 @@ class Requirements implements IteratorAggregate public function add(Requirement $requirement) { $merged = false; - foreach ($this as $knownRequirement) { + foreach ($this->requirements as $knownRequirement) { if ($knownRequirement instanceof Requirement && $requirement->equals($knownRequirement)) { if ($this->getMode() === static::MODE_AND && !$requirement->isOptional()) { $knownRequirement->setOptional(false); @@ -142,16 +141,6 @@ class Requirements implements IteratorAggregate return $this->containsMandatoryRequirements || $this->getMode() === static::MODE_OR; } - /** - * Return an iterator of all registered requirements - * - * @return ArrayIterator - */ - public function getIterator() - { - return new ArrayIterator($this->getAll()); - } - /** * Register the given requirements * @@ -162,7 +151,7 @@ class Requirements implements IteratorAggregate public function merge(Requirements $requirements) { if ($this->getMode() === static::MODE_OR && $requirements->getMode() === static::MODE_OR) { - foreach ($requirements as $requirement) { + foreach ($requirements->getAll() as $requirement) { if ($requirement instanceof static) { $this->merge($requirement); } else { @@ -188,7 +177,7 @@ class Requirements implements IteratorAggregate public function fulfilled() { $state = false; - foreach ($this as $requirement) { + foreach ($this->requirements as $requirement) { if ($requirement instanceof static) { if ($requirement->fulfilled()) { if ($this->getMode() === static::MODE_OR) { @@ -218,4 +207,71 @@ class Requirements implements IteratorAggregate return $state; } + + /** + * Return whether the current element represents a nested set of requirements + * + * @return bool + */ + public function hasChildren() + { + $current = $this->current(); + return $current instanceof static; + } + + /** + * Return a iterator for the current nested set of requirements + * + * @return RecursiveIterator + */ + public function getChildren() + { + return $this->current(); + } + + /** + * Rewind the iterator to its first element + */ + public function rewind() + { + reset($this->requirements); + } + + /** + * Return whether the current iterator position is valid + * + * @return bool + */ + public function valid() + { + return $this->key() !== null; + } + + /** + * Return the current element in the iteration + * + * @return Requirement|Requirements + */ + public function current() + { + return current($this->requirements); + } + + /** + * Return the position of the current element in the iteration + * + * @return int + */ + public function key() + { + return key($this->requirements); + } + + /** + * Advance the iterator to the next element + */ + public function next() + { + next($this->requirements); + } }