Rewrote core:
[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          * Instance registry
37          */
38         private $instanceRegistry = array();
39
40         /**
41          * Raw data entries (non-objects)
42          */
43         private $rawEntries = array();
44
45         /**
46          * Protected constructor
47          *
48          * @param       $className      Name of the class
49          * @return      void
50          */
51         protected function __construct ($className) {
52                 // Call parent constructor
53                 parent::__construct($className);
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 = (isset($this->instanceRegistry[$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->instanceRegistry[$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->instanceRegistry;
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                 // Simply add it
100                 $this->rawEntries[$key][] = $value;
101         }
102
103         /**
104          * Getter for entries or "sub entries"
105          *
106          * @return      $entries        An array with entries from this registry
107          */
108         public final function getEntries ($key = NULL) {
109                 // Default is whole array
110                 $entries = $this->rawEntries;
111
112                 // Is $key set?
113                 if (!is_null($key)) {
114                         // Then use this entry
115                         $entries = $this->rawEntries[$key];
116                 } // END - if
117
118                 // Return the array
119                 return $entries;
120         }
121
122         /**
123          * "Getter" for an array of all entries for given key
124          *
125          * @param       $lookFor        The key to look for
126          * @return      $entry          An array with all keys
127          */
128         public function getArrayFromKey ($lookFor) {
129                 // Init array
130                 $entry = array();
131
132                 // "Walk" over all entries
133                 foreach ($this->getEntries('object-name') as $key => $value) {
134                         // Debug message
135                         //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
136
137                         // If $value matches the $lookFor, we need to look for more entries for this!
138                         if ($lookFor == $value) {
139                                 // Look for more entries
140                                 foreach ($this->getEntries() as $key2 => $value2) {
141                                         // Debug message
142                                         //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY: Checking key2=' . $key2 . ',value2=' . print_r($value2, true) . ',lookFor=' . $lookFor);
143
144                                         // Both keys must match!
145                                         if (($key == $key2) || (isset($value2[$key]))) {
146                                                 // Debug message
147                                                 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY: Adding ' . $value2[$key] . ' ...');
148
149                                                 // Then add it
150                                                 $entry[$key2] = $value2[$key];
151                                         } // END - if
152                                 } // END - foreach
153
154                                 // Skip further lookups
155                                 break;
156                         } // END - if
157                 } // END - foreach
158
159                 // Return it
160                 return $entry;
161         }
162
163         /**
164          * Gets a registered instance or null if not found
165          *
166          * @param       $instanceKey            The key to identify the instance
167          * @return      $objectInstance         An instance we shall store
168          * @throws      NullPointerException    If the requested key is not found
169          */
170         public function getInstance ($instanceKey) {
171                 // Is the instance there?
172                 if (!$this->instanceExists($instanceKey)) {
173                         // This might happen if a non-registered key was requested
174                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
175                 } // END - if
176
177                 // Get the instance
178                 $objectInstance = $this->instanceRegistry[$instanceKey];
179
180                 // Return the result
181                 return $objectInstance;
182         }
183
184         /**
185          * "Getter" for a registry key for given prefix and array. This method
186          * calls implode() to get a suitable key. This method does not care about
187          * the indexes.
188          *
189          * @param       $prefix                 Prefix for the key
190          * @param       $data                   An array with data
191          * @return      $registryKey    A registry key
192          */
193         public static function getRegistryKeyFromArray ($prefix, array $data) {
194                 // "Generate" the key
195                 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);
196
197                 // Return it
198                 return $registryKey;
199         }
200 }
201
202 // [EOF]
203 ?>