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