df0f9798fdf75a2d070da93182b4dc016397356b
[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->pushValueToGenericArrayElement('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                 // Push it
90                 $this->pushValueToGenericArrayElement('raw', 'generic', $key, $value);
91         }
92
93         /**
94          * Getter for entries or "sub entries"
95          *
96          * @return      $entries        An array with entries from this registry
97          */
98         public final function getEntries ($key = NULL) {
99                 // Default is whole array
100                 $entries = $this->getGenericArray('raw');
101
102                 // Is $key set?
103                 if (!is_null($key)) {
104                         // Then use this entry
105                         $entries = $this->getGenericArrayKey('raw', 'generic', $key);
106                 } // END - if
107
108                 // Return the array
109                 return $entries;
110         }
111
112         /**
113          * "Getter" for an array of all entries for given key
114          *
115          * @param       $arrayKey       The array (key) to look in
116          * @param       $lookFor        The key to look for
117          * @return      $entry          An array with all keys
118          */
119         public function getArrayFromKey ($arrayKey, $lookFor) {
120                 // Init array
121                 $entry = array();
122
123                 // "Walk" over all entries
124                 foreach ($this->getEntries($arrayKey) as $key => $value) {
125                         // Debug message
126                         //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
127
128                         // If $value matches the $lookFor, we need to look for more entries for this!
129                         if ($lookFor == $value) {
130                                 // Look for more entries
131                                 foreach ($this->getEntries() as $key2 => $value2) {
132                                         // Debug message
133                                         //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key2=' . $key2 . ',value2=' . print_r($value2, true) . ',lookFor=' . $lookFor);
134
135                                         // Both keys must match!
136                                         if (($key == $key2) || (isset($value2[$key]))) {
137                                                 // Debug message
138                                                 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Adding ' . $value2[$key] . ' ...');
139
140                                                 // Then add it
141                                                 $entry[$key2] = $value2[$key];
142                                         } // END - if
143                                 } // END - foreach
144
145                                 // Skip further lookups
146                                 break;
147                         } // END - if
148                 } // END - foreach
149
150                 // Return it
151                 return $entry;
152         }
153
154         /**
155          * Gets a registered instance or null if not found
156          *
157          * @param       $instanceKey            The key to identify the instance
158          * @return      $objectInstance         An instance we shall store
159          * @throws      NullPointerException    If the requested key is not found
160          */
161         public function getInstance ($instanceKey) {
162                 // Is the instance there?
163                 if (!$this->instanceExists($instanceKey)) {
164                         // This might happen if a non-registered key was requested
165                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
166                 } // END - if
167
168                 // Get the instance
169                 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
170
171                 // Return the result
172                 return $objectInstance;
173         }
174
175         /**
176          * "Getter" for a registry key for given prefix and array. This method
177          * calls implode() to get a suitable key. This method does not care about
178          * the indexes.
179          *
180          * @param       $prefix                 Prefix for the key
181          * @param       $data                   An array with data
182          * @return      $registryKey    A registry key
183          */
184         public static function getRegistryKeyFromArray ($prefix, array $data) {
185                 // "Generate" the key
186                 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);
187
188                 // Return it
189                 return $registryKey;
190         }
191 }
192
193 // [EOF]
194 ?>