From b4a01b4e5ea1ce826b6df34f4619f6310c77cd02 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 31 May 2008 18:47:51 +0000 Subject: [PATCH] More object are configureable, cache initially added --- .gitattributes | 6 ++ .../ship-simu/class_ApplicationHelper.php | 2 +- .../web/class_WebShipsimuRegisterCommand.php | 2 +- .../main/class_ClassNotFoundException.php | 10 ++- inc/classes/interfaces/cache/.htaccess | 1 + .../interfaces/cache/class_Cacheable.php | 35 ++++++++ inc/classes/main/cache/.htaccess | 1 + inc/classes/main/cache/class_MemoryCache.php | 89 +++++++++++++++++++ inc/classes/main/class_ | 4 +- .../main/controller/class_BaseController.php | 4 +- .../wrapper/class_UserDatabaseWrapper.php | 77 +++++++++++++--- inc/classes/main/factories/cache/.htaccess | 1 + .../factories/cache/class_CacheFactory.php | 80 +++++++++++++++++ .../main/factories/class_BaseFactory.php | 3 - .../factories/objects/class_ObjectFactory.php | 23 ++++- .../factories/web/class_WebNewsFactory.php | 3 + .../compressor/class_CompressorChannel.php | 4 +- .../middleware/io/class_FileIoHandler.php | 4 +- inc/config.php | 20 ++++- inc/file_io.php | 2 +- 20 files changed, 341 insertions(+), 30 deletions(-) create mode 100644 inc/classes/interfaces/cache/.htaccess create mode 100644 inc/classes/interfaces/cache/class_Cacheable.php create mode 100644 inc/classes/main/cache/.htaccess create mode 100644 inc/classes/main/cache/class_MemoryCache.php create mode 100644 inc/classes/main/factories/cache/.htaccess create mode 100644 inc/classes/main/factories/cache/class_CacheFactory.php diff --git a/.gitattributes b/.gitattributes index 5ed3f32..9a680d2 100644 --- a/.gitattributes +++ b/.gitattributes @@ -217,6 +217,8 @@ inc/classes/exceptions/user/class_UsernameMissingException.php -text inc/classes/interfaces/.htaccess -text inc/classes/interfaces/application/.htaccess -text inc/classes/interfaces/application/class_ManageableApplication.php -text +inc/classes/interfaces/cache/.htaccess -text +inc/classes/interfaces/cache/class_Cacheable.php -text inc/classes/interfaces/class_ -text inc/classes/interfaces/class_FrameworkInterface.php -text inc/classes/interfaces/commands/.htaccess -text @@ -259,6 +261,8 @@ inc/classes/interfaces/template/view/class_ViewHelper.php -text inc/classes/interfaces/user/.htaccess -text inc/classes/interfaces/user/class_ManageableUser.php -text inc/classes/main/.htaccess -text +inc/classes/main/cache/.htaccess -text +inc/classes/main/cache/class_MemoryCache.php -text inc/classes/main/class_ -text inc/classes/main/class_BaseFrameworkSystem.php -text inc/classes/main/class_FrameworkArrayObject.php -text @@ -299,6 +303,8 @@ inc/classes/main/extended/.htaccess -text inc/classes/main/extended/class_ObjectLimits.php -text inc/classes/main/extended/class_SerializationContainer.php -text inc/classes/main/factories/.htaccess -text +inc/classes/main/factories/cache/.htaccess -text +inc/classes/main/factories/cache/class_CacheFactory.php -text inc/classes/main/factories/class_BaseFactory.php -text inc/classes/main/factories/objects/.htaccess -text inc/classes/main/factories/objects/class_ObjectFactory.php -text diff --git a/application/ship-simu/class_ApplicationHelper.php b/application/ship-simu/class_ApplicationHelper.php index b925ebd..6a94eba 100644 --- a/application/ship-simu/class_ApplicationHelper.php +++ b/application/ship-simu/class_ApplicationHelper.php @@ -182,7 +182,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica */ public final function entryPoint () { // Create a new request object - $requestInstance = HttpRequest::createHttpRequest(); + $requestInstance = ObjectFactory::createObjectByName('HttpRequest'); // ... and a new response object $responseInstance = HttpResponse::createHttpResponse($this); diff --git a/application/ship-simu/main/commands/web/class_WebShipsimuRegisterCommand.php b/application/ship-simu/main/commands/web/class_WebShipsimuRegisterCommand.php index 0b41b4f..86b9526 100644 --- a/application/ship-simu/main/commands/web/class_WebShipsimuRegisterCommand.php +++ b/application/ship-simu/main/commands/web/class_WebShipsimuRegisterCommand.php @@ -59,7 +59,7 @@ class WebShipsimuRegisterCommand extends BaseCommand implements Commandable { $controllerInstance = $resolverInstance->getControllerInstance(); // @TODO Add some more pre/post filters to the controller - $controllerInstance->addPreFilter(UserNameValidatorFilter::createUserNameValidatorFilter()); + $controllerInstance->addPreFilter(ObjectFactory::createObjectByName('UserNameValidatorFilter')); // Return the prepared instance return $commandInstance; diff --git a/inc/classes/exceptions/main/class_ClassNotFoundException.php b/inc/classes/exceptions/main/class_ClassNotFoundException.php index 304d7cd..34f0ed3 100644 --- a/inc/classes/exceptions/main/class_ClassNotFoundException.php +++ b/inc/classes/exceptions/main/class_ClassNotFoundException.php @@ -25,13 +25,17 @@ class ClassNotFoundException extends FrameworkException { /** * The constructor * - * @param $message Message from the exception + * @param $msgArray Message from the exception * @param $code Code number for the exception * @return void */ - public function __construct (BaseFrameworkSystem $class, $code) { + public function __construct (array $msgArray, $code) { // Add a message around the missing class - $message = sprintf("Die interne Script-Klasse %s konnte nicht gefunden werden!", $class); + $message = sprintf("[%s:%d] Internal class %s not found.", + $msgArray[0]->__toString(), + $this->getLine(), + $msgArray[1] + ); // Call parent constructor parent::__construct($message, $code); diff --git a/inc/classes/interfaces/cache/.htaccess b/inc/classes/interfaces/cache/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/inc/classes/interfaces/cache/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/interfaces/cache/class_Cacheable.php b/inc/classes/interfaces/cache/class_Cacheable.php new file mode 100644 index 0000000..c6fa237 --- /dev/null +++ b/inc/classes/interfaces/cache/class_Cacheable.php @@ -0,0 +1,35 @@ + + * @version 0.0.0 + * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @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 Cacheable extends FrameworkInterface { + /** + * Does the specified offset exist in cache? + * + * @param $offset The offsrt we are looking for + * @return $exists Wether the offset exists + */ + function offsetExists ($offset); +} + +// [EOF] +?> diff --git a/inc/classes/main/cache/.htaccess b/inc/classes/main/cache/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/inc/classes/main/cache/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/cache/class_MemoryCache.php b/inc/classes/main/cache/class_MemoryCache.php new file mode 100644 index 0000000..8dd2537 --- /dev/null +++ b/inc/classes/main/cache/class_MemoryCache.php @@ -0,0 +1,89 @@ + + * @version 0.3.0 + * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @license GNU GPL 3.0 or any newer version + * @link http://www.mxchange.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 MemoryCache extends BaseFrameworkSystem implements Cacheable { + /** + * The "memory cache" is simply a wrapped object array + */ + private $dataCache = null; + + /** + * Private constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set part description + $this->setObjectDescription("Memory cache"); + + // Create unique ID number + $this->createUniqueID(); + + // Clean up a little + $this->removeNumberFormaters(); + $this->removeSystemArray(); + } + + /** + * Creates an instance of this class + * + * @return $cacheInstance An instance of this cache class + */ + public final static function createMemoryCache () { + // Get a new instance + $cacheInstance = new MemoryCache(); + + // Initialize the cache + $cacheInstance->initCache(); + + // Return the prepared instance + return $cacheInstance; + } + + /** + * Initialize this cache by creating an object array + * + * @return void + */ + protected function initCache () { + // Now create the "data cache" + $this->dataCache = new FrameworkArrayObject(); + } + + /** + * Does the specified offset exist in cache? + * + * @param $offset The offsrt we are looking for + * @return $exists Wether the offset exists + */ + public final function offsetExists ($offset) { + $exists = $this->dataCache->offsetExists($offset); + return $exists; + } +} + +// [EOF] +?> diff --git a/inc/classes/main/class_ b/inc/classes/main/class_ index c7a35e7..8443460 100644 --- a/inc/classes/main/class_ +++ b/inc/classes/main/class_ @@ -27,9 +27,9 @@ class extends BaseFrameworkSystem { * * @return void */ - private function __construct () { + protected function __construct () { // Call parent constructor - parent::constructor(__CLASS__); + parent::__construct(__CLASS__); // Set part description $this->setObjectDescription(""); diff --git a/inc/classes/main/controller/class_BaseController.php b/inc/classes/main/controller/class_BaseController.php index a2bf811..409ded2 100644 --- a/inc/classes/main/controller/class_BaseController.php +++ b/inc/classes/main/controller/class_BaseController.php @@ -53,8 +53,8 @@ class BaseController extends BaseFrameworkSystem { $this->removeSystemArray(); // Initialize both filter chains - $this->preFilterChain = FilterChain::createFilterChain(); - $this->postFilterChain = FilterChain::createFilterChain(); + $this->preFilterChain = ObjectFactory::createObjectByConfiguredName('filter_chain'); + $this->postFilterChain = ObjectFactory::createObjectByConfiguredName('filter_chain'); } /** diff --git a/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php b/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php index 18aabb0..f9caae5 100644 --- a/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php +++ b/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php @@ -1,6 +1,6 @@ @@ -23,6 +23,11 @@ * along with this program. If not, see . */ class UserDatabaseWrapper extends BaseDatabaseWrapper { + /** + * Cache instance + */ + private $cacheInstance = null; + // Constants const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0xe00; @@ -36,35 +41,87 @@ class UserDatabaseWrapper extends BaseDatabaseWrapper { parent::__construct(__CLASS__); // Set part description - $this->setObjectDescription("Database client for user objects"); + $this->setObjectDescription("Database wrapper for user objects"); // Create unique ID number $this->createUniqueID(); + + // Initialize the cache instance + $this->initCacheInstance(); } /** - * Creates an instance of this database client by a provided user class + * Creates an instance of this database wrapper by a provided user class * * @param $userInstance An instance of a user class - * @return $clientInstance An instance of the created client class - * @throws WrapperUserNameNotFoundException If the supplied username + * @return $wrapperInstance An instance of the created wrapper class + * @throws WrapperUserNameNotFoundException If the supplied username * does not exist */ public final static function createUserDatabaseWrapper (ManageableUser $userInstance) { // Get a new instance - $clientInstance = new UserDatabaseWrapper(); + $wrapperInstance = new UserDatabaseWrapper(); // Does the username exists? - if (!$clientInstance->ifUserNameExists($userInstance->getUserName())) { + if (!$wrapperInstance->ifUserNameExists($userInstance->getUserName())) { // Throw an exception here - throw new WrapperUserNameNotFoundException (array($clientInstance, $userInstance), self::EXCEPTION_CLIENT_USERNAME_NOT_FOUND); + throw new WrapperUserNameNotFoundException (array($wrapperInstance, $userInstance), self::EXCEPTION_CLIENT_USERNAME_NOT_FOUND); } // The user exists - $clientInstance->partialStub("Add loading of full user details"); + $wrapperInstance->partialStub("Add loading of full user details"); // Return the instance - return $clientInstance; + return $wrapperInstance; + } + + /** + * Initializes the cache instance with a new object + * + * @return void + */ + private function initCacheInstance () { + // Set the new instance + $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache(); + } + + /** + * Checks wether the given username is already used + * + * @param $userName The username we shall look up + * @return $exists Wether the username exists + */ + public function ifUserNameExists ($userName) { + // By default no entry exist + $exists = false; + + // Does this entry exist? + if ($this->cacheInstance->offsetExists($userName)) { + // Then we have a user! + $exists = true; + } else { + // Create a search criteria + $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria'); + + // Add the username as a criteria and set limit to one entry + $criteriaInstance->add(self::DB_USERNAME, $userName); + $criteriaInstance->setLimit(1); + + // Get a search result + $result = $this->doSelectByCriteria($criteriaInstance); + + // Search for it + if ($result->next()) { + // Get the result + $this->cacheInstance->add($userName, $result->getResultArray()); + + // Entry found, so all is fine + $exists = true; + } + } + + // Return the result + return $exists; } } diff --git a/inc/classes/main/factories/cache/.htaccess b/inc/classes/main/factories/cache/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/inc/classes/main/factories/cache/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/factories/cache/class_CacheFactory.php b/inc/classes/main/factories/cache/class_CacheFactory.php new file mode 100644 index 0000000..087b942 --- /dev/null +++ b/inc/classes/main/factories/cache/class_CacheFactory.php @@ -0,0 +1,80 @@ + + * @version 0.0.0 + * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @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 CacheFactory extends BaseFactory { + /** + * Singleton instance + */ + private static $selfInstance = null; + + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set object description + $this->setObjectDescription("Factory for cache objects"); + + // Create unique ID number + $this->createUniqueID(); + } + + /** + * Singleton getter for this class + * + * @return $selfInstance An instance of this class + */ + public final static function getFactory () { + // Is the instance null? + if (is_null(self::$selfInstance)) { + // Set a new one + self::$selfInstance = new CacheFactory(); + } + + // Return the instance + return self::$selfInstance; + } + + /** + * Creates a configured cache instance + * + * @return $cacheInstance An instance of the configured cache + */ + public function createConfiguredCache () { + // Read the config entry + $cacheType = $this->getConfigInstance()->readConfig("cache_type"); + + // And get a new instance + $cacheInstance = ObjectFactory::createObjectByName($cacheType); + + // Return the instance + return $cacheInstance; + } +} + +// [EOF] +?> diff --git a/inc/classes/main/factories/class_BaseFactory.php b/inc/classes/main/factories/class_BaseFactory.php index db2611c..68a856d 100644 --- a/inc/classes/main/factories/class_BaseFactory.php +++ b/inc/classes/main/factories/class_BaseFactory.php @@ -37,9 +37,6 @@ class BaseFactory extends BaseFrameworkSystem { // Call parent constructor parent::__construct($class); - // Create unique ID number - $this->createUniqueID(); - // Clean up a little $this->removeNumberFormaters(); $this->removeSystemArray(); diff --git a/inc/classes/main/factories/objects/class_ObjectFactory.php b/inc/classes/main/factories/objects/class_ObjectFactory.php index 5dda5fb..3f8ac7f 100644 --- a/inc/classes/main/factories/objects/class_ObjectFactory.php +++ b/inc/classes/main/factories/objects/class_ObjectFactory.php @@ -25,7 +25,6 @@ class ObjectFactory extends BaseFactory { /** * Protected constructor * - * @param $class Name of the real class (not BaseFactory) * @return void */ protected function __construct () { @@ -34,6 +33,9 @@ class ObjectFactory extends BaseFactory { // Set object description $this->setObjectDescription("Simple generic object factory"); + + // Create unique ID number + $this->createUniqueID(); } /** @@ -56,7 +58,7 @@ class ObjectFactory extends BaseFactory { throw new EmptyVariableException(array($factoryInstance, 'className'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING); } elseif (!class_exists($className)) { // Then throw an exception - throw new ClassNotFoundException($className, self::EXCEPTION_CLASS_NOT_FOUND); + throw new ClassNotFoundException(array($factoryInstance, $className), self::EXCEPTION_CLASS_NOT_FOUND); } // Then Prepare the eval() command @@ -71,6 +73,23 @@ class ObjectFactory extends BaseFactory { // Return the prepared instance return $objectInstance; } + + /** + * Creates an object by it's configured name + * + * @param $configEnttry Configuration entry to read + * @return $objectInstance An instance of the requested object + */ + public final static function createObjectByConfiguredName ($configEntry) { + // Read the configuration entry + $className = FrameworkConfiguration::getInstance()->readConfig($configEntry); + + // Send this to the other factory... + $objectInstance = self::createObjectByName($className); + + // Return the instance + return $objectInstance; + } } // [EOF] diff --git a/inc/classes/main/factories/web/class_WebNewsFactory.php b/inc/classes/main/factories/web/class_WebNewsFactory.php index 5a604a7..946f96b 100644 --- a/inc/classes/main/factories/web/class_WebNewsFactory.php +++ b/inc/classes/main/factories/web/class_WebNewsFactory.php @@ -38,6 +38,9 @@ class WebNewsFactory extends BaseFactory { // Set part description $this->setObjectDescription("Factory for webpages with news"); + + // Create unique ID number + $this->createUniqueID(); } /** diff --git a/inc/classes/middleware/compressor/class_CompressorChannel.php b/inc/classes/middleware/compressor/class_CompressorChannel.php index d1c80ca..e48e091 100644 --- a/inc/classes/middleware/compressor/class_CompressorChannel.php +++ b/inc/classes/middleware/compressor/class_CompressorChannel.php @@ -98,8 +98,8 @@ class CompressorChannel extends BaseMiddleware { || (!method_exists($cInstance->getCompressor(), 'compressStream')) || (!method_exists($cInstance->getCompressor(), 'decompressStream')) ) { - // Set the null compressor handler - $cInstance->setCompressor(NullCompressor::createNullCompressor()); + // Set the null compressor handler. This should not be configureable! + $cInstance->setCompressor(ObjectFactory::createObjectByName('NullCompressor')); } // Return the compressor instance diff --git a/inc/classes/middleware/io/class_FileIoHandler.php b/inc/classes/middleware/io/class_FileIoHandler.php index 16c5b87..a7322fb 100644 --- a/inc/classes/middleware/io/class_FileIoHandler.php +++ b/inc/classes/middleware/io/class_FileIoHandler.php @@ -72,8 +72,8 @@ class FileIoHandler extends BaseMiddleware { $ioHandler = new FileIoHandler(); // Set the *real* file IO instances (both the same) - $ioHandler->setInputStream(FileIoStream::createFileIoStream()); - $ioHandler->setOutputStream(FileIoStream::createFileIoStream()); + $ioHandler->setInputStream(ObjectFactory::createObjectByConfiguredName('file_input_stream')); + $ioHandler->setOutputStream(ObjectFactory::createObjectByConfiguredName('file_output_stream')); // Return instance return $ioHandler; diff --git a/inc/config.php b/inc/config.php index 52bed7d..e918a66 100644 --- a/inc/config.php +++ b/inc/config.php @@ -37,7 +37,7 @@ $cfg->definePath(dirname(dirname(__FILE__)) . '/'); // DON'T MISS THE TRAILING S $cfg->defineDatabaseType('local'); // CFG: LOCAL-DB-PATH -$cfg->setConfigEntry('local_db_path', 'db/'); +$cfg->setConfigEntry('local_db_path', "db/"); // CFG: TIME-ZONE $cfg->setDefaultTimezone("Europe/Berlin"); @@ -144,5 +144,23 @@ $cfg->setConfigEntry('default_application', "selector"); // CFG: VERBOSE-LEVEL $cfg->setConfigEntry('verbose_level', 0); +// CFG: CACHE-TYPE +$cfg->setConfigEntry('cache_type', "MemoryCache"); + +// CFG: SEARCH-CRITERIA +$cfg->setConfigEntry('search_criteria', "SearchCriteria"); + +// CFG: FILE-IO-HANDLER +$cfg->setConfigEntry('file_io_handler', "FileIoHandler"); + +// CFG: FILTER-CHAIN +$cfg->setConfigEntry('filter_chain', "FilterChain"); + +// CFG: FILE-INPUT-STREAM +$cfg->setConfigEntry('file_input_stream', "FileIoStream"); + +// CFG: FILE-OUTPUT-STREAM +$cfg->setConfigEntry('file_output_stream', "FileIoStream"); + // [EOF] ?> diff --git a/inc/file_io.php b/inc/file_io.php index 5e4ba6a..e9472bb 100644 --- a/inc/file_io.php +++ b/inc/file_io.php @@ -25,7 +25,7 @@ */ // Get the instance -$io = FileIoHandler::createFileIoHandler(); +$io = ObjectFactory::createObjectByConfiguredName('file_io_handler'); // [EOF] ?> -- 2.39.2