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 \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', [$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 (string $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 (string $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 (string $key, $value) {
139 $this->pushValueToGenericArrayKey('registry', 'generic', $key, $value);
143 * Getter for entries or "sub entries"
146 * @return $entries An array with entries from this registry
148 public final function getEntries (string $key = NULL) {
149 // Default is whole array
150 $entries = $this->getGenericArray('registry');
153 if (!is_null($key)) {
154 // Then use this entry
155 $entries = $this->getGenericArrayKey('registry', 'generic', $key);
163 * "Getter" for an array of all entries for given key
165 * @param $arrayKey The array (key) to look in
166 * @param $lookFor The key to look for
167 * @return $entry An array with all keys
169 public function getArrayFromKey (string $arrayKey, string $lookFor) {
173 // "Walk" over all entries
174 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking arrayKey=' . $arrayKey . ',lookFor=' . $lookFor);
175 foreach ($this->getEntries($arrayKey) as $key => $value) {
176 // If $value matches the $lookFor, we need to look for more entries for this!
177 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
178 if ($lookFor == $value) {
179 // Look for more entries
180 foreach ($this->getEntries() as $key2 => $value2) {
181 // Now "walk" through all entries, if an array is returned
182 if (is_array($value2)) {
183 // "Walk" through all of them
184 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key2=' . $key2 . ',value2()=' . count($value2) . ',lookFor=' . $lookFor);
185 foreach ($value2 as $key3 => $value3) {
186 // $value3 needs to be an array
187 assert(is_array($value3));
189 // Both keys must match!
190 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',key3=' . $key3 . ',isset()=' . isset($value3[$key]) . ' ...');
191 if (($key == $key3) || (isset($value3[$key]))) {
193 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Adding ' . $value3[$key] . ' ...');
194 $entry[$key3] = $value3[$key];
200 // Skip further lookups
206 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Returning entry(' . count($entry) . ')=' . print_r($entry, true));
211 * Gets a registered instance or null if not found
213 * @param $instanceKey The key to identify the instance
214 * @return $objectInstance An instance we shall store
215 * @throws NullPointerException If the requested key is not found
217 public function getInstance (string $instanceKey) {
218 // Is the instance there?
219 if (!$this->instanceExists($instanceKey)) {
220 // This might happen if a non-registered key was requested
221 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
225 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
228 return $objectInstance;
232 * "Getter" for a registry key for given prefix and array. This method
233 * calls implode() to get a suitable key. This method does not care about
236 * @param $prefix Prefix for the key
237 * @param $data An array with data
238 * @return $registryKey A registry key
240 public static function getRegistryKeyFromArray (string $prefix, array $data) {
241 // "Generate" the key
242 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);