From 895fa052b855afcb198b3b592278a70d027eb054 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Tue, 24 Mar 2015 21:17:43 +0100 Subject: [PATCH] Also moved this stuff (more may come). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- inc/classes/main/lists/.htaccess | 1 + inc/classes/main/lists/class_ | 70 +++ inc/classes/main/lists/class_BaseList.php | 481 ++++++++++++++++++ inc/classes/main/lists/tasks/.htaccess | 1 + .../main/lists/tasks/class_TaskList.php | 72 +++ inc/config.php | 6 + 6 files changed, 631 insertions(+) create mode 100644 inc/classes/main/lists/.htaccess create mode 100644 inc/classes/main/lists/class_ create mode 100644 inc/classes/main/lists/class_BaseList.php create mode 100644 inc/classes/main/lists/tasks/.htaccess create mode 100644 inc/classes/main/lists/tasks/class_TaskList.php diff --git a/inc/classes/main/lists/.htaccess b/inc/classes/main/lists/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/lists/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/lists/class_ b/inc/classes/main/lists/class_ new file mode 100644 index 00000000..4e0a5e8f --- /dev/null +++ b/inc/classes/main/lists/class_ @@ -0,0 +1,70 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 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 . + */ +class ???List extends BaseList implements Listable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this class + * + * @return $listInstance An instance a Listable class + */ + public final static function create???List () { + // Get new instance + $listInstance = new ???List(); + + // Return the prepared instance + return $listInstance; + } + + /** + * "Getter" for an iterator instance of this list + * + * @return $iteratorInstance An instance of a Iterator class + * @todo 0% done + */ + public function getListIterator () { + $this->partialStub('Please implement this method.'); + } + + /** + * Clears this list by cleaning up all groups together. + * + * @return void + * @todo 0% done + */ + public function clearList () { + $this->partialStub('Please implement this method.'); + } +} + +// [EOF] +?> diff --git a/inc/classes/main/lists/class_BaseList.php b/inc/classes/main/lists/class_BaseList.php new file mode 100644 index 00000000..cdeaf33b --- /dev/null +++ b/inc/classes/main/lists/class_BaseList.php @@ -0,0 +1,481 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.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 . + */ +class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countable { + // Exception constants + const EXCEPTION_GROUP_ALREADY_ADDED = 0xf20; + const EXCEPTION_GROUP_NOT_FOUND = 0xf21; + const EXCEPTION_INVALID_HASH = 0xf22; + + /** + * List groups array + */ + private $listGroups = array(); + + /** + * List entries array + */ + private $listEntries = array(); + + /** + * List index array + */ + private $listIndex = array(); + + /** + * Protected constructor + * + * @param $className Name of the class + * @return void + */ + protected function __construct ($className) { + // Call parent constructor + parent::__construct($className); + } + + /** + * Getter for iterator instance from this list + * + * @return $iteratorInstance An instance of a Iterator class + */ + public function getIterator () { + // Get iterator from here + $iteratorInstance = $this->getIteratorInstance(); + + // Is the instance set? + if (is_null($iteratorInstance)) { + // Prepare a default iterator + $iteratorInstance = ObjectFactory::createObjectByConfiguredName('default_iterator_class', array($this)); + + // Set it here + $this->setIteratorInstance($iteratorInstance); + } // END - if + + // And return it + return $iteratorInstance; + } + + /** + * Checks whether the given group is set + * + * @param $groupName Group to check if found in list + * @return $isset Whether the group is valid + */ + public function isGroupSet ($groupName) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName); + return isset($this->listGroups[$groupName]); + } + + /** + * Adds the given group or if already added issues a ListGroupAlreadyAddedException + * + * @param $groupName Group to add + * @return void + * @throws ListGroupAlreadyAddedException If the given group is already added + */ + public function addGroup ($groupName) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!'); + // Is the group already added? + if ($this->isGroupSet($groupName)) { + // Throw the exception here + throw new ListGroupAlreadyAddedException(array($this, $groupName), self::EXCEPTION_GROUP_ALREADY_ADDED); + } // END - if + + // Add the group which is a simple array + $this->listGroups[$groupName] = ObjectFactory::createObjectByConfiguredName('list_group_class'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!'); + } + + /** + * Adds the given instance to list group and sub group + * + * @param $groupName Group to add instance to + * @param $subGroup Sub group to add instance to + * @param $visitableInstance An instance of Visitable + * @return void + * @throws NoListGroupException If the given group is not found + */ + public function addInstance ($groupName, $subGroup, Visitable $visitableInstance) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ',visitableInstance=' . $visitableInstance->__toString() . ' - CALLED!'); + + // Is the group there? + if (!$this->isGroupSet($groupName)) { + // Throw the exception here + throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND); + } // END - if + + // Is the sub group there? + if (!$this->listGroups[$groupName]->isGroupSet($subGroup)) { + // Automatically add it + $this->listGroups[$groupName]->addGroup($subGroup); + } // END - if + + // Generate the hash + $hash = $this->generateHash($groupName, $subGroup, $visitableInstance); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ',hash=' . $hash . ' - Calling addEntry() ...'); + + // Now add it to the group list and hash it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',this->listGroups[' . $groupName . ']=' . $this->listGroups[$groupName]->__toString()); + //$this->listGroups[$groupName]->addEntry($subGroup, $hash); + + // Add the hash to the index + array_push($this->listIndex, $hash); + + // Add the instance itself to the list + $this->listEntries[$hash] = $visitableInstance; + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ' - EXIT!'); + } + + /** + * Gets an array from given list + * + * @param $list The requested list + * @return $array The requested array + * @throws NoListGroupException If the given group is not found + */ + public final function getArrayFromList ($list) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ' - CALLED!'); + + // Is the group there? + if ((!is_null($list)) && (!$this->isGroupSet($list))) { + // Throw the exception here + throw new NoListGroupException(array($this, $list), self::EXCEPTION_GROUP_NOT_FOUND); + } // END - if + + // Init array + $array = array(); + + // Is there another list? + if (!is_null($list)) { + // Then get it as well + $array = $this->listGroups[$list]->getArrayFromList(NULL); + } // END - if + + // Walk through all entries + foreach ($this->listIndex as $hash) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash); + + // Is the list entry set? + if ($this->isHashValid($hash)) { + // Add it + array_push($array, $this->listEntries[$hash]); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash . ',array(' . count($array) . ')=' . print_r($array, TRUE) . ' - ADDED!'); + } // END - if + } // END - foreach + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ',array()=' . count($array) . ' - EXIT!'); + + // Return it + return $array; + } + + /** + * Adds the given entry to list group + * + * @param $groupName Group to add instance to + * @param $entry An entry of any type + * @return void + * @throws NoListGroupException If the given group is not found + */ + public function addEntry ($groupName, $entry) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!'); + + // Is the group already added? + if (!$this->isGroupSet($groupName)) { + // Throw the exception here + throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND); + } // END - if + + // Generate hash + $hash = $this->generateHash($groupName, $groupName, $entry); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . print_r($entry, TRUE) . ', hash=' . $hash); + + // Add the hash to the index + array_push($this->listIndex, $hash); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries)); + + // Now add the entry to the list + $this->listEntries[$hash] = $entry; + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries) . ' - EXIT!'); + } + + /** + * Removes given entry from the list group + * + * @param $groupName Group where we should remove the entry from + * @param $entry The entry we should remove + * @return void + * @throws NoListGroupException If the given group is not found + */ + public function removeEntry ($groupName, $entry) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!'); + + // Is the group already added? + if (!$this->isGroupSet($groupName)) { + // Throw the exception here + throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND); + } // END - if + + // Generate hash + $hash = $this->generateHash($groupName, $groupName, $entry); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . $entry . ', hash=' . $hash); + + // Remove it from the list ... + unset($this->listEntries[$hash]); + + // ... and hash list as well + unset($this->listIndex[array_search($hash, $this->listIndex)]); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!'); + } + + /** + * Generates a hash from given group, sub group and entry + * + * @param $groupName Group to add instance to + * @param $subGroup Sub group to add instance to + * @param $entry An entry of any type + * @return $hash The generated + */ + private function generateHash ($groupName, $subGroup, $entry) { + // Created entry, 'null' is default + $entry2 = 'null'; + + // Determine type of entry + if (is_null($entry)) { + // Ignore this + } elseif ($entry instanceof FrameworkInterface) { + // Own instance detected + $entry2 = $entry->hashCode(); + } elseif ((is_int($entry)) || (is_float($entry)) || (is_resource($entry))) { + // Integer/float/resource detected + $entry2 = gettype($entry) . ':' . $entry; + } elseif (is_string($entry)) { + // String found + $entry2 = crc32($entry) . ':' . strlen($entry); + } elseif ((is_array($entry)) && (isset($entry['id']))) { + // Supported array found + $entry2 = crc32($entry['id']) . ':' . count($entry); + } elseif ((is_array($entry)) && (isset($entry[BasePool::SOCKET_ARRAY_RESOURCE])) && (isset($entry[BasePool::SOCKET_ARRAY_CONN_TYPE]))) { + // Is a socket resource array + $entry2 = crc32($entry[BasePool::SOCKET_ARRAY_RESOURCE] . ':' . $entry[BasePool::SOCKET_ARRAY_CONN_TYPE]); + } else { + // Unsupported type detected + self::createDebugInstance(__CLASS__)->debugOutput('BASE-LIST[' . __METHOD__ . ':' . __LINE__ . ']: Entry type ' . gettype($entry) . ' is unsupported.'); + + // @TODO Extend this somehow? + $entry2 = gettype($entry); + } + + // Construct string which we shall hash + $hashString = $groupName . ':' . $subGroup . ':' . $entry2; + + // Hash it with fastest hasher + $hash = crc32($hashString); + + // And return it + return $hash; + } + + /** + * Clears an array of groups, all are being checked for existence + * + * @param $groupNames An array with existing list groups + * @return void + */ + protected function clearGroups (array $groupNames) { + // Walk through all groups + foreach ($groupNames as $groupName) { + // Clear this group + $this->clearGroup($groupName); + } // END - foreach + } + + /** + * Clears a single group by resetting it to its initial state (empty array) + * + * @param $groupName Name of an existing group to clear + * @return void + */ + protected function clearGroup ($groupName) { + // Does this group exist? + if (!$this->isGroupSet($groupName)) { + // Throw the exception here + throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND); + } // END - if + + // Then clear this group list + $this->listGroups[$groupName]->clearList(); + + // Clear this list + $this->listIndex = array(); + $this->listEntries = array(); + } + + /** + * Counts all entries in this list + * + * @return $count All entries in this list + */ + public final function count () { + return count($this->listIndex); + } + + /** + * Checks whether the given hash is valid + * + * @param $hash The hash we should validate + * @return $isValid Whether the given hash is valid + */ + public final function isHashValid ($hash) { + // Check it + $isValid = ((in_array($hash, $this->listIndex)) && (isset($this->listEntries[$hash]))); + + // Return the result + return $isValid; + } + + /** + * Getter for hash from given hash index + * + * @param $hashIndex Index holding the hash + * @return $hash The hash + */ + public final function getHash ($hashIndex) { + // Get it ... + $hash = $this->listIndex[$hashIndex]; + + // ... and return it + return $hash; + } + + /** + * Gets an entry from given hash index + * + * @param $hashIndex The hash index to resolve the mapped entry + * @return $entry Solved entry from list + * @throws InvalidListHashException If the solved hash index is invalid + */ + public function getEntry ($hashIndex) { + // Get the hash value + $hash = $this->getHash($hashIndex); + + // Is the hash valid? + if (!$this->isHashValid($hash)) { + // Throw an exception here + throw new InvalidListHashException(array($this, $hash, $hashIndex), self::EXCEPTION_INVALID_HASH); + } // END - if + + // Now copy the entry + $entry = $this->listEntries[$hash]; + + // Return it + return $entry; + } + + /** + * Gets a full array from given group name + * + * @param $groupName The group name to get a list for + * @return $entries The array with all entries + * @throws NoListGroupException If the specified group is invalid + */ + public function getArrayFromProtocolInstance ($groupName) { + // Is the group valid? + if (!$this->isGroupSet($groupName)) { + // Throw the exception here + throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND); + } // END - if + + // Init the entries' array + $entries = array(); + + // Get an iterator + $iteratorInstance = $this->listGroups[$groupName]->getIterator(); + + // Rewind the iterator for this round + $iteratorInstance->rewind(); + + // Go through all entries + while ($iteratorInstance->valid()) { + // Get key + $entryIndex = $iteratorInstance->key(); + + // ... and the final entry which is the stored instance + $entry = $this->getEntry($entryIndex); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('LIST: Adding entry ' . $entry . ' ...'); + + // Add it to the list + $entries[$iteratorInstance->current()] = $entry; + + // Skip to next one + $iteratorInstance->next(); + } // END - while + + // Return the list + return $entries; + } + + /** + * Updates the given entry by hash with given array + * + * @param $hash Hash for this entry + * @param $entryArray Array with entry we should update + * @return void + * @throws InvalidListHashException If the solved hash index is invalid + */ + public function updateCurrentEntryByHash ($hash, array $entryArray) { + // Is the hash valid? + if (!$this->isHashValid($hash)) { + // Throw an exception here, hashIndex is unknown at this point + throw new InvalidListHashException(array($this, $hash, -999), self::EXCEPTION_INVALID_HASH); + } // END - if + + // Set the entry + $this->listEntries[$hash] = $entryArray; + } +} + +// [EOF] +?> diff --git a/inc/classes/main/lists/tasks/.htaccess b/inc/classes/main/lists/tasks/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/lists/tasks/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/lists/tasks/class_TaskList.php b/inc/classes/main/lists/tasks/class_TaskList.php new file mode 100644 index 00000000..5e3fc8fd --- /dev/null +++ b/inc/classes/main/lists/tasks/class_TaskList.php @@ -0,0 +1,72 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.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 . + */ +class TaskList extends BaseList implements Listable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this class + * + * @return $listInstance An instance a Listable class + */ + public static final function createTaskList () { + // Get new instance + $listInstance = new TaskList(); + + // Add tasks group + $listInstance->addGroup('tasks'); + + // Return the prepared instance + return $listInstance; + } + + /** + * "Getter" for an iterator instance of this list (not implemented) + * + * @return $iteratorInstance An instance of a Iterator class + */ + public function getListIterator () { + $this->debugInstance($this->__toString() . ' uses the default iterator. Please call getIterator() instead!'); + } + + /** + * Clears this list by cleaning up all groups together. + * + * @return void + */ + public function clearList () { + // Clear the only one group + $this->clearGroup('tasks'); + } +} + +// [EOF] +?> diff --git a/inc/config.php b/inc/config.php index 210eab3d..80cefbf8 100644 --- a/inc/config.php +++ b/inc/config.php @@ -404,5 +404,11 @@ $cfg->setConfigEntry('stack_file_class', 'StackFile'); // CFG: INDEX-FILE-CLASS $cfg->setConfigEntry('index_file_class', 'IndexFile'); +// CFG: TASK-HANDLER-CLASS +$cfg->setConfigEntry('task_handler_class', 'TaskHandler'); + +// CFG: TASK-LIST-CLASS +$cfg->setConfigEntry('task_list_class', 'TaskList'); + // [EOF] ?> -- 2.39.2