[icinga-checkins] icinga.org: icingaweb2/feature/conditional-requirements-8508: Requirements : Do not implement ArrayIterator but RecursiveIterator

git at icinga.org git at icinga.org
Thu Feb 26 10:53:52 CET 2015


Module: icingaweb2
Branch: feature/conditional-requirements-8508
Commit: d0a8dd8973c385d804e7f6a5368702b3923bb446
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=d0a8dd8973c385d804e7f6a5368702b3923bb446

Author: Johannes Meyer <johannes.meyer at netways.de>
Date:   Thu Feb 26 10:52:39 2015 +0100

Requirements: Do not implement ArrayIterator but RecursiveIterator

refs #8508

---

 .../views/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 09ca98d..9918df0 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 @@
 <?php
 
+use \RecursiveIteratorIterator;
 use Icinga\Web\Wizard;
 
 $requirements = $form->getRequirements();
+$iterator = new RecursiveIteratorIterator($requirements);
 
 ?>
 <table class="requirements">
   <tbody>
-<?php foreach ($requirements as $requirement): ?>
+<?php foreach ($iterator as $requirement): ?>
     <tr>
       <td><h2><?= $requirement->getTitle(); ?></h2></td>
       <td style="width: 50%">
diff --git a/modules/setup/library/Setup/Requirements.php b/modules/setup/library/Setup/Requirements.php
index aba96c4..69892f2 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);
@@ -143,16 +142,6 @@ class Requirements implements IteratorAggregate
     }
 
     /**
-     * Return an iterator of all registered requirements
-     *
-     * @return  ArrayIterator
-     */
-    public function getIterator()
-    {
-        return new ArrayIterator($this->getAll());
-    }
-
-    /**
      * Register the given requirements
      *
      * @param   Requirements    $requirements   The requirements to register
@@ -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);
+    }
 }



More information about the icinga-checkins mailing list