More conventions than code added:
[shipsimu.git] / inc / classes / main / registry / class_Registry.php
1 <?php
2 /**
3  * A registry for several data types
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
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 Registry extends BaseFrameworkSystem implements Register {
25         /**
26          * Instance of this class
27          */
28         private static $selfInstance = null;
29
30         /**
31          * Wether the registry is initialized
32          */
33         private static $initialized = false;
34
35         /**
36          * Instance registry
37          */
38         private $instanceRegistry = array();
39
40         /**
41          * Protected constructor
42          *
43          * @return      void
44          */
45         protected function __construct () {
46                 // Call parent constructor
47                 parent::__construct(__CLASS__);
48
49                 // Set part description
50                 $this->setObjectDescription("Registry class");
51
52                 // Create unique ID number
53                 $this->generateUniqueId();
54
55                 // Clean up a little
56                 $this->removeNumberFormaters();
57                 $this->removeSystemArray();
58         }
59
60         /**
61          * Singleton getter for self instance
62          *
63          * @return      $selfInstance   Instance of this class
64          */
65         public final static function getRegistry () {
66                 // Is an instance there?
67                 if (is_null(self::$selfInstance)) {
68                         // Not yet, so create one
69                         self::$selfInstance = new Registry();
70                 }
71
72                 // Return the instance
73                 return self::$selfInstance;
74         }
75
76         /**
77          * Checks or sets wether the registry has been initialized. This had only be done once
78          *
79          * @param       $initialized    Wether the registry is initialized
80          * @return      $initialized    Wether the registry is initialized
81          */
82         public final static function isInitialized ($initialized = null) {
83                 if (is_null($initialized)) {
84                         // Get status if initialized
85                         return self::$initialized;
86                 } elseif (!is_null($initialized)) {
87                         // Registry is initialized!
88                         self::$initialized = true;
89                 }
90         }
91
92         /**
93          * Checks wether an instance key was found
94          *
95          * @param       $instanceKey    The key holding an instance in the registry
96          * @return      $exists                 Wether the key exists in the registry
97          */
98         public function instanceExists ($instanceKey) {
99                 // Does this key exists?
100                 $exists = (isset($this->instanceRegistry[$instanceKey]));
101
102                 // Return the result
103                 return $exists;
104         }
105
106         /**
107          * Adds/overwrites a new instance to the registry at the given key
108          *
109          * @param       $instanceKey            The key to identify the instance
110          * @param       $objectInstance         An instance we shall store
111          * @return      void
112          */
113         public function addInstance ($instanceKey, Registerable $objectInstance) {
114                 $this->instanceRegistry[$instanceKey] = $objectInstance;
115         }
116
117         /**
118          * Gets a registered instance or null if not found
119          *
120          * @param       $instanceKey            The key to identify the instance
121          * @return      $objectInstance         An instance we shall store
122          */
123         public function getInstance ($instanceKey) {
124                 // By default the instance is not in the registry
125                 $objectInstance = null;
126
127                 // Is the instance there?
128                 if ($this->instanceExists($instanceKey)) {
129                         $objectInstance = $this->instanceRegistry[$instanceKey];
130                 }
131
132                 // Return the result
133                 return $objectInstance;
134         }
135 }
136
137 // [EOF]
138 ?>