Some code-cosmetics applied:
[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, 2010 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          * Adds a new entry to the given list name. If you want to add objects
78          * please use addInstance() and getInstance() instead.
79          *
80          * @param       $key    The key to identify the whole list
81          * @param       $value  The value to be stored
82          * @return      void
83          */
84         public final function addEntry ($key, $value) {
85                 // Simply add it
86                 $this->rawEntries[$key][] = $value;
87         }
88
89         /**
90          * Getter for entries or "sub entries"
91          *
92          * @return      $entries        An array with entries from this registry
93          */
94         public final function getEntries ($key = null) {
95                 // Default is whole array
96                 $entries = $this->rawEntries;
97
98                 // Is $key set?
99                 if (!is_null($key)) {
100                         // Then use this entry
101                         $entries = $this->rawEntries[$key];
102                 } // END - if
103
104                 // Return the array
105                 return $entries;
106         }
107
108         /**
109          * "Getter" for an array of all entries for given key
110          *
111          * @param       $lookFor        The key to look for
112          * @return      $entry          An array with all keys
113          */
114         public function getArrayFromKey ($lookFor) {
115                 // Init array
116                 $entry = array();
117
118                 // "Walk" over all entries
119                 foreach ($this->getEntries('object-name') as $key=>$value) {
120                         // If $value matches the $lookFor, we need to look for more entries for this!
121                         if ($lookFor == $value) {
122                                 // Look for more entries
123                                 foreach ($this->getEntries() as $key2=>$value2) {
124                                         // Both keys must match!
125                                         if ($key == $key2) {
126                                                 // Then add it
127                                                 $entry[$key2] = $value2[$key];
128                                         } // END - if
129                                 } // END - foreach
130
131                                 // Skip further lookups
132                                 break;
133                         } // END - if
134                 } // END - foreach
135
136                 // Return it
137                 return $entry;
138         }
139
140         /**
141          * Gets a registered instance or null if not found
142          *
143          * @param       $instanceKey            The key to identify the instance
144          * @return      $objectInstance         An instance we shall store
145          * @throws      NullPointerException    If the requested key is not found
146          */
147         public function getInstance ($instanceKey) {
148                 // By default the instance is not in registry
149                 $objectInstance = null;
150
151                 // Is the instance there?
152                 if ($this->instanceExists($instanceKey)) {
153                         $objectInstance = $this->instanceRegistry[$instanceKey];
154                 } // END - if
155
156                 // Still not fetched?
157                 if (is_null($objectInstance)) {
158                         // This might happen if a non-registered key was requested
159                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
160                 } // END - if
161
162                 // Return the result
163                 return $objectInstance;
164         }
165 }
166
167 // [EOF]
168 ?>