<?php
// Own namespace
-namespace CoreFramework\Registry;
+namespace Org\Mxchange\CoreFramework\Registry;
// Import framework stuff
-use CoreFramework\Factory\ObjectFactory;
-use CoreFramework\Generic\NullPointerException;
-use CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
+use Org\Mxchange\CoreFramework\Generic\NullPointerException;
+use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
// Import SPL stuff
use \IteratorAggregate;
*
* @author Roland Haeder <webmaster@shipsimu.org>
* @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
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-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;
}
/**
* @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);
* @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
*
* @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;
* @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;
}
* @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);
* @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);