X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Fregistry%2Fclass_BaseRegistry.php;h=b334bfae099834a4e6eccb4dcbb507e8c2e3a279;hb=0ea73fee55f5e2a522415cdeb32c8d3ffa75abc3;hp=26fe8f9de2cfacfef2251fffc868fd31578a5098;hpb=b4a83b1583b9b3947633bbaf62b25883bfc3f9ea;p=core.git diff --git a/framework/main/classes/registry/class_BaseRegistry.php b/framework/main/classes/registry/class_BaseRegistry.php index 26fe8f9d..b334bfae 100644 --- a/framework/main/classes/registry/class_BaseRegistry.php +++ b/framework/main/classes/registry/class_BaseRegistry.php @@ -3,19 +3,22 @@ namespace Org\Mxchange\CoreFramework\Registry; // Import framework stuff -use Org\Mxchange\CoreFramework\Factory\ObjectFactory; -use Org\Mxchange\CoreFramework\Generic\NullPointerException; +use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory; +use Org\Mxchange\CoreFramework\Generic\FrameworkInterface; use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; +use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait; // Import SPL stuff +use \InvalidArgumentExeption; use \IteratorAggregate; +use \UnexpectedValueException; /** * A general Registry * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * @@ -33,29 +36,30 @@ use \IteratorAggregate; * 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 = '_'; - /** - * Instance of this class - */ - private static $registryInstance = NULL; - /** * Protected constructor * * @param $className Name of the class * @return void */ - protected function __construct ($className) { + protected function __construct (string $className) { // Call parent constructor + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: className=%s - CONSTRUCTED!', $className)); parent::__construct($className); // Init generic arrays $this->initGenericArrayGroup('registry', 'generic'); - $this->initGenericArrayGroup('registry', 'instance'); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-REGISTRY: EXIT!'); } /** @@ -65,50 +69,29 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg * @return $iteratorInstance An instance of a Iterator class */ public function getIterator (array $onlyRegistries = []) { + // Get iterator + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: onlyRegistries()=%d - CALLED!', count($onlyRegistries))); + $iteratorInstance = $this->getIteratorInstance(); + // Is it set? - if (is_null($this->getIteratorInstance())) { + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: iteratorInstance[]=%s', gettype($iteratorInstance))); + if (is_null($iteratorInstance)) { // Then instance it - $iteratorInstance = ObjectFactory::createObjectByConfiguredName('registry_iterator_class', array($this)); + $iteratorInstance = ObjectFactory::createObjectByConfiguredName('registry_iterator_class', [$this]); // ... and set it here $this->setIteratorInstance($iteratorInstance); - } else { - // Use set iterator - $iteratorInstance = $this->getIteratorInstance(); } // Init iterator instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking iteratorInstance->initIterator(onlyRegistries()=%d) ...', count($onlyRegistries))); $iteratorInstance->initIterator($onlyRegistries); // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: iteratorInstance=%s - EXIT!', $iteratorInstance->__toString())); 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 ($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 ($instanceKey, Registerable $objectInstance) { - $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance); - } - /** * Getter for whole generic registry * @@ -118,15 +101,6 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg 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. @@ -134,34 +108,53 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg * @param $key The key to identify the whole list * @param $value The value to be stored * @return void + * @throws InvalidArgumentException If a paramter has an invalid value */ - public final function addEntry ($key, $value) { - // Key must not be an array - assert(!is_array($key)); + public final function addEntry (string $key, $value) { + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: key=%s,value[]=%s - CALLED!', $key, gettype($value))); + if (empty($key)) { + // Throw IAE + throw new InvalidArgumentExeption('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } // Push it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking this->pushValueToGenericArrayKey(registry,generic,%s,value[]=%s) ...', $key, gettype($value))); $this->pushValueToGenericArrayKey('registry', 'generic', $key, $value); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-REGISTRY: EXIT!'); } /** * Getter for entries or "sub entries" * + * @param $key Key * @return $entries An array with entries from this registry + * @throws InvalidArgumentException If a paramter has an invalid value */ - public final function getEntries ($key = NULL) { - // Key must not be an array - assert(!is_array($key)); + public final function getEntries (string $key = NULL) { + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: key[%s]=%s - CALLED!', gettype($key), $key)); + if (!is_null($key) && empty($key)) { + // Throw IAE + throw new InvalidArgumentExeption('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } // Default is whole array + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-REGISTRY: Invoking this->getGenericArray(registry) ...'); $entries = $this->getGenericArray('registry'); // Is $key set? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: key[]=%s,entries()=%d', $key, count($entries))); if (!is_null($key)) { // Then use this entry + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking this->getGenericArrayKey(registry,generic,%s) ...', $key)); $entries = $this->getGenericArrayKey('registry', 'generic', $key); - } // END - if + } // Return the array + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: entries()=%d - EXIT!', count($entries))); return $entries; } @@ -171,84 +164,65 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg * @param $arrayKey The array (key) to look in * @param $lookFor The key to look for * @return $entry An array with all keys + * @throws InvalidArgumentException If a paramter has an invalid value + * @throws UnexpectedValueException If $value3 is not an array */ - public function getArrayFromKey ($arrayKey, $lookFor) { - // Key must not be an array - assert(!is_array($arrayKey)); + public function getArrayFromKey (string $arrayKey, string $lookFor) { + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: arrayKey=%s,lookFor=%s - CALLED!', $arrayKey, $lookFor)); + if (empty($arrayKey)) { + // Throw IAE + throw new InvalidArgumentExeption('Parameter "arrayKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (empty($lookFor)) { + // Throw IAE + throw new InvalidArgumentExeption('Parameter "lookFor" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } // Init array - $entry = array(); - - // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking arrayKey=' . $arrayKey . ',lookFor=' . $lookFor); + $entry = []; // "Walk" over all entries + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking arrayKey=%s,lookFor=%s', $arrayKey, $lookFor)); foreach ($this->getEntries($arrayKey) as $key => $value) { - // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor); - // If $value matches the $lookFor, we need to look for more entries for this! + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking key=%s,value=%s,lookFor=%s', $key, $value, $lookFor)); if ($lookFor == $value) { // Look for more entries foreach ($this->getEntries() as $key2 => $value2) { // Now "walk" through all entries, if an array is returned + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: key2=%s,value2[]=%s', $key2, gettype($value2))); if (is_array($value2)) { - // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key2=' . $key2 . ',value2()=' . count($value2) . ',lookFor=' . $lookFor); - // "Walk" through all of them + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking key2=%s,value2()=%s,lookFor=%s', $key2, count($value2), $lookFor)); foreach ($value2 as $key3 => $value3) { // $value3 needs to be an array - assert(is_array($value3)); - - // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',key3=' . $key3 . ',isset()=' . isset($value3[$key]) . ' ...'); + if (!is_array($value3)) { + // Throw exception + 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); + } // Both keys must match! + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking key=%s,key3=%s,isset()=%s ...', $key, $key3, intval(isset($value3[$key])))); if (($key == $key3) || (isset($value3[$key]))) { - // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Adding ' . $value3[$key] . ' ...'); - // Then add it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Adding value3[%s]=%s ...', $key, $value3[$key])); $entry[$key3] = $value3[$key]; - } // END - if - } // END - foreach - } // END - if - } // END - foreach + } + } + } + } // Skip further lookups + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('BASE-REGISTRY: BREAK!'); break; - } // END - if - } // END - foreach - - // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Returning entry(' . count($entry) . ')=' . print_r($entry, true)); + } + } // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: entry()=%d - EXIT!', count($entry))); 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 ($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); - } // END - if - - // 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 @@ -258,11 +232,22 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg * @param $data An array with data * @return $registryKey A registry key */ - public static function getRegistryKeyFromArray ($prefix, array $data) { + public static function getRegistryKeyFromArray (string $prefix, array $data) { + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: prefix=%s,data()=%d - CALLED!', $prefix, count($data))); + if (empty($prefix)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "prefix" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (count($data) == 0) { + // Throw IAE + throw new InvalidArgumentException('Parameter "data" is an empty array', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } + // "Generate" the key $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data); // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: registryKey=%s - EXIT!', $registryKey)); return $registryKey; }