]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/iterator/registry/class_RegistryIterator.php
Continued:
[core.git] / framework / main / classes / iterator / registry / class_RegistryIterator.php
index 522ad93440943dedebf33b95600791c8d923ca9c..66538f52ce3a70a03834ddcb96dcb698da9590c6 100644 (file)
@@ -3,11 +3,15 @@
 namespace CoreFramework\Iterator\Registry;
 
 // Import framework stuff
+use CoreFramework\Generic\FrameworkInterface;
+use CoreFramework\Generic\NullPointerException;
 use CoreFramework\Iterator\BaseIterator;
 use CoreFramework\Registry\Register;
+use CoreFramework\Registry\Registerable;
+use CoreFramework\Registry\Sub\SubRegistry;
 
 // Import SPL stuff
-use \Iterator;
+use \LogicException;
 
 /**
  * A Registry iterator
@@ -31,7 +35,22 @@ use \Iterator;
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-class RegistryIterator extends BaseIterator implements Iterator {
+class RegistryIterator extends BaseIterator implements IteratableRegistry {
+       /**
+        * All found registry keys
+        */
+       private $registryKeys = array();
+
+       /**
+        * Current array key
+        */
+       private $key = NULL;
+
+       /**
+        * Valid status (default: not valid)
+        */
+       private $valid = FALSE;
+
        /**
         * Protected constructor
         *
@@ -59,6 +78,121 @@ class RegistryIterator extends BaseIterator implements Iterator {
                return $iteratorInstance;
        }
 
+       /**
+        * Initializes this iterator by scanning over the registry for all keys.
+        *
+        * @param       $callbackInstance       An instance of a FrameworkInterface class to call back (optional)
+        * @param       $criteriaKey            Criteria key (optional)
+        * @param       $criteriaMethod         Method to call back (optional)
+        * @return      void
+        * @throws      LogicException  If a registry entry does not implement Registerable
+        * @throws      NullPointerException    If criteriaKey or criteriaMethod is not set but a call-back instance is set
+        */
+       public function initIterator (FrameworkInterface $callbackInstance = NULL, $criteriaKey = NULL, $criteriaMethod = NULL) {
+               // Is the call-back instance set?
+               if ($callbackInstance instanceof FrameworkInterface) {
+                       // Then also criteria key and method name must be given
+                       if (is_null($criteriaKey)) {
+                               // Throw NPE
+                               throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
+                       } elseif (is_null($criteriaMethod)) {
+                               // Throw NPE
+                               throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
+                       }
+
+                       // Set all
+                       $iteratorInstance->setCallbackInstance($callbackInstance);
+                       $iteratorInstance->setCriteriaKey($criteriaKey);
+               } // END - if
+
+               // Get generic registry entries from it
+               $entries = $this->getRegistryInstance()->getGenericRegistry();
+
+               // Init registry keys array
+               $this->registryKeys['generic'] = array();
+
+               // Anything in there?
+               if (count($entries) > 0) {
+                       // Debugging:
+                       /* DEBUG-DIE: */ die(sprintf('[%s:%d]: entries=%s', __METHOD__, __LINE__, print_r($entries, TRUE)));
+               } // END - if
+
+               // Get instance registry entries from it
+               $entries = $this->getRegistryInstance()->getInstanceRegistry();
+
+               // Init registry keys array
+               $this->registryKeys['instance'] = array();
+
+               // Anything in there?
+               if (count($entries) > 0) {
+                       // Then run over all
+                       foreach ($entries as $key => $entry) {
+                               // Debug message
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s,entry[]=%s', $key, gettype($entry)));
+
+                               // Is it 'socket_registry' ?
+                               if ($key == 'socket_registry') {
+                                       // Skip this entry
+                                       continue;
+                               } // END - if
+
+                               // Is it an instance of a sub-registry?
+                               if ($entry instanceof SubRegistry) {
+                                       // Get iterator from this instance
+                                       $iteratorInstance = $entry->getIterator();
+
+                                       // Debugging:
+                                       //* DEBUG-DIE: */ die(sprintf('[%s:%d]: key=%s,iteratorInstance=%s', __METHOD__, __LINE__, $key, print_r($iteratorInstance, TRUE)));
+
+                                       // Get all keys
+                                       $keys = $iteratorInstance->getRegistryKeys();
+
+                                       // Should be there
+                                       if (!isset($keys['instance'])) {
+                                               // Should not happen
+                                               throw new LogicException(sprintf('key=%s,keys[instance] is not set.', $key));
+                                       } // END - if
+
+                                       // Add all sub-registry keys to this registry keys array
+                                       $this->registryKeys['instance'][$key] = $keys['instance'];
+
+                                       // Debug message
+                                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s,keys=%s', $key, print_r($this->registryKeys['instance'][$key], TRUE)));
+
+                                       // Skip below code
+                                       continue;
+                               } elseif (!($entry instanceof Registerable)) {
+                                       // Not registerable?!
+                                       throw new LogicException(sprintf('entry[]=%s does not implement Registerable.', gettype($entry)));
+                               } elseif (is_null($this->key)) {
+                                       // Debug message
+                                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: Setting key=%s ...', $key));
+
+                                       // Init key/valid
+                                       $this->key = $key;
+                                       $this->valid = TRUE;
+                               }
+
+                               // Debugging:
+                               //* DEBUG-DIE: */ die(sprintf('[%s:%d]: key=%s,entry=%s', __METHOD__, __LINE__, $key, print_r($entry, TRUE)));
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s,entry[]=%s - Adding ...', $key, gettype($entry)));
+
+                               // Add key to array
+                               $this->registryKeys['instance'][$key] = array();
+                       } // END - foreach
+               } // END - if
+       }
+
+       /**
+        * Getter for all registry keys (array)
+        *
+        * @return      $registryKeys   Registry keys
+        */
+       public final function getRegistryKeys () {
+               // Return it
+               return $this->registryKeys;
+       }
+
        /**
         * Getter for current value from group or generic
         *
@@ -80,13 +214,8 @@ class RegistryIterator extends BaseIterator implements Iterator {
         * @return      $key    Current key in iteration
         */
        public function key () {
-               // Default is null
-               $key = null;
-
-               $this->partialStub('Please implement this method.');
-
                // Return it
-               return $key;
+               return $this->key;
        }
 
        /**
@@ -104,16 +233,18 @@ class RegistryIterator extends BaseIterator implements Iterator {
         * @return      void
         */
        public function rewind () {
-               $this->partialStub('Please implement this method.');
+               // Debugging:
+               /* DEBUG-DIE: */ die(sprintf('[%s:%d]: this->key(%d)[%s]=%s,this->valid=%d,this->registryKeys=%s', __METHOD__, __LINE__, strlen($this->key()), gettype($this->key()), $this->key(), intval($this->valid()), print_r($this->registryKeys, TRUE)));
        }
 
        /**
         * Checks wether the current entry is valid (not at the end of the list)
         *
-        * @return      void
+        * @return      $valid  Whether the current key is still valid
         */
        public function valid () {
-               $this->partialStub('Please implement this method.');
+               // Return flag
+               return $this->valid;
        }
 
 }