X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Fregistry%2Fclass_BaseRegistry.php;h=8d8a76abc8243fd4a622de0613c852b4f7a3a68e;hb=2218902056efcf9a2c66fe7c24995e066bd7cd11;hp=7c79ce3b3e40898ddce87bcd399c80b24b21bcc2;hpb=9038ac3be74ac32db35adceb83f40170ac123e4f;p=core.git diff --git a/framework/main/classes/registry/class_BaseRegistry.php b/framework/main/classes/registry/class_BaseRegistry.php index 7c79ce3b..8d8a76ab 100644 --- a/framework/main/classes/registry/class_BaseRegistry.php +++ b/framework/main/classes/registry/class_BaseRegistry.php @@ -1,11 +1,12 @@ * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * @@ -32,40 +33,54 @@ use \IteratorAggregate; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable, IteratorAggregate { +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 parent::__construct($className); // Init generic arrays - $this->initGenericArrayGroup('raw', 'generic'); - $this->initGenericArrayGroup('raw', 'instance'); + $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 () { - // Instance + return it - return ObjectFactory::createObjectByConfiguredName('registry_iterator_class', array($this)); + 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; } /** @@ -74,7 +89,7 @@ class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable * @param $instanceKey The key holding an instance in registry * @return $exists Whether the key exists in registry */ - public function instanceExists ($instanceKey) { + public function instanceExists (string $instanceKey) { // Does this key exists? $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey); @@ -89,10 +104,19 @@ class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable * @param $objectInstance An instance we shall store * @return void */ - public function addInstance ($instanceKey, Registerable $objectInstance) { + public function addInstance (string $instanceKey, Registerable $objectInstance) { $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 * @@ -110,31 +134,26 @@ class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable * @param $value The value to be stored * @return void */ - public final function addEntry ($key, $value) { - // Key must not be an array - assert(!is_array($key)); - + public final function addEntry (string $key, $value) { // Push it - $this->pushValueToGenericArrayKey('raw', 'generic', $key, $value); + $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 ($key = NULL) { - // Key must not be an array - assert(!is_array($key)); - + public final function getEntries (string $key = NULL) { // Default is whole array - $entries = $this->getGenericArray('raw'); + $entries = $this->getGenericArray('registry'); // Is $key set? if (!is_null($key)) { // Then use this entry - $entries = $this->getGenericArrayKey('raw', 'generic', $key); - } // END - if + $entries = $this->getGenericArrayKey('registry', 'generic', $key); + } // Return the array return $entries; @@ -147,59 +166,44 @@ class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable * @param $lookFor The key to look for * @return $entry An array with all keys */ - public function getArrayFromKey ($arrayKey, $lookFor) { - // Key must not be an array - assert(!is_array($arrayKey)); - + public function getArrayFromKey (string $arrayKey, string $lookFor) { // Init array - $entry = array(); - - // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Checking arrayKey=' . $arrayKey . ',lookFor=' . $lookFor); + $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) { - // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: 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__)->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)) { - // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Checking key2=' . $key2 . ',value2()=' . count($value2) . ',lookFor=' . $lookFor); - // "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)); - // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Checking key=' . $key . ',key3=' . $key3 . ',isset()=' . isset($value3[$key]) . ' ...'); - // 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]))) { - // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Adding ' . $value3[$key] . ' ...'); - // Then add it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Adding ' . $value3[$key] . ' ...'); $entry[$key3] = $value3[$key]; - } // END - if - } // END - foreach - } // END - if - } // END - foreach + } + } + } + } // Skip further lookups break; - } // END - if - } // END - foreach - - // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Returning entry(' . count($entry) . ')=' . print_r($entry, true)); + } + } // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Returning entry(' . count($entry) . ')=' . print_r($entry, true)); return $entry; } @@ -210,12 +214,12 @@ class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable * @return $objectInstance An instance we shall store * @throws NullPointerException If the requested key is not found */ - public function getInstance ($instanceKey) { + 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); - } // END - if + } // Get the instance $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey); @@ -233,7 +237,7 @@ class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable * @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) { // "Generate" the key $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);