]> git.mxchange.org Git - core.git/blob - framework/main/classes/iterator/registry/class_RegistryIterator.php
Continued:
[core.git] / framework / main / classes / iterator / registry / class_RegistryIterator.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Iterator\Registry;
4
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;
12
13 // Import SPL stuff
14 use \LogicException;
15
16 /**
17  * A Registry iterator
18  *
19  * @author              Roland Haeder <webmaster@ship-simu.org>
20  * @version             0.0.0
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
24  *
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.
29  *
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.
34  *
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/>.
37  */
38 class RegistryIterator extends BaseIterator implements IteratableRegistry {
39         /**
40          * All found registry keys
41          */
42         private $registryKeys = [];
43
44         /**
45          * Current array key
46          */
47         private $key = NULL;
48
49         /**
50          * Valid status (default: not valid)
51          */
52         private $valid = FALSE;
53
54         /**
55          * Protected constructor
56          *
57          * @return      void
58          */
59         protected function __construct () {
60                 // Call parent constructor
61                 parent::__construct(__CLASS__);
62         }
63
64         /**
65          * Creates an instance of this class
66          *
67          * @param       $registryInstance       An instance of a Register class
68          * @return      $iteratorInstance       An instance of a Iterator class
69          */
70         public final static function createRegistryIterator (Register $registryInstance) {
71                 // Get new instance
72                 $iteratorInstance = new RegistryIterator();
73
74                 // Set registry here
75                 $iteratorInstance->setRegistryInstance($registryInstance);
76
77                 // Return the prepared instance
78                 return $iteratorInstance;
79         }
80
81         /**
82          * Initializes this iterator by scanning over the registry for all keys.
83          *
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)
87          * @return      void
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
90          */
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)) {
96                                 // Throw NPE
97                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
98                         } elseif (is_null($criteriaMethod)) {
99                                 // Throw NPE
100                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
101                         }
102
103                         // Set all
104                         $iteratorInstance->setCallbackInstance($callbackInstance);
105                         $iteratorInstance->setCriteriaKey($criteriaKey);
106                 } // END - if
107
108                 // Get generic registry entries from it
109                 $entries = $this->getRegistryInstance()->getGenericRegistry();
110
111                 // Init registry keys array
112                 $this->registryKeys['generic'] = [];
113
114                 // Anything in there?
115                 if (count($entries) > 0) {
116                         // Debugging:
117                         /* DEBUG-DIE: */ die(sprintf('[%s:%d]: entries=%s', __METHOD__, __LINE__, print_r($entries, TRUE)));
118                 } // END - if
119
120                 // Get instance registry entries from it
121                 $entries = $this->getRegistryInstance()->getInstanceRegistry();
122
123                 // Init registry keys array
124                 $this->registryKeys['instance'] = [];
125
126                 // Anything in there?
127                 if (count($entries) > 0) {
128                         // Then run over all
129                         foreach ($entries as $key => $entry) {
130                                 // Debug message
131                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s,entry[]=%s', $key, gettype($entry)));
132
133                                 // Is it 'socket_registry' ?
134                                 if ($key == 'socket_registry') {
135                                         // Skip this entry
136                                         continue;
137                                 } // END - if
138
139                                 // Is it an instance of a sub-registry?
140                                 if ($entry instanceof SubRegistry) {
141                                         // Get iterator from this instance
142                                         $iteratorInstance = $entry->getIterator();
143
144                                         // Debugging:
145                                         //* DEBUG-DIE: */ die(sprintf('[%s:%d]: key=%s,iteratorInstance=%s', __METHOD__, __LINE__, $key, print_r($iteratorInstance, TRUE)));
146
147                                         // Get all keys
148                                         $keys = $iteratorInstance->getRegistryKeys();
149
150                                         // Should be there
151                                         if (!isset($keys['instance'])) {
152                                                 // Should not happen
153                                                 throw new LogicException(sprintf('key=%s,keys[instance] is not set.', $key));
154                                         } // END - if
155
156                                         // Add all sub-registry keys to this registry keys array
157                                         $this->registryKeys['instance'][$key] = $keys['instance'];
158
159                                         // Debug message
160                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s,keys=%s', $key, print_r($this->registryKeys['instance'][$key], TRUE)));
161
162                                         // Skip below code
163                                         continue;
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)) {
168                                         // Debug message
169                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: Setting key=%s ...', $key));
170
171                                         // Init key/valid
172                                         $this->key = $key;
173                                         $this->valid = TRUE;
174                                 }
175
176                                 // Debugging:
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)));
179
180                                 // Add key to array
181                                 $this->registryKeys['instance'][$key] = [];
182                         } // END - foreach
183                 } // END - if
184         }
185
186         /**
187          * Getter for all registry keys (array)
188          *
189          * @return      $registryKeys   Registry keys
190          */
191         public final function getRegistryKeys () {
192                 // Return it
193                 return $this->registryKeys;
194         }
195
196         /**
197          * Getter for current value from group or generic
198          *
199          * @return      $current        Current value in iteration
200          */
201         public function current () {
202                 // Default is null
203                 $current = null;
204
205                 $this->partialStub('Please implement this method.');
206
207                 // Return it
208                 return $current;
209         }
210
211         /**
212          * Getter for key from group or generic
213          *
214          * @return      $key    Current key in iteration
215          */
216         public function key () {
217                 // Return it
218                 return $this->key;
219         }
220
221         /**
222          * Advances to the next entry
223          *
224          * @return      void
225          */
226         public function next () {
227                 $this->partialStub('Please implement this method.');
228         }
229
230         /**
231          * Rewinds to the beginning of the iteration
232          *
233          * @return      void
234          */
235         public function rewind () {
236                 // Is the registry empty?
237                 if (count($this->registryKeys) == 0) {
238                         // Then no need to continue
239                         return;
240                 } // END - if
241
242                 // Debugging:
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)));
244         }
245
246         /**
247          * Checks wether the current entry is valid (not at the end of the list)
248          *
249          * @return      $valid  Whether the current key is still valid
250          */
251         public function valid () {
252                 // Return flag
253                 return $this->valid;
254         }
255
256 }