5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 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 * Glue for generating a registry key
28 const REGISTRY_KEY_GLUE = '_';
31 * Instance of this class
33 private static $registryInstance = NULL;
36 * Protected constructor
38 * @param $className Name of the class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
47 * Checks whether an instance key was found
49 * @param $instanceKey The key holding an instance in registry
50 * @return $exists Whether the key exists in registry
52 public function instanceExists ($instanceKey) {
53 // Does this key exists?
54 $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey);
61 * Adds/overwrites a new instance to the registry at the given key
63 * @param $instanceKey The key to identify the instance
64 * @param $objectInstance An instance we shall store
67 public function addInstance ($instanceKey, Registerable $objectInstance) {
68 $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance);
72 * Getter for whole instance registry
74 * @return $instanceRegistry The whole instance registry array
76 public final function getInstanceRegistry () {
77 return $this->getGenericSubArray('registry', 'instance');
81 * Adds a new entry to the given list name. If you want to add objects
82 * please use addInstance() and getInstance() instead.
84 * @param $key The key to identify the whole list
85 * @param $value The value to be stored
88 public final function addEntry ($key, $value) {
89 // Key must not be an array
90 assert(!is_array($key));
93 $this->pushValueToGenericArrayElement('raw', 'generic', $key, $value);
97 * Getter for entries or "sub entries"
99 * @return $entries An array with entries from this registry
101 public final function getEntries ($key = NULL) {
102 // Key must not be an array
103 assert(!is_array($key));
105 // Default is whole array
106 $entries = $this->getGenericArray('raw');
109 if (!is_null($key)) {
110 // Then use this entry
111 $entries = $this->getGenericArrayKey('raw', 'generic', $key);
119 * "Getter" for an array of all entries for given key
121 * @param $arrayKey The array (key) to look in
122 * @param $lookFor The key to look for
123 * @return $entry An array with all keys
125 public function getArrayFromKey ($arrayKey, $lookFor) {
126 // Key must not be an array
127 assert(!is_array($arrayKey));
133 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking arrayKey=' . $arrayKey . ',lookFor=' . $lookFor);
135 // "Walk" over all entries
136 foreach ($this->getEntries($arrayKey) as $key => $value) {
138 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
140 // If $value matches the $lookFor, we need to look for more entries for this!
141 if ($lookFor == $value) {
142 // Look for more entries
143 foreach ($this->getEntries() as $key2 => $value2) {
144 // Now "walk" through all entries, if an array is returned
145 if (is_array($value2)) {
147 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key2=' . $key2 . ',value2()=' . count($value2) . ',lookFor=' . $lookFor);
149 // "Walk" through all of them
150 foreach ($value2 as $key3 => $value3) {
151 // $value3 needs to be an array
152 assert(is_array($value3));
155 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key=' . $key . ',key3=' . $key3 . ',isset()=' . isset($value3[$key]) . ' ...');
157 // Both keys must match!
158 if (($key == $key3) || (isset($value3[$key]))) {
160 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Adding ' . $value3[$key] . ' ...');
163 $entry[$key3] = $value3[$key];
169 // Skip further lookups
175 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Returning entry(' . count($entry) . ')=' . print_r($entry, TRUE));
182 * Gets a registered instance or null if not found
184 * @param $instanceKey The key to identify the instance
185 * @return $objectInstance An instance we shall store
186 * @throws NullPointerException If the requested key is not found
188 public function getInstance ($instanceKey) {
189 // Is the instance there?
190 if (!$this->instanceExists($instanceKey)) {
191 // This might happen if a non-registered key was requested
192 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
196 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
199 return $objectInstance;
203 * "Getter" for a registry key for given prefix and array. This method
204 * calls implode() to get a suitable key. This method does not care about
207 * @param $prefix Prefix for the key
208 * @param $data An array with data
209 * @return $registryKey A registry key
211 public static function getRegistryKeyFromArray ($prefix, array $data) {
212 // "Generate" the key
213 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);