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;
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 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 parent::__construct($className);
57 // Init generic arrays
58 $this->initGenericArrayGroup('registry', 'generic');
59 $this->initGenericArrayGroup('registry', 'instance');
63 * Returns an iterator for this whole registry.
65 * @param $onlyRegistries Only iterate on these sub-registry keys, default is all
66 * @return $iteratorInstance An instance of a Iterator class
68 public function getIterator (array $onlyRegistries = []) {
70 if (is_null($this->getIteratorInstance())) {
72 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('registry_iterator_class', [$this]);
74 // ... and set it here
75 $this->setIteratorInstance($iteratorInstance);
78 $iteratorInstance = $this->getIteratorInstance();
81 // Init iterator instance
82 $iteratorInstance->initIterator($onlyRegistries);
85 return $iteratorInstance;
89 * Checks whether an instance key was found
91 * @param $instanceKey The key holding an instance in registry
92 * @return $exists Whether the key exists in registry
94 public function instanceExists (string $instanceKey) {
95 // Does this key exists?
96 $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey);
103 * Adds/overwrites a new instance to the registry at the given key
105 * @param $instanceKey The key to identify the instance
106 * @param $objectInstance An instance we shall store
109 public function addInstance (string $instanceKey, Registerable $objectInstance) {
110 // Validate parameter
111 if (empty($instanceKey)) {
113 throw new InvalidArgumentExeption('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
116 // Set entry in generic array
117 $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance);
121 * Getter for whole generic registry
123 * @return $instanceRegistry The whole generic registry array
125 public final function getGenericRegistry () {
126 return $this->getGenericSubArray('registry', 'generic');
130 * Getter for whole instance registry
132 * @return $instanceRegistry The whole instance registry array
134 public final function getInstanceRegistry () {
135 return $this->getGenericSubArray('registry', 'instance');
139 * Adds a new entry to the given list name. If you want to add objects
140 * please use addInstance() and getInstance() instead.
142 * @param $key The key to identify the whole list
143 * @param $value The value to be stored
146 public final function addEntry (string $key, $value) {
148 $this->pushValueToGenericArrayKey('registry', 'generic', $key, $value);
152 * Getter for entries or "sub entries"
155 * @return $entries An array with entries from this registry
157 public final function getEntries (string $key = NULL) {
158 // Default is whole array
159 $entries = $this->getGenericArray('registry');
162 if (!is_null($key)) {
163 // Then use this entry
164 $entries = $this->getGenericArrayKey('registry', 'generic', $key);
172 * "Getter" for an array of all entries for given key
174 * @param $arrayKey The array (key) to look in
175 * @param $lookFor The key to look for
176 * @return $entry An array with all keys
178 public function getArrayFromKey (string $arrayKey, string $lookFor) {
182 // "Walk" over all entries
183 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking arrayKey=' . $arrayKey . ',lookFor=' . $lookFor);
184 foreach ($this->getEntries($arrayKey) as $key => $value) {
185 // If $value matches the $lookFor, we need to look for more entries for this!
186 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
187 if ($lookFor == $value) {
188 // Look for more entries
189 foreach ($this->getEntries() as $key2 => $value2) {
190 // Now "walk" through all entries, if an array is returned
191 if (is_array($value2)) {
192 // "Walk" through all of them
193 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key2=' . $key2 . ',value2()=' . count($value2) . ',lookFor=' . $lookFor);
194 foreach ($value2 as $key3 => $value3) {
195 // $value3 needs to be an array
196 assert(is_array($value3));
198 // Both keys must match!
199 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',key3=' . $key3 . ',isset()=' . isset($value3[$key]) . ' ...');
200 if (($key == $key3) || (isset($value3[$key]))) {
202 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Adding ' . $value3[$key] . ' ...');
203 $entry[$key3] = $value3[$key];
209 // Skip further lookups
215 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Returning entry(' . count($entry) . ')=' . print_r($entry, true));
220 * Gets a registered instance or null if not found
222 * @param $instanceKey The key to identify the instance
223 * @return $objectInstance An instance we shall store
224 * @throws NullPointerException If the requested key is not found
226 public function getInstance (string $instanceKey) {
227 // Is the instance there?
228 if (!$this->instanceExists($instanceKey)) {
229 // This might happen if a non-registered key was requested
230 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
234 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
237 return $objectInstance;
241 * "Getter" for a registry key for given prefix and array. This method
242 * calls implode() to get a suitable key. This method does not care about
245 * @param $prefix Prefix for the key
246 * @param $data An array with data
247 * @return $registryKey A registry key
249 public static function getRegistryKeyFromArray (string $prefix, array $data) {
250 // "Generate" the key
251 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);