* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable, IteratorAggregate { // Load traits use IteratorTrait; /** * Glue for generating a registry key */ const REGISTRY_KEY_GLUE = '_'; /** * Protected constructor * * @param $className Name of the class * @return void */ protected function __construct (string $className) { // Call parent constructor parent::__construct($className); // Init generic arrays $this->initGenericArrayGroup('registry', 'generic'); $this->initGenericArrayGroup('registry', 'instance'); } /** * Returns an iterator for this whole registry. * * @param $onlyRegistries Only iterate on these sub-registry keys, default is all * @return $iteratorInstance An instance of a Iterator class */ public function getIterator (array $onlyRegistries = []) { // Is it set? if (is_null($this->getIteratorInstance())) { // Then instance it $iteratorInstance = ObjectFactory::createObjectByConfiguredName('registry_iterator_class', [$this]); // ... and set it here $this->setIteratorInstance($iteratorInstance); } else { // Use set iterator $iteratorInstance = $this->getIteratorInstance(); } // Init iterator instance $iteratorInstance->initIterator($onlyRegistries); // Return it return $iteratorInstance; } /** * Checks whether an instance key was found * * @param $instanceKey The key holding an instance in registry * @return $exists Whether the key exists in registry */ public function instanceExists (string $instanceKey) { // Does this key exists? $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey); // Return the result return $exists; } /** * Adds/overwrites a new instance to the registry at the given key * * @param $instanceKey The key to identify the instance * @param $objectInstance An instance we shall store * @return void */ public function addInstance (string $instanceKey, Registerable $objectInstance) { // Validate parameter if (empty($instanceKey)) { // Throw IAE throw new InvalidArgumentExeption('Parameter "instanceKey" is empty'); } // Set entry in generic array $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance); } /** * Getter for whole generic registry * * @return $instanceRegistry The whole generic registry array */ public final function getGenericRegistry () { return $this->getGenericSubArray('registry', 'generic'); } /** * Getter for whole instance registry * * @return $instanceRegistry The whole instance registry array */ public final function getInstanceRegistry () { return $this->getGenericSubArray('registry', 'instance'); } /** * Adds a new entry to the given list name. If you want to add objects * please use addInstance() and getInstance() instead. * * @param $key The key to identify the whole list * @param $value The value to be stored * @return void */ public final function addEntry (string $key, $value) { // Push it $this->pushValueToGenericArrayKey('registry', 'generic', $key, $value); } /** * Getter for entries or "sub entries" * * @param $key Key * @return $entries An array with entries from this registry */ public final function getEntries (string $key = NULL) { // Default is whole array $entries = $this->getGenericArray('registry'); // Is $key set? if (!is_null($key)) { // Then use this entry $entries = $this->getGenericArrayKey('registry', 'generic', $key); } // Return the array return $entries; } /** * "Getter" for an array of all entries for given key * * @param $arrayKey The array (key) to look in * @param $lookFor The key to look for * @return $entry An array with all keys */ public function getArrayFromKey (string $arrayKey, string $lookFor) { // Init array $entry = []; // "Walk" over all entries //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking arrayKey=' . $arrayKey . ',lookFor=' . $lookFor); foreach ($this->getEntries($arrayKey) as $key => $value) { // If $value matches the $lookFor, we need to look for more entries for this! //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor); if ($lookFor == $value) { // Look for more entries foreach ($this->getEntries() as $key2 => $value2) { // Now "walk" through all entries, if an array is returned if (is_array($value2)) { // "Walk" through all of them //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key2=' . $key2 . ',value2()=' . count($value2) . ',lookFor=' . $lookFor); foreach ($value2 as $key3 => $value3) { // $value3 needs to be an array assert(is_array($value3)); // Both keys must match! //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',key3=' . $key3 . ',isset()=' . isset($value3[$key]) . ' ...'); if (($key == $key3) || (isset($value3[$key]))) { // Then add it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Adding ' . $value3[$key] . ' ...'); $entry[$key3] = $value3[$key]; } } } } // Skip further lookups break; } } // Return it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Returning entry(' . count($entry) . ')=' . print_r($entry, true)); return $entry; } /** * Gets a registered instance or null if not found * * @param $instanceKey The key to identify the instance * @return $objectInstance An instance we shall store * @throws NullPointerException If the requested key is not found */ public function getInstance (string $instanceKey) { // Is the instance there? if (!$this->instanceExists($instanceKey)) { // This might happen if a non-registered key was requested throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); } // Get the instance $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey); // Return the result return $objectInstance; } /** * "Getter" for a registry key for given prefix and array. This method * calls implode() to get a suitable key. This method does not care about * the indexes. * * @param $prefix Prefix for the key * @param $data An array with data * @return $registryKey A registry key */ public static function getRegistryKeyFromArray (string $prefix, array $data) { // "Generate" the key $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data); // Return it return $registryKey; } }