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