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;
38 private $instanceRegistry = array();
41 * Raw data entries (non-objects)
43 private $rawEntries = array();
46 * Protected constructor
48 * @param $className Name of the class
51 protected function __construct ($className) {
52 // Call parent constructor
53 parent::__construct($className);
57 * Checks whether an instance key was found
59 * @param $instanceKey The key holding an instance in registry
60 * @return $exists Whether the key exists in registry
62 public function instanceExists ($instanceKey) {
63 // Does this key exists?
64 $exists = (isset($this->instanceRegistry[$instanceKey]));
71 * Adds/overwrites a new instance to the registry at the given key
73 * @param $instanceKey The key to identify the instance
74 * @param $objectInstance An instance we shall store
77 public function addInstance ($instanceKey, Registerable $objectInstance) {
78 $this->instanceRegistry[$instanceKey] = $objectInstance;
82 * Getter for whole instance registry
84 * @return $instanceRegistry The whole instance registry array
86 public final function getInstanceRegistry () {
87 return $this->instanceRegistry;
91 * Adds a new entry to the given list name. If you want to add objects
92 * please use addInstance() and getInstance() instead.
94 * @param $key The key to identify the whole list
95 * @param $value The value to be stored
98 public final function addEntry ($key, $value) {
99 // Is the array there?
100 if (!isset($this->rawEntries[$key])) {
101 // Then intialize it here
102 $this->rawEntries[$key] = array();
106 array_push($this->rawEntries[$key], $value);
110 * Getter for entries or "sub entries"
112 * @return $entries An array with entries from this registry
114 public final function getEntries ($key = NULL) {
115 // Default is whole array
116 $entries = $this->rawEntries;
119 if ((!is_null($key)) && (isset($this->rawEntries[$key]))) {
120 // Then use this entry
121 $entries = $this->rawEntries[$key];
129 * "Getter" for an array of all entries for given key
131 * @param $arrayKey The array (key) to look in
132 * @param $lookFor The key to look for
133 * @return $entry An array with all keys
135 public function getArrayFromKey ($arrayKey, $lookFor) {
139 // "Walk" over all entries
140 foreach ($this->getEntries($arrayKey) as $key => $value) {
142 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
144 // If $value matches the $lookFor, we need to look for more entries for this!
145 if ($lookFor == $value) {
146 // Look for more entries
147 foreach ($this->getEntries() as $key2 => $value2) {
149 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Checking key2=' . $key2 . ',value2=' . print_r($value2, true) . ',lookFor=' . $lookFor);
151 // Both keys must match!
152 if (($key == $key2) || (isset($value2[$key]))) {
154 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __LINE__ . ']: Adding ' . $value2[$key] . ' ...');
157 $entry[$key2] = $value2[$key];
161 // Skip further lookups
171 * Gets a registered instance or null if not found
173 * @param $instanceKey The key to identify the instance
174 * @return $objectInstance An instance we shall store
175 * @throws NullPointerException If the requested key is not found
177 public function getInstance ($instanceKey) {
178 // Is the instance there?
179 if (!$this->instanceExists($instanceKey)) {
180 // This might happen if a non-registered key was requested
181 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
185 $objectInstance = $this->instanceRegistry[$instanceKey];
188 return $objectInstance;
192 * "Getter" for a registry key for given prefix and array. This method
193 * calls implode() to get a suitable key. This method does not care about
196 * @param $prefix Prefix for the key
197 * @param $data An array with data
198 * @return $registryKey A registry key
200 public static function getRegistryKeyFromArray ($prefix, array $data) {
201 // "Generate" the key
202 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);