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\Object\BaseFrameworkSystem;
9 use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
12 use \InvalidArgumentExeption;
13 use \IteratorAggregate;
14 use \UnexpectedValueException;
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable, IteratorAggregate {
43 * Glue for generating a registry key
45 const REGISTRY_KEY_GLUE = '_';
48 * Protected constructor
50 * @param $className Name of the class
53 protected function __construct (string $className) {
54 // Call parent constructor
55 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: className=%s - CONSTRUCTED!', $className));
56 parent::__construct($className);
58 // Init generic arrays
59 $this->initGenericArrayGroup('registry', 'generic');
62 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-REGISTRY: EXIT!');
66 * Returns an iterator for this whole registry.
68 * @param $onlyRegistries Only iterate on these sub-registry keys, default is all
69 * @return $iteratorInstance An instance of a Iterator class
71 public function getIterator (array $onlyRegistries = []) {
73 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: onlyRegistries()=%d - CALLED!', count($onlyRegistries)));
74 $iteratorInstance = $this->getIteratorInstance();
77 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: iteratorInstance[]=%s', gettype($iteratorInstance)));
78 if (is_null($iteratorInstance)) {
80 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('registry_iterator_class', [$this]);
82 // ... and set it here
83 $this->setIteratorInstance($iteratorInstance);
86 // Init iterator instance
87 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking iteratorInstance->initIterator(onlyRegistries()=%d) ...', count($onlyRegistries)));
88 $iteratorInstance->initIterator($onlyRegistries);
91 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: iteratorInstance=%s - EXIT!', $iteratorInstance->__toString()));
92 return $iteratorInstance;
96 * Getter for whole generic registry
98 * @return $instanceRegistry The whole generic registry array
100 public final function getGenericRegistry () {
101 return $this->getGenericSubArray('registry', 'generic');
105 * Adds a new entry to the given list name. If you want to add objects
106 * please use addInstance() and getInstance() instead.
108 * @param $key The key to identify the whole list
109 * @param $value The value to be stored
111 * @throws InvalidArgumentException If a paramter has an invalid value
113 public final function addEntry (string $key, $value) {
115 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: key=%s,value[]=%s - CALLED!', $key, gettype($value)));
118 throw new InvalidArgumentExeption('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
122 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking this->pushValueToGenericArrayKey(registry,generic,%s,value[]=%s) ...', $key, gettype($value)));
123 $this->pushValueToGenericArrayKey('registry', 'generic', $key, $value);
126 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-REGISTRY: EXIT!');
130 * Getter for entries or "sub entries"
133 * @return $entries An array with entries from this registry
134 * @throws InvalidArgumentException If a paramter has an invalid value
136 public final function getEntries (string $key = NULL) {
138 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: key[%s]=%s - CALLED!', gettype($key), $key));
139 if (!is_null($key) && empty($key)) {
141 throw new InvalidArgumentExeption('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
144 // Default is whole array
145 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-REGISTRY: Invoking this->getGenericArray(registry) ...');
146 $entries = $this->getGenericArray('registry');
149 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: key[]=%s,entries()=%d', $key, count($entries)));
150 if (!is_null($key)) {
151 // Then use this entry
152 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking this->getGenericArrayKey(registry,generic,%s) ...', $key));
153 $entries = $this->getGenericArrayKey('registry', 'generic', $key);
157 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: entries()=%d - EXIT!', count($entries)));
162 * "Getter" for an array of all entries for given key
164 * @param $arrayKey The array (key) to look in
165 * @param $lookFor The key to look for
166 * @return $entry An array with all keys
167 * @throws InvalidArgumentException If a paramter has an invalid value
168 * @throws UnexpectedValueException If $value3 is not an array
170 public function getArrayFromKey (string $arrayKey, string $lookFor) {
172 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: arrayKey=%s,lookFor=%s - CALLED!', $arrayKey, $lookFor));
173 if (empty($arrayKey)) {
175 throw new InvalidArgumentExeption('Parameter "arrayKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
176 } elseif (empty($lookFor)) {
178 throw new InvalidArgumentExeption('Parameter "lookFor" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
184 // "Walk" over all entries
185 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking arrayKey=%s,lookFor=%s', $arrayKey, $lookFor));
186 foreach ($this->getEntries($arrayKey) as $key => $value) {
187 // If $value matches the $lookFor, we need to look for more entries for this!
188 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking key=%s,value=%s,lookFor=%s', $key, $value, $lookFor));
189 if ($lookFor == $value) {
190 // Look for more entries
191 foreach ($this->getEntries() as $key2 => $value2) {
192 // Now "walk" through all entries, if an array is returned
193 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: key2=%s,value2[]=%s', $key2, gettype($value2)));
194 if (is_array($value2)) {
195 // "Walk" through all of them
196 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking key2=%s,value2()=%s,lookFor=%s', $key2, count($value2), $lookFor));
197 foreach ($value2 as $key3 => $value3) {
198 // $value3 needs to be an array
199 if (!is_array($value3)) {
201 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);
204 // Both keys must match!
205 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking key=%s,key3=%s,isset()=%s ...', $key, $key3, intval(isset($value3[$key]))));
206 if (($key == $key3) || (isset($value3[$key]))) {
208 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Adding value3[%s]=%s ...', $key, $value3[$key]));
209 $entry[$key3] = $value3[$key];
215 // Skip further lookups
216 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('BASE-REGISTRY: BREAK!');
222 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: entry()=%d - EXIT!', count($entry)));
227 * "Getter" for a registry key for given prefix and array. This method
228 * calls implode() to get a suitable key. This method does not care about
231 * @param $prefix Prefix for the key
232 * @param $data An array with data
233 * @return $registryKey A registry key
235 public static function getRegistryKeyFromArray (string $prefix, array $data) {
237 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: prefix=%s,data()=%d - CALLED!', $prefix, count($data)));
238 if (empty($prefix)) {
240 throw new InvalidArgumentException('Parameter "prefix" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
241 } elseif (count($data) == 0) {
243 throw new InvalidArgumentException('Parameter "data" is an empty array', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
246 // "Generate" the key
247 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);
250 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: registryKey=%s - EXIT!', $registryKey));