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->pushValueToGenericArrayElement('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) {
90 $this->pushValueToGenericArrayElement('raw', 'generic', $key, $value);
94 * Getter for entries or "sub entries"
96 * @return $entries An array with entries from this registry
98 public final function getEntries ($key = NULL) {
99 // Default is whole array
100 $entries = $this->getGenericArray('raw');
103 if (!is_null($key)) {
104 // Then use this entry
105 $entries = $this->getGenericArrayKey('raw', 'generic', $key);
113 * "Getter" for an array of all entries for given key
115 * @param $arrayKey The array (key) to look in
116 * @param $lookFor The key to look for
117 * @return $entry An array with all keys
119 public function getArrayFromKey ($arrayKey, $lookFor) {
123 // "Walk" over all entries
124 foreach ($this->getEntries($arrayKey) as $key => $value) {
126 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
128 // If $value matches the $lookFor, we need to look for more entries for this!
129 if ($lookFor == $value) {
130 // Look for more entries
131 foreach ($this->getEntries() as $key2 => $value2) {
133 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key2=' . $key2 . ',value2=' . print_r($value2, true) . ',lookFor=' . $lookFor);
135 // Both keys must match!
136 if (($key == $key2) || (isset($value2[$key]))) {
138 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Adding ' . $value2[$key] . ' ...');
141 $entry[$key2] = $value2[$key];
145 // Skip further lookups
155 * Gets a registered instance or null if not found
157 * @param $instanceKey The key to identify the instance
158 * @return $objectInstance An instance we shall store
159 * @throws NullPointerException If the requested key is not found
161 public function getInstance ($instanceKey) {
162 // Is the instance there?
163 if (!$this->instanceExists($instanceKey)) {
164 // This might happen if a non-registered key was requested
165 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
169 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
172 return $objectInstance;
176 * "Getter" for a registry key for given prefix and array. This method
177 * calls implode() to get a suitable key. This method does not care about
180 * @param $prefix Prefix for the key
181 * @param $data An array with data
182 * @return $registryKey A registry key
184 public static function getRegistryKeyFromArray ($prefix, array $data) {
185 // "Generate" the key
186 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);