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) {
130 //* DEBUG: */ $this->debugOutput('REGISTRY: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
132 // If $value matches the $lookFor, we need to look for more entries for this!
133 if ($lookFor == $value) {
134 // Look for more entries
135 foreach ($this->getEntries() as $key2=>$value2) {
137 //* DEBUG: */ $this->debugOutput('REGISTRY: Checking key2=' . $key2 . ',value2=' . print_r($value2, true) . ',lookFor=' . $lookFor);
139 // Both keys must match!
140 if (($key == $key2) || (isset($value2[$key]))) {
142 //* DEBUG: */ $this->debugOutput('REGISTRY: Adding ' . $value2[$key] . ' ...');
145 $entry[$key2] = $value2[$key];
149 // Skip further lookups
159 * Gets a registered instance or null if not found
161 * @param $instanceKey The key to identify the instance
162 * @return $objectInstance An instance we shall store
163 * @throws NullPointerException If the requested key is not found
165 public function getInstance ($instanceKey) {
166 // By default the instance is not in registry
167 $objectInstance = null;
169 // Is the instance there?
170 if ($this->instanceExists($instanceKey)) {
171 $objectInstance = $this->instanceRegistry[$instanceKey];
174 // Still not fetched?
175 if (is_null($objectInstance)) {
176 // This might happen if a non-registered key was requested
177 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
181 return $objectInstance;