Copyright upgraded to 2010
[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, 2008 Roland Haeder, 2009, 2010 Core Developer Team
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          * Instance registry
33          */
34         private $instanceRegistry = array();
35
36         /**
37          * Protected constructor
38          *
39          * @return      void
40          */
41         protected function __construct () {
42                 // Call parent constructor
43                 parent::__construct(__CLASS__);
44         }
45
46         /**
47          * Singleton getter for self instance. This class has no factory pattern
48          * because here is no need for special parameters.
49          *
50          * @return      $registryInstance       Instance of this class
51          */
52         public final static function getRegistry () {
53                 // Is an instance there?
54                 if (is_null(self::$registryInstance)) {
55                         // Not yet, so create one
56                         self::$registryInstance = new Registry();
57                 } // END - if
58
59                 // Return the instance
60                 return self::$registryInstance;
61         }
62
63         /**
64          * Checks wether an instance key was found
65          *
66          * @param       $instanceKey    The key holding an instance in registry
67          * @return      $exists                 Wether the key exists in registry
68          */
69         public function instanceExists ($instanceKey) {
70                 // Does this key exists?
71                 $exists = (isset($this->instanceRegistry[$instanceKey]));
72
73                 // Return the result
74                 return $exists;
75         }
76
77         /**
78          * Adds/overwrites a new instance to the registry at the given key
79          *
80          * @param       $instanceKey            The key to identify the instance
81          * @param       $objectInstance         An instance we shall store
82          * @return      void
83          */
84         public function addInstance ($instanceKey, Registerable $objectInstance) {
85                 $this->instanceRegistry[$instanceKey] = $objectInstance;
86         }
87
88         /**
89          * Gets a registered instance or null if not found
90          *
91          * @param       $instanceKey            The key to identify the instance
92          * @return      $objectInstance         An instance we shall store
93          * @throws      NullPointerException    If the requested key is not found
94          */
95         public function getInstance ($instanceKey) {
96                 // By default the instance is not in registry
97                 $objectInstance = null;
98
99                 // Is the instance there?
100                 if ($this->instanceExists($instanceKey)) {
101                         $objectInstance = $this->instanceRegistry[$instanceKey];
102                 } // END - if
103
104                 // Still not fetched?
105                 if (is_null($objectInstance)) {
106                         // This might happen if a non-registered key was requested
107                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
108                 } // END - if
109
110                 // Return the result
111                 return $objectInstance;
112         }
113 }
114
115 // [EOF]
116 ?>