3 namespace CoreFramework\Registry;
8 * @author Roland Haeder <webmaster@shipsimu.org>
10 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
11 * @license GNU GPL 3.0 or any newer version
12 * @link http://www.shipsimu.org
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 class BaseRegistry extends BaseFrameworkSystem implements Registerable {
29 * Glue for generating a registry key
31 const REGISTRY_KEY_GLUE = '_';
34 * Instance of this class
36 private static $registryInstance = NULL;
39 * Protected constructor
41 * @param $className Name of the class
44 protected function __construct ($className) {
45 // Call parent constructor
46 parent::__construct($className);
49 $this->initGenericArrayGroup('raw', 'generic');
53 * Checks whether an instance key was found
55 * @param $instanceKey The key holding an instance in registry
56 * @return $exists Whether the key exists in registry
58 public function instanceExists ($instanceKey) {
59 // Does this key exists?
60 $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey);
67 * Adds/overwrites a new instance to the registry at the given key
69 * @param $instanceKey The key to identify the instance
70 * @param $objectInstance An instance we shall store
73 public function addInstance ($instanceKey, Registerable $objectInstance) {
74 $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance);
78 * Getter for whole instance registry
80 * @return $instanceRegistry The whole instance registry array
82 public final function getInstanceRegistry () {
83 return $this->getGenericSubArray('registry', 'instance');
87 * Adds a new entry to the given list name. If you want to add objects
88 * please use addInstance() and getInstance() instead.
90 * @param $key The key to identify the whole list
91 * @param $value The value to be stored
94 public final function addEntry ($key, $value) {
95 // Key must not be an array
96 assert(!is_array($key));
99 $this->pushValueToGenericArrayKey('raw', 'generic', $key, $value);
103 * Getter for entries or "sub entries"
105 * @return $entries An array with entries from this registry
107 public final function getEntries ($key = NULL) {
108 // Key must not be an array
109 assert(!is_array($key));
111 // Default is whole array
112 $entries = $this->getGenericArray('raw');
115 if (!is_null($key)) {
116 // Then use this entry
117 $entries = $this->getGenericArrayKey('raw', 'generic', $key);
125 * "Getter" for an array of all entries for given key
127 * @param $arrayKey The array (key) to look in
128 * @param $lookFor The key to look for
129 * @return $entry An array with all keys
131 public function getArrayFromKey ($arrayKey, $lookFor) {
132 // Key must not be an array
133 assert(!is_array($arrayKey));
139 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Checking arrayKey=' . $arrayKey . ',lookFor=' . $lookFor);
141 // "Walk" over all entries
142 foreach ($this->getEntries($arrayKey) as $key => $value) {
144 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
146 // If $value matches the $lookFor, we need to look for more entries for this!
147 if ($lookFor == $value) {
148 // Look for more entries
149 foreach ($this->getEntries() as $key2 => $value2) {
150 // Now "walk" through all entries, if an array is returned
151 if (is_array($value2)) {
153 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Checking key2=' . $key2 . ',value2()=' . count($value2) . ',lookFor=' . $lookFor);
155 // "Walk" through all of them
156 foreach ($value2 as $key3 => $value3) {
157 // $value3 needs to be an array
158 assert(is_array($value3));
161 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Checking key=' . $key . ',key3=' . $key3 . ',isset()=' . isset($value3[$key]) . ' ...');
163 // Both keys must match!
164 if (($key == $key3) || (isset($value3[$key]))) {
166 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Adding ' . $value3[$key] . ' ...');
169 $entry[$key3] = $value3[$key];
175 // Skip further lookups
181 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Returning entry(' . count($entry) . ')=' . print_r($entry, TRUE));
188 * Gets a registered instance or null if not found
190 * @param $instanceKey The key to identify the instance
191 * @return $objectInstance An instance we shall store
192 * @throws NullPointerException If the requested key is not found
194 public function getInstance ($instanceKey) {
195 // Is the instance there?
196 if (!$this->instanceExists($instanceKey)) {
197 // This might happen if a non-registered key was requested
198 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
202 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
205 return $objectInstance;
209 * "Getter" for a registry key for given prefix and array. This method
210 * calls implode() to get a suitable key. This method does not care about
213 * @param $prefix Prefix for the key
214 * @param $data An array with data
215 * @return $registryKey A registry key
217 public static function getRegistryKeyFromArray ($prefix, array $data) {
218 // "Generate" the key
219 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);