* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ 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 * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Creates an instance of this class * * @param $registryInstance An instance of a Register class * @return $iteratorInstance An instance of a Iterator class */ public final static function createRegistryIterator (Register $registryInstance) { // Get new instance $iteratorInstance = new RegistryIterator(); // Set registry here $iteratorInstance->setRegistryInstance($registryInstance); // Return the prepared instance 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 * * @return $current Current value in iteration */ public function current () { // Default is null $current = null; $this->partialStub('Please implement this method.'); // Return it return $current; } /** * Getter for key from group or generic * * @return $key Current key in iteration */ public function key () { // Return it return $this->key; } /** * Advances to the next entry * * @return void */ public function next () { $this->partialStub('Please implement this method.'); } /** * Rewinds to the beginning of the iteration * * @return void */ public function rewind () { // 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 $valid Whether the current key is still valid */ public function valid () { // Return flag return $this->valid; } }