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