]> git.mxchange.org Git - core.git/blob - inc/classes/main/registry/class_BaseRegistry.php
Fixed handling of newly introduced generic array
[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
46         /**
47          * Checks whether an instance key was found
48          *
49          * @param       $instanceKey    The key holding an instance in registry
50          * @return      $exists                 Whether the key exists in registry
51          */
52         public function instanceExists ($instanceKey) {
53                 // Does this key exists?
54                 $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey);
55
56                 // Return the result
57                 return $exists;
58         }
59
60         /**
61          * Adds/overwrites a new instance to the registry at the given key
62          *
63          * @param       $instanceKey            The key to identify the instance
64          * @param       $objectInstance         An instance we shall store
65          * @return      void
66          */
67         public function addInstance ($instanceKey, Registerable $objectInstance) {
68                 $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance);
69         }
70
71         /**
72          * Getter for whole instance registry
73          *
74          * @return      $instanceRegistry       The whole instance registry array
75          */
76         public final function getInstanceRegistry () {
77                 return $this->getGenericSubArray('registry', 'instance');
78         }
79
80         /**
81          * Adds a new entry to the given list name. If you want to add objects
82          * please use addInstance() and getInstance() instead.
83          *
84          * @param       $key    The key to identify the whole list
85          * @param       $value  The value to be stored
86          * @return      void
87          */
88         public final function addEntry ($key, $value) {
89                 // Key must not be an array
90                 assert(!is_array($key));
91
92                 // Push it
93                 $this->pushValueToGenericArrayElement('raw', 'generic', $key, $value);
94         }
95
96         /**
97          * Getter for entries or "sub entries"
98          *
99          * @return      $entries        An array with entries from this registry
100          */
101         public final function getEntries ($key = NULL) {
102                 // Key must not be an array
103                 assert(!is_array($key));
104
105                 // Default is whole array
106                 $entries = $this->getGenericArray('raw');
107
108                 // Is $key set?
109                 if (!is_null($key)) {
110                         // Then use this entry
111                         $entries = $this->getGenericArrayKey('raw', 'generic', $key);
112                 } // END - if
113
114                 // Return the array
115                 return $entries;
116         }
117
118         /**
119          * "Getter" for an array of all entries for given key
120          *
121          * @param       $arrayKey       The array (key) to look in
122          * @param       $lookFor        The key to look for
123          * @return      $entry          An array with all keys
124          */
125         public function getArrayFromKey ($arrayKey, $lookFor) {
126                 // Key must not be an array
127                 assert(!is_array($arrayKey));
128
129                 // Init array
130                 $entry = array();
131
132                 // Debug message
133                 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking arrayKey=' . $arrayKey . ',lookFor=' . $lookFor);
134
135                 // "Walk" over all entries
136                 foreach ($this->getEntries($arrayKey) as $key => $value) {
137                         // Debug message
138                         //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
139
140                         // If $value matches the $lookFor, we need to look for more entries for this!
141                         if ($lookFor == $value) {
142                                 // Look for more entries
143                                 foreach ($this->getEntries() as $key2 => $value2) {
144                                         // Now "walk" through all entries, if an array is returned
145                                         if (is_array($value2)) {
146                                                 // Debug message
147                                                 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key2=' . $key2 . ',value2()=' . count($value2) . ',lookFor=' . $lookFor);
148
149                                                 // "Walk" through all of them
150                                                 foreach ($value2 as $key3 => $value3) {
151                                                         // $value3 needs to be an array
152                                                         assert(is_array($value3));
153
154                                                         // Debug message
155                                                         //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key=' . $key . ',key3=' . $key3 . ',isset()=' . isset($value3[$key]) . ' ...');
156
157                                                         // Both keys must match!
158                                                         if (($key == $key3) || (isset($value3[$key]))) {
159                                                                 // Debug message
160                                                                 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Adding ' . $value3[$key] . ' ...');
161
162                                                                 // Then add it
163                                                                 $entry[$key3] = $value3[$key];
164                                                         } // END - if
165                                                 } // END - foreach
166                                         } // END - if
167                                 } // END - foreach
168
169                                 // Skip further lookups
170                                 break;
171                         } // END - if
172                 } // END - foreach
173
174                 // Debug message
175                 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Returning entry(' . count($entry) . ')=' . print_r($entry, TRUE));
176
177                 // Return it
178                 return $entry;
179         }
180
181         /**
182          * Gets a registered instance or null if not found
183          *
184          * @param       $instanceKey            The key to identify the instance
185          * @return      $objectInstance         An instance we shall store
186          * @throws      NullPointerException    If the requested key is not found
187          */
188         public function getInstance ($instanceKey) {
189                 // Is the instance there?
190                 if (!$this->instanceExists($instanceKey)) {
191                         // This might happen if a non-registered key was requested
192                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
193                 } // END - if
194
195                 // Get the instance
196                 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
197
198                 // Return the result
199                 return $objectInstance;
200         }
201
202         /**
203          * "Getter" for a registry key for given prefix and array. This method
204          * calls implode() to get a suitable key. This method does not care about
205          * the indexes.
206          *
207          * @param       $prefix                 Prefix for the key
208          * @param       $data                   An array with data
209          * @return      $registryKey    A registry key
210          */
211         public static function getRegistryKeyFromArray ($prefix, array $data) {
212                 // "Generate" the key
213                 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);
214
215                 // Return it
216                 return $registryKey;
217         }
218 }
219
220 // [EOF]
221 ?>