All filters rewritten to throw FilterChainException
[core.git] / inc / classes / main / registry / class_Registry.php
1 <?php
2 /**
3  * A registry for several data types and objects. Objects should be added by
4  * addInstance() and therefore must implement the interface Registerable.
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0.0
8  * @copyright   Copyright (c) 2007 - 2009 Roland Haeder, this is free software
9  * @license             GNU GPL 3.0 or any newer version
10  * @link                http://www.ship-simu.org
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 class Registry extends BaseFrameworkSystem implements Register {
26         /**
27          * Instance of this class
28          */
29         private static $registryInstance = null;
30
31         /**
32          * Wether the registry is initialized
33          */
34         private static $initialized = false;
35
36         /**
37          * Instance registry
38          */
39         private $instanceRegistry = array();
40
41         /**
42          * Protected constructor
43          *
44          * @return      void
45          */
46         protected function __construct () {
47                 // Call parent constructor
48                 parent::__construct(__CLASS__);
49
50                 // Clean up a little
51                 $this->removeNumberFormaters();
52                 $this->removeSystemArray();
53         }
54
55         /**
56          * Singleton getter for self instance. This class has no factory pattern
57          * because here is no need for special parameters.
58          *
59          * @return      $registryInstance       Instance of this class
60          */
61         public final static function getRegistry () {
62                 // Is an instance there?
63                 if (is_null(self::$registryInstance)) {
64                         // Not yet, so create one
65                         self::$registryInstance = new Registry();
66                 } // END - if
67
68                 // Return the instance
69                 return self::$registryInstance;
70         }
71
72         /**
73          * Checks or sets wether the registry has been initialized. This had only
74          * be done once.
75          *
76          * @param       $initialized    Wether the registry is initialized
77          * @return      $initialized    Wether the registry is initialized
78          */
79         public final static function isInitialized ($initialized = null) {
80                 // If no parameter (null by default) is provided we want to get the
81                 // parameter. If set to e.g. "OK" then the registry is set to
82                 // "initialized".
83                 if (is_null($initialized)) {
84                         // Get status if initialized
85                         return self::$initialized;
86                 } else {
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 registry
96          * @return      $exists                 Wether the key exists in 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 registry
125                 $objectInstance = null;
126
127                 // Is the instance there?
128                 if ($this->instanceExists($instanceKey)) {
129                         $objectInstance = $this->instanceRegistry[$instanceKey];
130                 } // END - if
131
132                 // Return the result
133                 return $objectInstance;
134         }
135 }
136
137 // [EOF]
138 ?>