Fixed missing array
[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                 // Is the array there?
100                 if (!isset($this->rawEntries[$key])) {
101                         // Then intialize it here
102                         $this->rawEntries[$key] = array();
103                 } // END - if
104
105                 // Simply add it
106                 array_push($this->rawEntries[$key], $value);
107         }
108
109         /**
110          * Getter for entries or "sub entries"
111          *
112          * @return      $entries        An array with entries from this registry
113          */
114         public final function getEntries ($key = NULL) {
115                 // Default is whole array
116                 $entries = $this->rawEntries;
117
118                 // Is $key set?
119                 if ((!is_null($key)) && (isset($this->rawEntries[$key]))) {
120                         // Then use this entry
121                         $entries = $this->rawEntries[$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                 // Init array
137                 $entry = array();
138
139                 // "Walk" over all entries
140                 foreach ($this->getEntries($arrayKey) as $key => $value) {
141                         // Debug message
142                         //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
143
144                         // If $value matches the $lookFor, we need to look for more entries for this!
145                         if ($lookFor == $value) {
146                                 // Look for more entries
147                                 foreach ($this->getEntries() as $key2 => $value2) {
148                                         // Debug message
149                                         //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key2=' . $key2 . ',value2=' . print_r($value2, true) . ',lookFor=' . $lookFor);
150
151                                         // Both keys must match!
152                                         if (($key == $key2) || (isset($value2[$key]))) {
153                                                 // Debug message
154                                                 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Adding ' . $value2[$key] . ' ...');
155
156                                                 // Then add it
157                                                 $entry[$key2] = $value2[$key];
158                                         } // END - if
159                                 } // END - foreach
160
161                                 // Skip further lookups
162                                 break;
163                         } // END - if
164                 } // END - foreach
165
166                 // Return it
167                 return $entry;
168         }
169
170         /**
171          * Gets a registered instance or null if not found
172          *
173          * @param       $instanceKey            The key to identify the instance
174          * @return      $objectInstance         An instance we shall store
175          * @throws      NullPointerException    If the requested key is not found
176          */
177         public function getInstance ($instanceKey) {
178                 // Is the instance there?
179                 if (!$this->instanceExists($instanceKey)) {
180                         // This might happen if a non-registered key was requested
181                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
182                 } // END - if
183
184                 // Get the instance
185                 $objectInstance = $this->instanceRegistry[$instanceKey];
186
187                 // Return the result
188                 return $objectInstance;
189         }
190
191         /**
192          * "Getter" for a registry key for given prefix and array. This method
193          * calls implode() to get a suitable key. This method does not care about
194          * the indexes.
195          *
196          * @param       $prefix                 Prefix for the key
197          * @param       $data                   An array with data
198          * @return      $registryKey    A registry key
199          */
200         public static function getRegistryKeyFromArray ($prefix, array $data) {
201                 // "Generate" the key
202                 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);
203
204                 // Return it
205                 return $registryKey;
206         }
207 }
208
209 // [EOF]
210 ?>