f39457cf07f4f5b32085ececa9abbfc45dedccd5
[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                 // "Walk" over all entries
133                 foreach ($this->getEntries($arrayKey) as $key => $value) {
134                         // Debug message
135                         //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: 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[' . __LINE__ . ']: 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[' . __LINE__ . ']: 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->getGenericArrayKey('registry', 'instance', $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 ?>