]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/registry/class_BaseRegistry.php
Continued:
[core.git] / framework / main / classes / registry / class_BaseRegistry.php
index 871975af6ab3e2645dd18b18a51cd51f91212ee0..8d8a76abc8243fd4a622de0613c852b4f7a3a68e 100644 (file)
@@ -3,9 +3,10 @@
 namespace Org\Mxchange\CoreFramework\Registry;
 
 // Import framework stuff
-use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+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;
@@ -15,7 +16,7 @@ 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
  *
@@ -33,23 +34,21 @@ use \IteratorAggregate;
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 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);
 
@@ -61,13 +60,14 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg
        /**
         * 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 () {
+       public function getIterator (array $onlyRegistries = []) {
                // Is it set?
                if (is_null($this->getIteratorInstance())) {
                        // 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);
@@ -76,6 +76,9 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg
                        $iteratorInstance = $this->getIteratorInstance();
                }
 
+               // Init iterator instance
+               $iteratorInstance->initIterator($onlyRegistries);
+
                // Return it
                return $iteratorInstance;
        }
@@ -86,7 +89,7 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg
         * @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);
 
@@ -101,7 +104,7 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg
         * @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);
        }
 
@@ -131,10 +134,7 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg
         * @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('registry', 'generic', $key, $value);
        }
@@ -142,12 +142,10 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg
        /**
         * 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('registry');
 
@@ -155,7 +153,7 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg
                if (!is_null($key)) {
                        // Then use this entry
                        $entries = $this->getGenericArrayKey('registry', 'generic', $key);
-               } // END - if
+               }
 
                // Return the array
                return $entries;
@@ -168,59 +166,44 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg
         * @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;
        }
 
@@ -231,12 +214,12 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg
         * @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);
@@ -254,7 +237,7 @@ 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) {
                // "Generate" the key
                $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);