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\Registerable;
10 use Org\Mxchange\CoreFramework\Registry\Sub\SubRegistry;
11 use Org\Mxchange\CoreFramework\Traits\Registry\RegisterTrait;
14 use \BadMethodCallException;
20 * @author Roland Haeder <webmaster@ship-simu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.ship-simu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 class RegistryIterator extends BaseIterator implements IteratableRegistry {
44 * All found registry keys
46 private $registryKeys = [];
49 * An array containing keys ob sub-registries which keys should only be
50 * included in the iterator.
52 private $onlyRegistries = [];
60 * Protected constructor
64 protected function __construct () {
65 // Call parent constructor
66 parent::__construct(__CLASS__);
68 // Init registry keys array
69 $this->registryKeys = [
78 * Creates an instance of this class
80 * @param $registryInstance An instance of a Register class
81 * @return $iteratorInstance An instance of a Iterator class
83 public final static function createRegistryIterator (Register $registryInstance) {
85 $iteratorInstance = new RegistryIterator();
88 $iteratorInstance->setRegistryInstance($registryInstance);
90 // Return the prepared instance
91 return $iteratorInstance;
95 * Setter for only-registries array
97 * @param $onlyRegistries Array with keys only being iterated over
100 private function setOnlyRegistries (array $onlyRegistries) {
101 $this->onlyRegistries = $onlyRegistries;
105 * Initializes this iterator by scanning over the registry for all keys.
107 * @param $onlyRegistries Only iterate on these sub-registry keys, default is all
109 * @throws LogicException If a registry entry does not implement Registerable
110 * @throws NullPointerException If criteriaKey or criteriaMethod is not set but a call-back instance is set
112 public function initIterator (array $onlyRegistries = []) {
113 // Set it in this registry
114 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR: onlyRegistries()=%d - CALLED!', count($onlyRegistries)));
115 $this->setOnlyRegistries($onlyRegistries);
117 // Get generic registry entries from it
118 $entries = $this->getRegistryInstance()->getGenericRegistry();
120 // Anything in there?
121 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[generic]: entries()=%d', count($entries)));
122 if (count($entries) > 0) {
124 /* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: UNFINISHED: entries=%s', __METHOD__, __LINE__, print_r($entries, TRUE)));
127 // Get instance registry entries from it
128 $entries = $this->getRegistryInstance()->getInstanceRegistry();
130 // Anything in there?
131 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: entries()=%d', count($entries)));
132 if (count($entries) > 0) {
134 foreach ($entries as $key => $entry) {
135 // Is an unwanted registry found?
136 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s,entry[]=%s', $key, gettype($entry)));
137 if (substr($key, -8, 8) == 'registry') {
139 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s is a registry key, skipping ...', $key));
143 // Is it an instance of a sub-registry?
144 if (count($onlyRegistries) > 0 && !in_array($key, $onlyRegistries, TRUE)) {
145 // Not in requested registries
146 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s is not wanted, skipping ...', $key));
148 } elseif ($entry instanceof SubRegistry) {
149 // Get iterator from this instance
150 $subRegistryInstances = $entry->getInstanceRegistry();
152 // Add all sub-registry keys to this registry keys array
153 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: subRegistryInstances()=%d', count($subRegistryInstances)));
154 //* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: key=%s,subRegistryInstances=%s', __METHOD__, __LINE__, $key, print_r($subRegistryInstances, TRUE)));
155 foreach (array_keys($subRegistryInstances) as $subKey) {
157 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: Adding key=%s,subKey=%s ...', $key, $subKey));
158 array_push($this->registryKeys['instance'], $subKey);
160 // Is the current key set?
161 if (is_null($this->key)) {
163 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: Setting subKey=%s as first key ...', $subKey));
164 $this->key = $subKey;
170 } elseif (!($entry instanceof Registerable)) {
171 // Not registerable?!
172 throw new LogicException(sprintf('entry[]=%s does not implement Registerable.', gettype($entry)));
175 // Is the current key set?
176 if (is_null($this->key)) {
178 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: Setting key=%s as first key ...', $key));
183 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s - Adding ...', $key));
184 //* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: key=%s,entry=%s', __METHOD__, __LINE__, $key, print_r($entry, TRUE)));
185 array_push($this->registryKeys['instance'], $key);
190 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY-ITERATOR: EXIT!');
194 * Getter for all registry keys (array)
196 * @return $registryKeys Registry keys
198 public final function getRegistryKeys () {
200 return $this->registryKeys;
204 * Getter for current value from group or generic
206 * @return $current Current value in iteration
207 * @throws NullPointerException If current key points to a non-existing entry in searched registries
209 public function current () {
211 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: CALLED!', $this->key()));
212 //* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(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)));
216 // Get all registries
217 $allRegistries = $this->getRegistryInstance()->getInstanceRegistry();
219 // Loop through all sub-sets
220 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: allRegistries()=%d', $this->key(), count($allRegistries)));
221 foreach ($allRegistries as $registryKey => $registryInstance) {
222 // Is this key wanted?
223 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: registryKey=%s', $this->key(), $registryKey));
224 if (count($this->onlyRegistries) == 0 || in_array($registryKey, $this->onlyRegistries)) {
225 // Yes, then loop through them only
226 $instances = $registryInstance->getInstanceRegistry();
228 // The key should be there
229 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: Handling registryKey=%s ...', $this->key(), $registryKey));
230 //* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: instances=%s', __METHOD__, __LINE__, print_r($instances, TRUE)));
231 if (!isset($instances[$this->key()])) {
233 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: registryKey=%s has no this->key=%s, skipping ...', $this->key(), $registryKey));
237 // Set as current element and leave loop
238 $current = $instances[$this->key()];
239 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: registryKey=%s,current=%s found. - BREAK!', $this->key(), $registryKey, $current->__toString()));
244 // Is current still NULL?
245 if (is_null($current)) {
246 // This cannot happen and indicates a logic error
247 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
251 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: current[%s]=%s - EXIT!', $this->key(), gettype($current), $current));
256 * Getter for key from group or generic
258 * @return $key Current key in iteration
260 public function key () {
262 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: EXIT!', $this->key));
267 * Advances to the next entry
270 * @throws BadMethodCallException If $this->valid() returns FALSE
271 * @throws UnexpectedValueException If $registryType is not changed
273 public function next () {
274 // Is valid() still TRUE?
275 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: CALLED!', $this->key()));
276 //* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(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)));
277 if (!$this->valid()) {
279 throw new BadMethodCallException(sprintf('this->key[%s]=%s is no longer valid, but method was called.', gettype($this->key()), $this->key()));
280 } elseif ((count($this->registryKeys['generic']) == 0) && (count($this->registryKeys['instance']) == 0)) {
281 // Both arrays are empty
282 throw new BadMethodCallException('No array elements in "generic" and "instance" found but method called.');
285 // Default is not valid
287 $isNextValid = FALSE;
288 $registryType = 'invalid';
290 // Get first array element ...
291 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: this->registryKeys[generic]()=%d,this->registryKeys[instance]()=%d', $this->key(), count($this->registryKeys['generic']), count($this->registryKeys['instance'])));
292 if (count($this->registryKeys['generic']) > 0) {
293 // First generic array
294 /* UNFINISHED */ ApplicationEntryPoint::exitApplication(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)));
295 } elseif (count($this->registryKeys['instance']) > 0) {
296 // Second instance, current key's index + 1
297 $nextIndex = array_search($this->key(), $this->registryKeys['instance']) + 1;
298 $isNextValid = isset($this->registryKeys['instance'][$nextIndex]);
299 $registryType = 'instance';
302 // Is it still valid?
303 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: isNextValid=%d', $this->key(), intval($isNextValid)));
306 throw new BadMethodCallException(sprintf('this->key=%s is last key in this iteration. Forgot to invoke valid() before?', $this->key()));
307 } elseif ($registryType == 'invalid') {
309 throw new UnexpectedValueException('registryType has not been changed.');
312 // Yes, then advance to that entry
313 $this->key = $this->registryKeys[$registryType][$nextIndex];
316 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: EXIT!', $this->key()));
320 * Rewinds to the beginning of the iteration
323 * @throws BadMethodCallException If $this->key is already the first element
325 public function rewind () {
326 // Is current key first key?
327 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: CALLED!', $this->key()));
328 //* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(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)));
329 if (array_search($this->key(), $this->getRegistryKeys()) === 0) {
330 // rewind() cannot rewind first entry!
331 throw new BadMethodCallException(sprintf('this->key=%s is already first element, but method was called.', $this->key()));
332 } elseif ((count($this->registryKeys['generic']) == 0) && (count($this->registryKeys['instance']) == 0)) {
333 // Both arrays are empty
334 throw new BadMethodCallException('No array elements in "generic" and "instance" found but method called.');
337 // Get first array element ...
338 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: this->registryKeys[generic]()=%d,this->registryKeys[instance]()=%d', $this->key(), count($this->registryKeys['generic']), count($this->registryKeys['instance'])));
339 if (count($this->registryKeys['generic']) > 0) {
340 // First generic array
341 /* UNFINISHED */ ApplicationEntryPoint::exitApplication(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)));
342 } elseif (count($this->registryKeys['instance']) > 0) {
344 $this->key = $this->registryKeys['instance'][0];
348 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: EXIT!', $this->key()));
352 * Checks wether the current entry is valid (not at the end of the list)
354 * @return $valid Whether the current key is still valid
356 public function valid () {
357 // Is the element there?
358 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: CALLED!', $this->key()));
359 //* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: this->key(%d)[%s]=%s,this->registryKeys=%s', __METHOD__, __LINE__, strlen($this->key()), gettype($this->key()), $this->key(), print_r($this->registryKeys, TRUE)));
360 $valid = (in_array($this->key(), $this->registryKeys['instance']) || in_array($this->key(), $this->registryKeys['generic']));
363 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[%s]: valid=%d - EXIT!', $this->key(), intval($valid)));