718f0a4f24b17176e13676eaf7562f0c44d8122e
[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 - 2011 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          * Instance of this class
27          */
28         private static $registryInstance = NULL;
29
30         /**
31          * Instance registry
32          */
33         private $instanceRegistry = array();
34
35         /**
36          * Raw data entries (non-objects)
37          */
38         private $rawEntries = array();
39
40         /**
41          * Protected constructor
42          *
43          * @param       $className      Name of the class
44          * @return      void
45          */
46         protected function __construct ($className) {
47                 // Call parent constructor
48                 parent::__construct($className);
49         }
50
51         /**
52          * Checks wether an instance key was found
53          *
54          * @param       $instanceKey    The key holding an instance in registry
55          * @return      $exists                 Wether the key exists in registry
56          */
57         public function instanceExists ($instanceKey) {
58                 // Does this key exists?
59                 $exists = (isset($this->instanceRegistry[$instanceKey]));
60
61                 // Return the result
62                 return $exists;
63         }
64
65         /**
66          * Adds/overwrites a new instance to the registry at the given key
67          *
68          * @param       $instanceKey            The key to identify the instance
69          * @param       $objectInstance         An instance we shall store
70          * @return      void
71          */
72         public function addInstance ($instanceKey, Registerable $objectInstance) {
73                 $this->instanceRegistry[$instanceKey] = $objectInstance;
74         }
75
76         /**
77          * Getter for whole instance registry
78          *
79          * @return      $instanceRegistry       The whole instance registry array
80          */
81         public final function getInstanceRegistry () {
82                 return $this->instanceRegistry;
83         }
84
85         /**
86          * Adds a new entry to the given list name. If you want to add objects
87          * please use addInstance() and getInstance() instead.
88          *
89          * @param       $key    The key to identify the whole list
90          * @param       $value  The value to be stored
91          * @return      void
92          */
93         public final function addEntry ($key, $value) {
94                 // Simply add it
95                 $this->rawEntries[$key][] = $value;
96         }
97
98         /**
99          * Getter for entries or "sub entries"
100          *
101          * @return      $entries        An array with entries from this registry
102          */
103         public final function getEntries ($key = NULL) {
104                 // Default is whole array
105                 $entries = $this->rawEntries;
106
107                 // Is $key set?
108                 if (!is_null($key)) {
109                         // Then use this entry
110                         $entries = $this->rawEntries[$key];
111                 } // END - if
112
113                 // Return the array
114                 return $entries;
115         }
116
117         /**
118          * "Getter" for an array of all entries for given key
119          *
120          * @param       $lookFor        The key to look for
121          * @return      $entry          An array with all keys
122          */
123         public function getArrayFromKey ($lookFor) {
124                 // Init array
125                 $entry = array();
126
127                 // "Walk" over all entries
128                 foreach ($this->getEntries('object-name') as $key=>$value) {
129                         // Debug message
130                         //* DEBUG: */ $this->debugOutput('REGISTRY: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
131
132                         // If $value matches the $lookFor, we need to look for more entries for this!
133                         if ($lookFor == $value) {
134                                 // Look for more entries
135                                 foreach ($this->getEntries() as $key2=>$value2) {
136                                         // Debug message
137                                         //* DEBUG: */ $this->debugOutput('REGISTRY: Checking key2=' . $key2 . ',value2=' . print_r($value2, true) . ',lookFor=' . $lookFor);
138
139                                         // Both keys must match!
140                                         if (($key == $key2) || (isset($value2[$key]))) {
141                                                 // Debug message
142                                                 //* DEBUG: */ $this->debugOutput('REGISTRY: Adding ' . $value2[$key] . ' ...');
143
144                                                 // Then add it
145                                                 $entry[$key2] = $value2[$key];
146                                         } // END - if
147                                 } // END - foreach
148
149                                 // Skip further lookups
150                                 break;
151                         } // END - if
152                 } // END - foreach
153
154                 // Return it
155                 return $entry;
156         }
157
158         /**
159          * Gets a registered instance or null if not found
160          *
161          * @param       $instanceKey            The key to identify the instance
162          * @return      $objectInstance         An instance we shall store
163          * @throws      NullPointerException    If the requested key is not found
164          */
165         public function getInstance ($instanceKey) {
166                 // By default the instance is not in registry
167                 $objectInstance = NULL;
168
169                 // Is the instance there?
170                 if ($this->instanceExists($instanceKey)) {
171                         $objectInstance = $this->instanceRegistry[$instanceKey];
172                 } // END - if
173
174                 // Still not fetched?
175                 if (is_null($objectInstance)) {
176                         // This might happen if a non-registered key was requested
177                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
178                 } // END - if
179
180                 // Return the result
181                 return $objectInstance;
182         }
183 }
184
185 // [EOF]
186 ?>