5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team
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 BaseRegistry extends BaseFrameworkSystem implements Registerable {
26 * Instance of this class
28 private static $registryInstance = null;
33 private $instanceRegistry = array();
36 * Raw data entries (non-objects)
38 private $rawEntries = array();
41 * Protected constructor
43 * @param $className Name of the class
46 protected function __construct ($className) {
47 // Call parent constructor
48 parent::__construct($className);
52 * Checks wether an instance key was found
54 * @param $instanceKey The key holding an instance in registry
55 * @return $exists Wether the key exists in registry
57 public function instanceExists ($instanceKey) {
58 // Does this key exists?
59 $exists = (isset($this->instanceRegistry[$instanceKey]));
66 * Adds/overwrites a new instance to the registry at the given key
68 * @param $instanceKey The key to identify the instance
69 * @param $objectInstance An instance we shall store
72 public function addInstance ($instanceKey, Registerable $objectInstance) {
73 $this->instanceRegistry[$instanceKey] = $objectInstance;
77 * Getter for whole instanceregistry
79 * @return $instanceRegistry The whole instance registry array
81 public final function getInstanceRegistry () {
82 return $this->instanceRegistry;
86 * Adds a new entry to the given list name. If you want to add objects
87 * please use addInstance() and getInstance() instead.
89 * @param $key The key to identify the whole list
90 * @param $value The value to be stored
93 public final function addEntry ($key, $value) {
95 $this->rawEntries[$key][] = $value;
99 * Getter for entries or "sub entries"
101 * @return $entries An array with entries from this registry
103 public final function getEntries ($key = null) {
104 // Default is whole array
105 $entries = $this->rawEntries;
108 if (!is_null($key)) {
109 // Then use this entry
110 $entries = $this->rawEntries[$key];
118 * "Getter" for an array of all entries for given key
120 * @param $lookFor The key to look for
121 * @return $entry An array with all keys
123 public function getArrayFromKey ($lookFor) {
127 // "Walk" over all entries
128 foreach ($this->getEntries('object-name') as $key=>$value) {
129 // If $value matches the $lookFor, we need to look for more entries for this!
130 if ($lookFor == $value) {
131 // Look for more entries
132 foreach ($this->getEntries() as $key2=>$value2) {
133 // Both keys must match!
136 $entry[$key2] = $value2[$key];
140 // Skip further lookups
150 * Gets a registered instance or null if not found
152 * @param $instanceKey The key to identify the instance
153 * @return $objectInstance An instance we shall store
154 * @throws NullPointerException If the requested key is not found
156 public function getInstance ($instanceKey) {
157 // By default the instance is not in registry
158 $objectInstance = null;
160 // Is the instance there?
161 if ($this->instanceExists($instanceKey)) {
162 $objectInstance = $this->instanceRegistry[$instanceKey];
165 // Still not fetched?
166 if (is_null($objectInstance)) {
167 // This might happen if a non-registered key was requested
168 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
172 return $objectInstance;