3 * A registry for several data types and objects. Objects should be added by
4 * addInstance() and therefore must implement the interface Registerable.
6 * @author Roland Haeder <webmaster@ship-simu.org>
8 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
9 * @license GNU GPL 3.0 or any newer version
10 * @link http://www.ship-simu.org
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.
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.
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/>.
25 class Registry extends BaseFrameworkSystem implements Register {
27 * Instance of this class
29 private static $registryInstance = null;
32 * Wether the registry is initialized
34 private static $initialized = false;
39 private $instanceRegistry = array();
42 * Protected constructor
46 protected function __construct () {
47 // Call parent constructor
48 parent::__construct(__CLASS__);
52 * Singleton getter for self instance. This class has no factory pattern
53 * because here is no need for special parameters.
55 * @return $registryInstance Instance of this class
57 public final static function getRegistry () {
58 // Is an instance there?
59 if (is_null(self::$registryInstance)) {
60 // Not yet, so create one
61 self::$registryInstance = new Registry();
64 // Return the instance
65 return self::$registryInstance;
69 * Checks or sets wether the registry has been initialized. This had only
72 * @param $initialized Wether the registry is initialized
73 * @return $initialized Wether the registry is initialized
75 public final static function isInitialized ($initialized = null) {
76 // If no parameter (null by default) is provided we want to get the
77 // parameter. If set to e.g. 'OK' then the registry is set to
79 if (is_null($initialized)) {
80 // Get status if initialized
81 return self::$initialized;
83 // Registry is initialized!
84 self::$initialized = true;
89 * Checks wether an instance key was found
91 * @param $instanceKey The key holding an instance in registry
92 * @return $exists Wether the key exists in registry
94 public function instanceExists ($instanceKey) {
95 // Does this key exists?
96 $exists = (isset($this->instanceRegistry[$instanceKey]));
103 * Adds/overwrites a new instance to the registry at the given key
105 * @param $instanceKey The key to identify the instance
106 * @param $objectInstance An instance we shall store
109 public function addInstance ($instanceKey, Registerable $objectInstance) {
110 $this->instanceRegistry[$instanceKey] = $objectInstance;
114 * Gets a registered instance or null if not found
116 * @param $instanceKey The key to identify the instance
117 * @return $objectInstance An instance we shall store
119 public function getInstance ($instanceKey) {
120 // By default the instance is not in registry
121 $objectInstance = null;
123 // Is the instance there?
124 if ($this->instanceExists($instanceKey)) {
125 $objectInstance = $this->instanceRegistry[$instanceKey];
129 return $objectInstance;