From b954ccaa3dbfc59ddc76e463d2980a5dc204cc9f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 15 Jul 2017 23:05:39 +0200 Subject: [PATCH] Continued: - added experimental (maybe not working) iterator for registries - callbackInstance is now generic MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../config/class_FrameworkConfiguration.php | 15 ++ .../classes/class_BaseFrameworkSystem.php | 24 +++ .../classes/iterator/class_BaseIterator.php | 5 +- .../registry/class_RegistryIterator.php | 153 ++++++++++++++++-- .../main/classes/lists/class_BaseList.php | 21 +-- .../classes/registry/class_BaseRegistry.php | 35 +++- .../interfaces/class_FrameworkInterface.php | 8 + .../main/interfaces/iterator/file/.htaccess | 1 + .../class_SeekableWritableFileIterator.php | 0 .../interfaces/iterator/registry/.htaccess | 1 + .../registry/class_IteratableRegistry.php | 44 +++++ .../interfaces/registry/class_Register.php | 41 +++++ 12 files changed, 311 insertions(+), 37 deletions(-) create mode 100644 framework/main/interfaces/iterator/file/.htaccess rename framework/main/interfaces/iterator/{ => file}/class_SeekableWritableFileIterator.php (100%) create mode 100644 framework/main/interfaces/iterator/registry/.htaccess create mode 100644 framework/main/interfaces/iterator/registry/class_IteratableRegistry.php diff --git a/framework/config/class_FrameworkConfiguration.php b/framework/config/class_FrameworkConfiguration.php index 15894ff4..17c456e7 100644 --- a/framework/config/class_FrameworkConfiguration.php +++ b/framework/config/class_FrameworkConfiguration.php @@ -49,6 +49,11 @@ class FrameworkConfiguration implements Registerable { */ private static $configInstance = NULL; + /** + * Call-back instance (unused) + */ + private $callbackInstance = NULL; + // Some constants for the configuration system const EXCEPTION_CONFIG_KEY_IS_EMPTY = 0x130; const EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND = 0x131; @@ -446,4 +451,14 @@ class FrameworkConfiguration implements Registerable { return $equals; } + /** + * Setter for call-back instance + * + * @param $callbackInstance An instance of a FrameworkInterface class + * @return void + */ + public function setCallbackInstance (FrameworkInterface $callbackInstance) { + $this->callbackInstance = $callbackInstance; + } + } diff --git a/framework/main/classes/class_BaseFrameworkSystem.php b/framework/main/classes/class_BaseFrameworkSystem.php index 69bcba95..322bd282 100644 --- a/framework/main/classes/class_BaseFrameworkSystem.php +++ b/framework/main/classes/class_BaseFrameworkSystem.php @@ -262,6 +262,11 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { */ private $registryInstance = NULL; + /** + * Call-back instance + */ + private $callbackInstance = NULL; + /** * Thousands separator */ @@ -1551,6 +1556,25 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { return $this->registryInstance; } + /** + * Setter for call-back instance + * + * @param $callbackInstance An instance of a FrameworkInterface class + * @return void + */ + public final function setCallbackInstance (FrameworkInterface $callbackInstance) { + $this->callbackInstance = $callbackInstance; + } + + /** + * Getter for call-back instance + * + * @return $callbackInstance An instance of a FrameworkInterface class + */ + protected final function getCallbackInstance () { + return $this->callbackInstance; + } + /** * Setter for command name * diff --git a/framework/main/classes/iterator/class_BaseIterator.php b/framework/main/classes/iterator/class_BaseIterator.php index a51da231..da5d6488 100644 --- a/framework/main/classes/iterator/class_BaseIterator.php +++ b/framework/main/classes/iterator/class_BaseIterator.php @@ -5,6 +5,9 @@ namespace CoreFramework\Iterator; // Import framework stuff use CoreFramework\Object\BaseFrameworkSystem; +// Import SPL stuff +use \Iterator; + /** * A general iterator * @@ -27,7 +30,7 @@ use CoreFramework\Object\BaseFrameworkSystem; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class BaseIterator extends BaseFrameworkSystem { +abstract class BaseIterator extends BaseFrameworkSystem implements Iterator { /** * Protected constructor * diff --git a/framework/main/classes/iterator/registry/class_RegistryIterator.php b/framework/main/classes/iterator/registry/class_RegistryIterator.php index 522ad934..66538f52 100644 --- a/framework/main/classes/iterator/registry/class_RegistryIterator.php +++ b/framework/main/classes/iterator/registry/class_RegistryIterator.php @@ -3,11 +3,15 @@ namespace CoreFramework\Iterator\Registry; // Import framework stuff +use CoreFramework\Generic\FrameworkInterface; +use CoreFramework\Generic\NullPointerException; use CoreFramework\Iterator\BaseIterator; use CoreFramework\Registry\Register; +use CoreFramework\Registry\Registerable; +use CoreFramework\Registry\Sub\SubRegistry; // Import SPL stuff -use \Iterator; +use \LogicException; /** * A Registry iterator @@ -31,7 +35,22 @@ use \Iterator; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class RegistryIterator extends BaseIterator implements Iterator { +class RegistryIterator extends BaseIterator implements IteratableRegistry { + /** + * All found registry keys + */ + private $registryKeys = array(); + + /** + * Current array key + */ + private $key = NULL; + + /** + * Valid status (default: not valid) + */ + private $valid = FALSE; + /** * Protected constructor * @@ -59,6 +78,121 @@ class RegistryIterator extends BaseIterator implements Iterator { return $iteratorInstance; } + /** + * Initializes this iterator by scanning over the registry for all keys. + * + * @param $callbackInstance An instance of a FrameworkInterface class to call back (optional) + * @param $criteriaKey Criteria key (optional) + * @param $criteriaMethod Method to call back (optional) + * @return void + * @throws LogicException If a registry entry does not implement Registerable + * @throws NullPointerException If criteriaKey or criteriaMethod is not set but a call-back instance is set + */ + public function initIterator (FrameworkInterface $callbackInstance = NULL, $criteriaKey = NULL, $criteriaMethod = NULL) { + // Is the call-back instance set? + if ($callbackInstance instanceof FrameworkInterface) { + // Then also criteria key and method name must be given + if (is_null($criteriaKey)) { + // Throw NPE + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } elseif (is_null($criteriaMethod)) { + // Throw NPE + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } + + // Set all + $iteratorInstance->setCallbackInstance($callbackInstance); + $iteratorInstance->setCriteriaKey($criteriaKey); + } // END - if + + // Get generic registry entries from it + $entries = $this->getRegistryInstance()->getGenericRegistry(); + + // Init registry keys array + $this->registryKeys['generic'] = array(); + + // Anything in there? + if (count($entries) > 0) { + // Debugging: + /* DEBUG-DIE: */ die(sprintf('[%s:%d]: entries=%s', __METHOD__, __LINE__, print_r($entries, TRUE))); + } // END - if + + // Get instance registry entries from it + $entries = $this->getRegistryInstance()->getInstanceRegistry(); + + // Init registry keys array + $this->registryKeys['instance'] = array(); + + // Anything in there? + if (count($entries) > 0) { + // Then run over all + foreach ($entries as $key => $entry) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s,entry[]=%s', $key, gettype($entry))); + + // Is it 'socket_registry' ? + if ($key == 'socket_registry') { + // Skip this entry + continue; + } // END - if + + // Is it an instance of a sub-registry? + if ($entry instanceof SubRegistry) { + // Get iterator from this instance + $iteratorInstance = $entry->getIterator(); + + // Debugging: + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: key=%s,iteratorInstance=%s', __METHOD__, __LINE__, $key, print_r($iteratorInstance, TRUE))); + + // Get all keys + $keys = $iteratorInstance->getRegistryKeys(); + + // Should be there + if (!isset($keys['instance'])) { + // Should not happen + throw new LogicException(sprintf('key=%s,keys[instance] is not set.', $key)); + } // END - if + + // Add all sub-registry keys to this registry keys array + $this->registryKeys['instance'][$key] = $keys['instance']; + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s,keys=%s', $key, print_r($this->registryKeys['instance'][$key], TRUE))); + + // Skip below code + continue; + } elseif (!($entry instanceof Registerable)) { + // Not registerable?! + throw new LogicException(sprintf('entry[]=%s does not implement Registerable.', gettype($entry))); + } elseif (is_null($this->key)) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: Setting key=%s ...', $key)); + + // Init key/valid + $this->key = $key; + $this->valid = TRUE; + } + + // Debugging: + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: key=%s,entry=%s', __METHOD__, __LINE__, $key, print_r($entry, TRUE))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGISTRY-ITERATOR[instance]: key=%s,entry[]=%s - Adding ...', $key, gettype($entry))); + + // Add key to array + $this->registryKeys['instance'][$key] = array(); + } // END - foreach + } // END - if + } + + /** + * Getter for all registry keys (array) + * + * @return $registryKeys Registry keys + */ + public final function getRegistryKeys () { + // Return it + return $this->registryKeys; + } + /** * Getter for current value from group or generic * @@ -80,13 +214,8 @@ class RegistryIterator extends BaseIterator implements Iterator { * @return $key Current key in iteration */ public function key () { - // Default is null - $key = null; - - $this->partialStub('Please implement this method.'); - // Return it - return $key; + return $this->key; } /** @@ -104,16 +233,18 @@ class RegistryIterator extends BaseIterator implements Iterator { * @return void */ public function rewind () { - $this->partialStub('Please implement this method.'); + // Debugging: + /* DEBUG-DIE: */ die(sprintf('[%s:%d]: this->key(%d)[%s]=%s,this->valid=%d,this->registryKeys=%s', __METHOD__, __LINE__, strlen($this->key()), gettype($this->key()), $this->key(), intval($this->valid()), print_r($this->registryKeys, TRUE))); } /** * Checks wether the current entry is valid (not at the end of the list) * - * @return void + * @return $valid Whether the current key is still valid */ public function valid () { - $this->partialStub('Please implement this method.'); + // Return flag + return $this->valid; } } diff --git a/framework/main/classes/lists/class_BaseList.php b/framework/main/classes/lists/class_BaseList.php index dfe9803a..965c9b82 100644 --- a/framework/main/classes/lists/class_BaseList.php +++ b/framework/main/classes/lists/class_BaseList.php @@ -40,11 +40,6 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab const EXCEPTION_GROUP_NOT_FOUND = 0xf21; const EXCEPTION_INVALID_HASH = 0xf22; - /** - * Call-back instance - */ - private $callbackInstance = NULL; - /** * List groups array */ @@ -71,16 +66,6 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab parent::__construct($className); } - /** - * Setter for call-back instance - * - * @param $callbackInstance An instance of a FrameworkInterface class - * @return void - */ - public final function setCallbackInstance (FrameworkInterface $callbackInstance) { - $this->callbackInstance = $callbackInstance; - } - /** * Getter for iterator instance from this list * @@ -319,12 +304,12 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab } elseif ((is_array($entry)) && (isset($entry['id']))) { // Supported array found $entry2 = crc32($entry['id']) . ':' . count($entry); - } elseif (($this->callbackInstance instanceof FrameworkInterface) && (method_exists($this->callbackInstance, 'generateListHashFromEntry'))) { + } elseif (($this->getCallbackInstance() instanceof FrameworkInterface) && (method_exists($this->getCallbackInstance(), 'generateListHashFromEntry'))) { // Call it instead - $entry2 = $this->callbackInstance->generateListHashFromEntry($entry); + $entry2 = $this->getCallbackInstance()->generateListHashFromEntry($entry); } else { // Unsupported type detected - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST[' . __METHOD__ . ':' . __LINE__ . ']: Entry type ' . gettype($entry) . ' is unsupported (this->callbackInstance=' . $this->callbackInstance . ').'); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST[' . __METHOD__ . ':' . __LINE__ . ']: Entry type ' . gettype($entry) . ' is unsupported (this->callbackInstance=' . $this->getCallbackInstance() . ').'); // At least take all keys from array $entry2 = gettype($entry) . ':' . implode(':', array_keys($entry)); diff --git a/framework/main/classes/registry/class_BaseRegistry.php b/framework/main/classes/registry/class_BaseRegistry.php index 7c79ce3b..e6be0bf5 100644 --- a/framework/main/classes/registry/class_BaseRegistry.php +++ b/framework/main/classes/registry/class_BaseRegistry.php @@ -54,8 +54,8 @@ class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable parent::__construct($className); // Init generic arrays - $this->initGenericArrayGroup('raw', 'generic'); - $this->initGenericArrayGroup('raw', 'instance'); + $this->initGenericArrayGroup('registry', 'generic'); + $this->initGenericArrayGroup('registry', 'instance'); } /** @@ -64,8 +64,20 @@ class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable * @return $iteratorInstance An instance of a Iterator class */ public function getIterator () { - // Instance + return it - return ObjectFactory::createObjectByConfiguredName('registry_iterator_class', array($this)); + // Is it set? + if (is_null($this->getIteratorInstance())) { + // Then instance it + $iteratorInstance = ObjectFactory::createObjectByConfiguredName('registry_iterator_class', array($this)); + + // ... and set it here + $this->setIteratorInstance($iteratorInstance); + } else { + // Use set iterator + $iteratorInstance = $this->getIteratorInstance(); + } + + // Return it + return $iteratorInstance; } /** @@ -93,6 +105,15 @@ class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable $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 * @@ -115,7 +136,7 @@ class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable assert(!is_array($key)); // Push it - $this->pushValueToGenericArrayKey('raw', 'generic', $key, $value); + $this->pushValueToGenericArrayKey('registry', 'generic', $key, $value); } /** @@ -128,12 +149,12 @@ class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable assert(!is_array($key)); // 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); + $entries = $this->getGenericArrayKey('registry', 'generic', $key); } // END - if // Return the array diff --git a/framework/main/interfaces/class_FrameworkInterface.php b/framework/main/interfaces/class_FrameworkInterface.php index 909832f9..cb52f268 100644 --- a/framework/main/interfaces/class_FrameworkInterface.php +++ b/framework/main/interfaces/class_FrameworkInterface.php @@ -45,6 +45,14 @@ interface FrameworkInterface { */ function isFieldSet ($fieldName); + /** + * Setter for call-back instance + * + * @param $callbackInstance An instance of a FrameworkInterface class + * @return void + */ + function setCallbackInstance (FrameworkInterface $callbackInstance); + /** * Checks whether an object equals this object. You should overwrite this * method to implement own equality checks diff --git a/framework/main/interfaces/iterator/file/.htaccess b/framework/main/interfaces/iterator/file/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/framework/main/interfaces/iterator/file/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/framework/main/interfaces/iterator/class_SeekableWritableFileIterator.php b/framework/main/interfaces/iterator/file/class_SeekableWritableFileIterator.php similarity index 100% rename from framework/main/interfaces/iterator/class_SeekableWritableFileIterator.php rename to framework/main/interfaces/iterator/file/class_SeekableWritableFileIterator.php diff --git a/framework/main/interfaces/iterator/registry/.htaccess b/framework/main/interfaces/iterator/registry/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/framework/main/interfaces/iterator/registry/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/framework/main/interfaces/iterator/registry/class_IteratableRegistry.php b/framework/main/interfaces/iterator/registry/class_IteratableRegistry.php new file mode 100644 index 00000000..335ee5f4 --- /dev/null +++ b/framework/main/interfaces/iterator/registry/class_IteratableRegistry.php @@ -0,0 +1,44 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.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 . + */ +interface IteratableRegistry extends FrameworkInterface, Iterator { + + /** + * Initializes this iterator by scanning over the registry for all keys. + * + * @return void + * @throws LogicException If a registry entry does not implement Registerable + * @throws NullPointerException If criteriaKey or criteriaMethod is not set but a call-back instance is set + */ + function initIterator (FrameworkInterface $callbackInstance = NULL, $criteriaKey = NULL, $criteriaMethod = NULL); + +} diff --git a/framework/main/interfaces/registry/class_Register.php b/framework/main/interfaces/registry/class_Register.php index 3fc6b55b..3383964d 100644 --- a/framework/main/interfaces/registry/class_Register.php +++ b/framework/main/interfaces/registry/class_Register.php @@ -45,11 +45,52 @@ interface Register extends FrameworkInterface { */ function addInstance ($instanceKey, Registerable $objectInstance); + /** + * Getter for whole generic registry + * + * @return $instanceRegistry The whole generic registry array + */ + function getGenericRegistry (); + + /** + * Getter for whole instance registry + * + * @return $instanceRegistry The whole instance registry array + */ + function getInstanceRegistry (); + + /** + * 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 + */ + function addEntry ($key, $value); + + /** + * Getter for entries or "sub entries" + * + * @return $entries An array with entries from this registry + */ + function getEntries ($key = NULL); + + /** + * "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 + */ + function getArrayFromKey ($arrayKey, $lookFor); + /** * 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 */ function getInstance ($instanceKey); -- 2.30.2