3 namespace Org\Mxchange\CoreFramework\Registry;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
9 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
10 use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
13 use \InvalidArgumentExeption;
14 use \IteratorAggregate;
15 use \UnexpectedValueException;
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable, IteratorAggregate {
44 * Glue for generating a registry key
46 const REGISTRY_KEY_GLUE = '_';
49 * Protected constructor
51 * @param $className Name of the class
54 protected function __construct (string $className) {
55 // Call parent constructor
56 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: className=%s - CONSTRUCTED!', $className));
57 parent::__construct($className);
59 // Init generic arrays
60 $this->initGenericArrayGroup('registry', 'generic');
61 $this->initGenericArrayGroup('registry', 'instance');
64 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-REGISTRY: EXIT!');
68 * Returns an iterator for this whole registry.
70 * @param $onlyRegistries Only iterate on these sub-registry keys, default is all
71 * @return $iteratorInstance An instance of a Iterator class
73 public function getIterator (array $onlyRegistries = []) {
75 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: onlyRegistries()=%d - CALLED!', count($onlyRegistries)));
76 $iteratorInstance = $this->getIteratorInstance();
79 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: iteratorInstance[]=%s', gettype($iteratorInstance)));
80 if (is_null($iteratorInstance)) {
82 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('registry_iterator_class', [$this]);
84 // ... and set it here
85 $this->setIteratorInstance($iteratorInstance);
88 // Init iterator instance
89 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking iteratorInstance->initIterator(onlyRegistries()=%d) ...', count($onlyRegistries)));
90 $iteratorInstance->initIterator($onlyRegistries);
93 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: iteratorInstance=%s - EXIT!', $iteratorInstance->__toString()));
94 return $iteratorInstance;
98 * Checks whether an instance key was found
100 * @param $instanceKey The key holding an instance in registry
101 * @return $exists Whether the key exists in registry
102 * @throws InvalidArgumentException If a paramter has an invalid value
104 public function instanceExists (string $instanceKey) {
106 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
107 if (empty($instanceKey)) {
109 throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
112 // Does this key exists?
113 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking this->isGenericArrayKeySet(registry, instance, %s) ...', $instanceKey));
114 $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey);
117 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: exists=%d - EXIT!', intval($exists)));
122 * Adds/overwrites a new instance to the registry at the given key
124 * @param $instanceKey The key to identify the instance
125 * @param $objectInstance An instance we shall store
127 * @throws InvalidArgumentException If a paramter has an invalid value
129 public function addInstance (string $instanceKey, Registerable $objectInstance) {
131 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: instanceKey=%s,objectInstance=%s - CALLED!', $instanceKey, $objectInstance->__toString()));
132 if (empty($instanceKey)) {
134 throw new InvalidArgumentExeption('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
137 // Set entry in generic array
138 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking this->setGenericArrayKey(registry,instance,%s,%s) ...', $instanceKey, $objectInstance->__toString()));
139 $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance);
142 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-REGISTRY: EXIT!');
146 * Getter for whole generic registry
148 * @return $instanceRegistry The whole generic registry array
150 public final function getGenericRegistry () {
151 return $this->getGenericSubArray('registry', 'generic');
155 * Getter for whole instance registry
157 * @return $instanceRegistry The whole instance registry array
159 public final function getInstanceRegistry () {
160 return $this->getGenericSubArray('registry', 'instance');
164 * Adds a new entry to the given list name. If you want to add objects
165 * please use addInstance() and getInstance() instead.
167 * @param $key The key to identify the whole list
168 * @param $value The value to be stored
170 * @throws InvalidArgumentException If a paramter has an invalid value
172 public final function addEntry (string $key, $value) {
174 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: key=%s,value[]=%s - CALLED!', $key, gettype($value)));
177 throw new InvalidArgumentExeption('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
181 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking this->pushValueToGenericArrayKey(registry,generic,%s,value[]=%s) ...', $key, gettype($value)));
182 $this->pushValueToGenericArrayKey('registry', 'generic', $key, $value);
185 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-REGISTRY: EXIT!');
189 * Getter for entries or "sub entries"
192 * @return $entries An array with entries from this registry
193 * @throws InvalidArgumentException If a paramter has an invalid value
195 public final function getEntries (string $key = NULL) {
197 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: key=%s,value[]=%s - CALLED!', $key, gettype($value)));
198 if (!is_null($key) && empty($key)) {
200 throw new InvalidArgumentExeption('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
203 // Default is whole array
204 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-REGISTRY: Invoking this->getGenericArray(registry) ...');
205 $entries = $this->getGenericArray('registry');
208 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: key[]=%s,entries()=%d', $key, count($entries)));
209 if (!is_null($key)) {
210 // Then use this entry
211 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking this->getGenericArrayKey(registry,generic,%s) ...', $key));
212 $entries = $this->getGenericArrayKey('registry', 'generic', $key);
216 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: entries()=%d - EXIT!', count($entries)));
221 * "Getter" for an array of all entries for given key
223 * @param $arrayKey The array (key) to look in
224 * @param $lookFor The key to look for
225 * @return $entry An array with all keys
226 * @throws InvalidArgumentException If a paramter has an invalid value
227 * @throws UnexpectedValueException If $value3 is not an array
229 public function getArrayFromKey (string $arrayKey, string $lookFor) {
231 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: arrayKey=%s,lookFor=%s - CALLED!', $arrayKey, $lookFor));
232 if (empty($arrayKey)) {
234 throw new InvalidArgumentExeption('Parameter "arrayKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
235 } elseif (empty($lookFor)) {
237 throw new InvalidArgumentExeption('Parameter "lookFor" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
243 // "Walk" over all entries
244 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking arrayKey=%s,lookFor=%s', $arrayKey, $lookFor));
245 foreach ($this->getEntries($arrayKey) as $key => $value) {
246 // If $value matches the $lookFor, we need to look for more entries for this!
247 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking key=%s,value=%s,lookFor=%s', $key, $value, $lookFor));
248 if ($lookFor == $value) {
249 // Look for more entries
250 foreach ($this->getEntries() as $key2 => $value2) {
251 // Now "walk" through all entries, if an array is returned
252 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: key2=%s,value2[]=%s', $key2, gettype($value2)));
253 if (is_array($value2)) {
254 // "Walk" through all of them
255 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking key2=%s,value2()=%s,lookFor=%s', $key2, count($value2), $lookFor));
256 foreach ($value2 as $key3 => $value3) {
257 // $value3 needs to be an array
258 if (!is_array($value3)) {
260 throw new UnexpectedValueException(sprintf('arrayKey=%s,key=%s,value=%s,key2=%s,key3=%s has unexpected value3[]=%s', $arrayKey, $key, $value, $key2, $key3, gettype($value3)), FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE);
263 // Both keys must match!
264 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking key=%s,key3=%s,isset()=%s ...', $key, $key3, intval(isset($value3[$key]))));
265 if (($key == $key3) || (isset($value3[$key]))) {
267 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Adding value3[%s]=%s ...', $key, $value3[$key]));
268 $entry[$key3] = $value3[$key];
274 // Skip further lookups
275 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('BASE-REGISTRY: BREAK!');
281 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: entry()=%d - EXIT!', count($entry)));
286 * Gets a registered instance or null if not found
288 * @param $instanceKey The key to identify the instance
289 * @return $objectInstance An instance we shall store
290 * @throws InvalidArgumentException If a paramter has an invalid value
291 * @throws NullPointerException If the requested key is not found
293 public function getInstance (string $instanceKey) {
295 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
296 if (empty($instanceKey)) {
298 throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
301 // Is the instance there?
302 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking this->instanceExists(%s) ...', $instanceKey));
303 if (!$this->instanceExists($instanceKey)) {
304 // This might happen if a non-registered key was requested
305 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
309 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking this->getGenericArrayKey(registry,instance,%s) ...', $instanceKey));
310 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
313 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: objectInstance=%s - EXIT!', $objectInstance->__toString()));
314 return $objectInstance;
318 * "Getter" for a registry key for given prefix and array. This method
319 * calls implode() to get a suitable key. This method does not care about
322 * @param $prefix Prefix for the key
323 * @param $data An array with data
324 * @return $registryKey A registry key
326 public static function getRegistryKeyFromArray (string $prefix, array $data) {
328 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: prefix=%s,data()=%d - CALLED!', $prefix, count($data)));
329 if (empty($prefix)) {
331 throw new InvalidArgumentException('Parameter "prefix" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
332 } elseif (count($data) == 0) {
334 throw new InvalidArgumentException('Parameter "data" is an empty array', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
337 // "Generate" the key
338 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);
341 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: registryKey=%s - EXIT!', $registryKey));