Some updates:
[core.git] / framework / main / classes / registry / class_BaseRegistry.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Registry;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
8 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
9
10 // Import SPL stuff
11 use \IteratorAggregate;
12
13 /**
14  * A general Registry
15  *
16  * @author              Roland Haeder <webmaster@shipsimu.org>
17  * @version             0.0.0
18 <<<<<<< HEAD:framework/main/classes/registry/class_BaseRegistry.php
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20 =======
21  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
22 >>>>>>> Some updates::inc/main/classes/registry/class_BaseRegistry.php
23  * @license             GNU GPL 3.0 or any newer version
24  * @link                http://www.shipsimu.org
25  *
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.
30  *
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.
35  *
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/>.
38  */
39 abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable, IteratorAggregate {
40         /**
41          * Glue for generating a registry key
42          */
43         const REGISTRY_KEY_GLUE = '_';
44
45         /**
46          * Instance of this class
47          */
48         private static $registryInstance = NULL;
49
50         /**
51          * Protected constructor
52          *
53          * @param       $className      Name of the class
54          * @return      void
55          */
56         protected function __construct ($className) {
57                 // Call parent constructor
58                 parent::__construct($className);
59
60                 // Init generic arrays
61                 $this->initGenericArrayGroup('registry', 'generic');
62                 $this->initGenericArrayGroup('registry', 'instance');
63         }
64
65         /**
66          * Returns an iterator for this whole registry.
67          *
68          * @return      $iteratorInstance       An instance of a Iterator class
69          */
70         public function getIterator () {
71                 // Is it set?
72                 if (is_null($this->getIteratorInstance())) {
73                         // Then instance it
74                         $iteratorInstance = ObjectFactory::createObjectByConfiguredName('registry_iterator_class', array($this));
75
76                         // ... and set it here
77                         $this->setIteratorInstance($iteratorInstance);
78                 } else {
79                         // Use set iterator
80                         $iteratorInstance = $this->getIteratorInstance();
81                 }
82
83                 // Return it
84                 return $iteratorInstance;
85         }
86
87         /**
88          * Checks whether an instance key was found
89          *
90          * @param       $instanceKey    The key holding an instance in registry
91          * @return      $exists                 Whether the key exists in registry
92          */
93         public function instanceExists ($instanceKey) {
94                 // Does this key exists?
95                 $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey);
96
97                 // Return the result
98                 return $exists;
99         }
100
101         /**
102          * Adds/overwrites a new instance to the registry at the given key
103          *
104          * @param       $instanceKey            The key to identify the instance
105          * @param       $objectInstance         An instance we shall store
106          * @return      void
107          */
108         public function addInstance ($instanceKey, Registerable $objectInstance) {
109                 $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance);
110         }
111
112         /**
113          * Getter for whole generic registry
114          *
115          * @return      $instanceRegistry       The whole generic registry array
116          */
117         public final function getGenericRegistry () {
118                 return $this->getGenericSubArray('registry', 'generic');
119         }
120
121         /**
122          * Getter for whole instance registry
123          *
124          * @return      $instanceRegistry       The whole instance registry array
125          */
126         public final function getInstanceRegistry () {
127                 return $this->getGenericSubArray('registry', 'instance');
128         }
129
130         /**
131          * Adds a new entry to the given list name. If you want to add objects
132          * please use addInstance() and getInstance() instead.
133          *
134          * @param       $key    The key to identify the whole list
135          * @param       $value  The value to be stored
136          * @return      void
137          */
138         public final function addEntry ($key, $value) {
139                 // Key must not be an array
140                 assert(!is_array($key));
141
142                 // Push it
143                 $this->pushValueToGenericArrayKey('registry', 'generic', $key, $value);
144         }
145
146         /**
147          * Getter for entries or "sub entries"
148          *
149          * @return      $entries        An array with entries from this registry
150          */
151         public final function getEntries ($key = NULL) {
152                 // Key must not be an array
153                 assert(!is_array($key));
154
155                 // Default is whole array
156                 $entries = $this->getGenericArray('registry');
157
158                 // Is $key set?
159                 if (!is_null($key)) {
160                         // Then use this entry
161                         $entries = $this->getGenericArrayKey('registry', 'generic', $key);
162                 } // END - if
163
164                 // Return the array
165                 return $entries;
166         }
167
168         /**
169          * "Getter" for an array of all entries for given key
170          *
171          * @param       $arrayKey       The array (key) to look in
172          * @param       $lookFor        The key to look for
173          * @return      $entry          An array with all keys
174          */
175         public function getArrayFromKey ($arrayKey, $lookFor) {
176                 // Key must not be an array
177                 assert(!is_array($arrayKey));
178
179                 // Init array
180                 $entry = array();
181
182                 // Debug message
183                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Checking arrayKey=' . $arrayKey . ',lookFor=' . $lookFor);
184
185                 // "Walk" over all entries
186                 foreach ($this->getEntries($arrayKey) as $key => $value) {
187                         // Debug message
188                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
189
190                         // If $value matches the $lookFor, we need to look for more entries for this!
191                         if ($lookFor == $value) {
192                                 // Look for more entries
193                                 foreach ($this->getEntries() as $key2 => $value2) {
194                                         // Now "walk" through all entries, if an array is returned
195                                         if (is_array($value2)) {
196                                                 // Debug message
197                                                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Checking key2=' . $key2 . ',value2()=' . count($value2) . ',lookFor=' . $lookFor);
198
199                                                 // "Walk" through all of them
200                                                 foreach ($value2 as $key3 => $value3) {
201                                                         // $value3 needs to be an array
202                                                         assert(is_array($value3));
203
204                                                         // Debug message
205                                                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Checking key=' . $key . ',key3=' . $key3 . ',isset()=' . isset($value3[$key]) . ' ...');
206
207                                                         // Both keys must match!
208                                                         if (($key == $key3) || (isset($value3[$key]))) {
209                                                                 // Debug message
210                                                                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Adding ' . $value3[$key] . ' ...');
211
212                                                                 // Then add it
213                                                                 $entry[$key3] = $value3[$key];
214                                                         } // END - if
215                                                 } // END - foreach
216                                         } // END - if
217                                 } // END - foreach
218
219                                 // Skip further lookups
220                                 break;
221                         } // END - if
222                 } // END - foreach
223
224                 // Debug message
225                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Returning entry(' . count($entry) . ')=' . print_r($entry, true));
226
227                 // Return it
228                 return $entry;
229         }
230
231         /**
232          * Gets a registered instance or null if not found
233          *
234          * @param       $instanceKey            The key to identify the instance
235          * @return      $objectInstance         An instance we shall store
236          * @throws      NullPointerException    If the requested key is not found
237          */
238         public function getInstance ($instanceKey) {
239                 // Is the instance there?
240                 if (!$this->instanceExists($instanceKey)) {
241                         // This might happen if a non-registered key was requested
242                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
243                 } // END - if
244
245                 // Get the instance
246                 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
247
248                 // Return the result
249                 return $objectInstance;
250         }
251
252         /**
253          * "Getter" for a registry key for given prefix and array. This method
254          * calls implode() to get a suitable key. This method does not care about
255          * the indexes.
256          *
257          * @param       $prefix                 Prefix for the key
258          * @param       $data                   An array with data
259          * @return      $registryKey    A registry key
260          */
261         public static function getRegistryKeyFromArray ($prefix, array $data) {
262                 // "Generate" the key
263                 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);
264
265                 // Return it
266                 return $registryKey;
267         }
268
269 }