3 namespace Org\Mxchange\CoreFramework\Registry;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
8 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
9 use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
12 use \IteratorAggregate;
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable, IteratorAggregate {
41 * Glue for generating a registry key
43 const REGISTRY_KEY_GLUE = '_';
46 * Protected constructor
48 * @param $className Name of the class
51 protected function __construct (string $className) {
52 // Call parent constructor
53 parent::__construct($className);
55 // Init generic arrays
56 $this->initGenericArrayGroup('registry', 'generic');
57 $this->initGenericArrayGroup('registry', 'instance');
61 * Returns an iterator for this whole registry.
63 * @param $onlyRegistries Only iterate on these sub-registry keys, default is all
64 * @return $iteratorInstance An instance of a Iterator class
66 public function getIterator (array $onlyRegistries = []) {
68 if (is_null($this->getIteratorInstance())) {
70 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('registry_iterator_class', array($this));
72 // ... and set it here
73 $this->setIteratorInstance($iteratorInstance);
76 $iteratorInstance = $this->getIteratorInstance();
79 // Init iterator instance
80 $iteratorInstance->initIterator($onlyRegistries);
83 return $iteratorInstance;
87 * Checks whether an instance key was found
89 * @param $instanceKey The key holding an instance in registry
90 * @return $exists Whether the key exists in registry
92 public function instanceExists ($instanceKey) {
93 // Does this key exists?
94 $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey);
101 * Adds/overwrites a new instance to the registry at the given key
103 * @param $instanceKey The key to identify the instance
104 * @param $objectInstance An instance we shall store
107 public function addInstance ($instanceKey, Registerable $objectInstance) {
108 $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance);
112 * Getter for whole generic registry
114 * @return $instanceRegistry The whole generic registry array
116 public final function getGenericRegistry () {
117 return $this->getGenericSubArray('registry', 'generic');
121 * Getter for whole instance registry
123 * @return $instanceRegistry The whole instance registry array
125 public final function getInstanceRegistry () {
126 return $this->getGenericSubArray('registry', 'instance');
130 * Adds a new entry to the given list name. If you want to add objects
131 * please use addInstance() and getInstance() instead.
133 * @param $key The key to identify the whole list
134 * @param $value The value to be stored
137 public final function addEntry ($key, $value) {
138 // Key must not be an array
139 assert(!is_array($key));
142 $this->pushValueToGenericArrayKey('registry', 'generic', $key, $value);
146 * Getter for entries or "sub entries"
148 * @return $entries An array with entries from this registry
150 public final function getEntries ($key = NULL) {
151 // Key must not be an array
152 assert(!is_array($key));
154 // Default is whole array
155 $entries = $this->getGenericArray('registry');
158 if (!is_null($key)) {
159 // Then use this entry
160 $entries = $this->getGenericArrayKey('registry', 'generic', $key);
168 * "Getter" for an array of all entries for given key
170 * @param $arrayKey The array (key) to look in
171 * @param $lookFor The key to look for
172 * @return $entry An array with all keys
174 public function getArrayFromKey ($arrayKey, $lookFor) {
175 // Key must not be an array
176 assert(!is_array($arrayKey));
182 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking arrayKey=' . $arrayKey . ',lookFor=' . $lookFor);
184 // "Walk" over all entries
185 foreach ($this->getEntries($arrayKey) as $key => $value) {
187 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
189 // If $value matches the $lookFor, we need to look for more entries for this!
190 if ($lookFor == $value) {
191 // Look for more entries
192 foreach ($this->getEntries() as $key2 => $value2) {
193 // Now "walk" through all entries, if an array is returned
194 if (is_array($value2)) {
196 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key2=' . $key2 . ',value2()=' . count($value2) . ',lookFor=' . $lookFor);
198 // "Walk" through all of them
199 foreach ($value2 as $key3 => $value3) {
200 // $value3 needs to be an array
201 assert(is_array($value3));
204 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',key3=' . $key3 . ',isset()=' . isset($value3[$key]) . ' ...');
206 // Both keys must match!
207 if (($key == $key3) || (isset($value3[$key]))) {
209 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Adding ' . $value3[$key] . ' ...');
212 $entry[$key3] = $value3[$key];
218 // Skip further lookups
224 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Returning entry(' . count($entry) . ')=' . print_r($entry, true));
231 * Gets a registered instance or null if not found
233 * @param $instanceKey The key to identify the instance
234 * @return $objectInstance An instance we shall store
235 * @throws NullPointerException If the requested key is not found
237 public function getInstance ($instanceKey) {
238 // Is the instance there?
239 if (!$this->instanceExists($instanceKey)) {
240 // This might happen if a non-registered key was requested
241 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
245 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
248 return $objectInstance;
252 * "Getter" for a registry key for given prefix and array. This method
253 * calls implode() to get a suitable key. This method does not care about
256 * @param $prefix Prefix for the key
257 * @param $data An array with data
258 * @return $registryKey A registry key
260 public static function getRegistryKeyFromArray ($prefix, array $data) {
261 // "Generate" the key
262 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);