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