3 * A registry for several data types
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class Registry extends BaseFrameworkSystem implements Register {
26 * Instance of this class
28 private static $selfInstance = null;
31 * Wether the registry is initialized
33 private static $initialized = false;
38 private $instanceRegistry = array();
41 * Protected constructor
45 protected function __construct () {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
49 // Set part description
50 $this->setObjectDescription("Registry class");
52 // Create unique ID number
53 $this->generateUniqueId();
56 $this->removeNumberFormaters();
57 $this->removeSystemArray();
61 * Singleton getter for self instance
63 * @return $selfInstance Instance of this class
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();
72 // Return the instance
73 return self::$selfInstance;
77 * Checks or sets wether the registry has been initialized. This had only be done once
79 * @param $initialized Wether the registry is initialized
80 * @return $initialized Wether the registry is initialized
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;
93 * Checks wether an instance key was found
95 * @param $instanceKey The key holding an instance in the registry
96 * @return $exists Wether the key exists in the registry
98 public function instanceExists ($instanceKey) {
99 // Does this key exists?
100 $exists = (isset($this->instanceRegistry[$instanceKey]));
107 * Adds/overwrites a new instance to the registry at the given key
109 * @param $instanceKey The key to identify the instance
110 * @param $objectInstance An instance we shall store
113 public function addInstance ($instanceKey, Registerable $objectInstance) {
114 $this->instanceRegistry[$instanceKey] = $objectInstance;
118 * Gets a registered instance or null if not found
120 * @param $instanceKey The key to identify the instance
121 * @return $objectInstance An instance we shall store
123 public function getInstance ($instanceKey) {
124 // By default the instance is not in the registry
125 $objectInstance = null;
127 // Is the instance there?
128 if ($this->instanceExists($instanceKey)) {
129 $objectInstance = $this->instanceRegistry[$instanceKey];
133 return $objectInstance;