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 * Adds a new entry to the given list name. If you want to add objects
78 * please use addInstance() and getInstance() instead.
80 * @param $key The key to identify the whole list
81 * @param $value The value to be stored
84 public final function addEntry ($key, $value) {
86 $this->rawEntries[$key][] = $value;
90 * Getter for entries or "sub entries"
92 * @return $entries An array with entries from this registry
94 public final function getEntries ($key = null) {
95 // Default is whole array
96 $entries = $this->rawEntries;
100 // Then use this entry
101 $entries = $this->rawEntries[$key];
109 * "Getter" for an array of all entries for given key
111 * @param $lookFor The key to look for
112 * @return $entry An array with all keys
114 public function getArrayFromKey ($lookFor) {
118 // "Walk" over all entries
119 foreach ($this->getEntries('object-name') as $key=>$value) {
120 // If $value matches the $lookFor, we need to look for more entries for this!
121 if ($lookFor == $value) {
122 // Look for more entries
123 foreach ($this->getEntries() as $key2=>$value2) {
124 // Both keys must match!
127 $entry[$key2] = $value2[$key];
131 // Skip further lookups
141 * Gets a registered instance or null if not found
143 * @param $instanceKey The key to identify the instance
144 * @return $objectInstance An instance we shall store
145 * @throws NullPointerException If the requested key is not found
147 public function getInstance ($instanceKey) {
148 // By default the instance is not in registry
149 $objectInstance = null;
151 // Is the instance there?
152 if ($this->instanceExists($instanceKey)) {
153 $objectInstance = $this->instanceRegistry[$instanceKey];
156 // Still not fetched?
157 if (is_null($objectInstance)) {
158 // This might happen if a non-registered key was requested
159 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
163 return $objectInstance;