3 namespace Org\Mxchange\CoreFramework\Registry;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\Object\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 \InvalidArgumentExeption;
13 use \IteratorAggregate;
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable, IteratorAggregate {
42 * Glue for generating a registry key
44 const REGISTRY_KEY_GLUE = '_';
47 * Protected constructor
49 * @param $className Name of the class
52 protected function __construct (string $className) {
53 // Call parent constructor
54 parent::__construct($className);
56 // Init generic arrays
57 $this->initGenericArrayGroup('registry', 'generic');
58 $this->initGenericArrayGroup('registry', 'instance');
62 * Returns an iterator for this whole registry.
64 * @param $onlyRegistries Only iterate on these sub-registry keys, default is all
65 * @return $iteratorInstance An instance of a Iterator class
67 public function getIterator (array $onlyRegistries = []) {
69 if (is_null($this->getIteratorInstance())) {
71 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('registry_iterator_class', [$this]);
73 // ... and set it here
74 $this->setIteratorInstance($iteratorInstance);
77 $iteratorInstance = $this->getIteratorInstance();
80 // Init iterator instance
81 $iteratorInstance->initIterator($onlyRegistries);
84 return $iteratorInstance;
88 * Checks whether an instance key was found
90 * @param $instanceKey The key holding an instance in registry
91 * @return $exists Whether the key exists in registry
93 public function instanceExists (string $instanceKey) {
94 // Does this key exists?
95 $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey);
102 * Adds/overwrites a new instance to the registry at the given key
104 * @param $instanceKey The key to identify the instance
105 * @param $objectInstance An instance we shall store
108 public function addInstance (string $instanceKey, Registerable $objectInstance) {
109 // Validate parameter
110 if (empty($instanceKey)) {
112 throw new InvalidArgumentExeption('Parameter "instanceKey" is empty');
115 // Set entry in generic array
116 $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance);
120 * Getter for whole generic registry
122 * @return $instanceRegistry The whole generic registry array
124 public final function getGenericRegistry () {
125 return $this->getGenericSubArray('registry', 'generic');
129 * Getter for whole instance registry
131 * @return $instanceRegistry The whole instance registry array
133 public final function getInstanceRegistry () {
134 return $this->getGenericSubArray('registry', 'instance');
138 * Adds a new entry to the given list name. If you want to add objects
139 * please use addInstance() and getInstance() instead.
141 * @param $key The key to identify the whole list
142 * @param $value The value to be stored
145 public final function addEntry (string $key, $value) {
147 $this->pushValueToGenericArrayKey('registry', 'generic', $key, $value);
151 * Getter for entries or "sub entries"
154 * @return $entries An array with entries from this registry
156 public final function getEntries (string $key = NULL) {
157 // Default is whole array
158 $entries = $this->getGenericArray('registry');
161 if (!is_null($key)) {
162 // Then use this entry
163 $entries = $this->getGenericArrayKey('registry', 'generic', $key);
171 * "Getter" for an array of all entries for given key
173 * @param $arrayKey The array (key) to look in
174 * @param $lookFor The key to look for
175 * @return $entry An array with all keys
177 public function getArrayFromKey (string $arrayKey, string $lookFor) {
181 // "Walk" over all entries
182 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking arrayKey=' . $arrayKey . ',lookFor=' . $lookFor);
183 foreach ($this->getEntries($arrayKey) as $key => $value) {
184 // If $value matches the $lookFor, we need to look for more entries for this!
185 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
186 if ($lookFor == $value) {
187 // Look for more entries
188 foreach ($this->getEntries() as $key2 => $value2) {
189 // Now "walk" through all entries, if an array is returned
190 if (is_array($value2)) {
191 // "Walk" through all of them
192 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key2=' . $key2 . ',value2()=' . count($value2) . ',lookFor=' . $lookFor);
193 foreach ($value2 as $key3 => $value3) {
194 // $value3 needs to be an array
195 assert(is_array($value3));
197 // Both keys must match!
198 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',key3=' . $key3 . ',isset()=' . isset($value3[$key]) . ' ...');
199 if (($key == $key3) || (isset($value3[$key]))) {
201 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Adding ' . $value3[$key] . ' ...');
202 $entry[$key3] = $value3[$key];
208 // Skip further lookups
214 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Returning entry(' . count($entry) . ')=' . print_r($entry, true));
219 * Gets a registered instance or null if not found
221 * @param $instanceKey The key to identify the instance
222 * @return $objectInstance An instance we shall store
223 * @throws NullPointerException If the requested key is not found
225 public function getInstance (string $instanceKey) {
226 // Is the instance there?
227 if (!$this->instanceExists($instanceKey)) {
228 // This might happen if a non-registered key was requested
229 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
233 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
236 return $objectInstance;
240 * "Getter" for a registry key for given prefix and array. This method
241 * calls implode() to get a suitable key. This method does not care about
244 * @param $prefix Prefix for the key
245 * @param $data An array with data
246 * @return $registryKey A registry key
248 public static function getRegistryKeyFromArray (string $prefix, array $data) {
249 // "Generate" the key
250 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);