3 namespace Org\Mxchange\CoreFramework\Iterator\Registry;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
7 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
8 use Org\Mxchange\CoreFramework\Iterator\BaseIterator;
9 use Org\Mxchange\CoreFramework\Registry\Register;
10 use Org\Mxchange\CoreFramework\Registry\Registerable;
11 use Org\Mxchange\CoreFramework\Registry\Sub\SubRegistry;
19 * @author Roland Haeder <webmaster@ship-simu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.ship-simu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class RegistryIterator extends BaseIterator implements IteratableRegistry {
40 * All found registry keys
42 private $registryKeys = [];
50 * Valid status (default: not valid)
52 private $valid = FALSE;
55 * Protected constructor
59 protected function __construct () {
60 // Call parent constructor
61 parent::__construct(__CLASS__);
65 * Creates an instance of this class
67 * @param $registryInstance An instance of a Register class
68 * @return $iteratorInstance An instance of a Iterator class
70 public final static function createRegistryIterator (Register $registryInstance) {
72 $iteratorInstance = new RegistryIterator();
75 $iteratorInstance->setRegistryInstance($registryInstance);
77 // Return the prepared instance
78 return $iteratorInstance;
82 * Initializes this iterator by scanning over the registry for all keys.
84 * @param $callbackInstance An instance of a FrameworkInterface class to call back (optional)
85 * @param $criteriaKey Criteria key (optional)
86 * @param $criteriaMethod Method to call back (optional)
88 * @throws LogicException If a registry entry does not implement Registerable
89 * @throws NullPointerException If criteriaKey or criteriaMethod is not set but a call-back instance is set
91 public function initIterator (FrameworkInterface $callbackInstance = NULL, $criteriaKey = NULL, $criteriaMethod = NULL) {
92 // Is the call-back instance set?
93 if ($callbackInstance instanceof FrameworkInterface) {
94 // Then also criteria key and method name must be given
95 if (is_null($criteriaKey)) {
97 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
98 } elseif (is_null($criteriaMethod)) {
100 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
104 $iteratorInstance->setCallbackInstance($callbackInstance);
105 $iteratorInstance->setCriteriaKey($criteriaKey);
108 // Get generic registry entries from it
109 $entries = $this->getRegistryInstance()->getGenericRegistry();
111 // Init registry keys array
112 $this->registryKeys['generic'] = [];
114 // Anything in there?
115 if (count($entries) > 0) {
117 /* DEBUG-DIE: */ die(sprintf('[%s:%d]: entries=%s', __METHOD__, __LINE__, print_r($entries, TRUE)));
120 // Get instance registry entries from it
121 $entries = $this->getRegistryInstance()->getInstanceRegistry();
123 // Init registry keys array
124 $this->registryKeys['instance'] = [];
126 // Anything in there?
127 if (count($entries) > 0) {
129 foreach ($entries as $key => $entry) {
131 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s,entry[]=%s', $key, gettype($entry)));
133 // Is it 'socket_registry' ?
134 if ($key == 'socket_registry') {
139 // Is it an instance of a sub-registry?
140 if ($entry instanceof SubRegistry) {
141 // Get iterator from this instance
142 $iteratorInstance = $entry->getIterator();
145 //* DEBUG-DIE: */ die(sprintf('[%s:%d]: key=%s,iteratorInstance=%s', __METHOD__, __LINE__, $key, print_r($iteratorInstance, TRUE)));
148 $keys = $iteratorInstance->getRegistryKeys();
151 if (!isset($keys['instance'])) {
153 throw new LogicException(sprintf('key=%s,keys[instance] is not set.', $key));
156 // Add all sub-registry keys to this registry keys array
157 $this->registryKeys['instance'][$key] = $keys['instance'];
160 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s,keys=%s', $key, print_r($this->registryKeys['instance'][$key], TRUE)));
164 } elseif (!($entry instanceof Registerable)) {
165 // Not registerable?!
166 throw new LogicException(sprintf('entry[]=%s does not implement Registerable.', gettype($entry)));
167 } elseif (is_null($this->key)) {
169 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: Setting key=%s ...', $key));
177 //* DEBUG-DIE: */ die(sprintf('[%s:%d]: key=%s,entry=%s', __METHOD__, __LINE__, $key, print_r($entry, TRUE)));
178 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s,entry[]=%s - Adding ...', $key, gettype($entry)));
181 $this->registryKeys['instance'][$key] = [];
187 * Getter for all registry keys (array)
189 * @return $registryKeys Registry keys
191 public final function getRegistryKeys () {
193 return $this->registryKeys;
197 * Getter for current value from group or generic
199 * @return $current Current value in iteration
201 public function current () {
205 $this->partialStub('Please implement this method.');
212 * Getter for key from group or generic
214 * @return $key Current key in iteration
216 public function key () {
222 * Advances to the next entry
226 public function next () {
227 $this->partialStub('Please implement this method.');
231 * Rewinds to the beginning of the iteration
235 public function rewind () {
236 // Is the registry empty?
237 if (count($this->registryKeys) == 0) {
238 // Then no need to continue
243 /* 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)));
247 * Checks wether the current entry is valid (not at the end of the list)
249 * @return $valid Whether the current key is still valid
251 public function valid () {