]> git.mxchange.org Git - shipsimu.git/commitdiff
More object are configureable, cache initially added
authorRoland Häder <roland@mxchange.org>
Sat, 31 May 2008 18:47:51 +0000 (18:47 +0000)
committerRoland Häder <roland@mxchange.org>
Sat, 31 May 2008 18:47:51 +0000 (18:47 +0000)
20 files changed:
.gitattributes
application/ship-simu/class_ApplicationHelper.php
application/ship-simu/main/commands/web/class_WebShipsimuRegisterCommand.php
inc/classes/exceptions/main/class_ClassNotFoundException.php
inc/classes/interfaces/cache/.htaccess [new file with mode: 0644]
inc/classes/interfaces/cache/class_Cacheable.php [new file with mode: 0644]
inc/classes/main/cache/.htaccess [new file with mode: 0644]
inc/classes/main/cache/class_MemoryCache.php [new file with mode: 0644]
inc/classes/main/class_
inc/classes/main/controller/class_BaseController.php
inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php
inc/classes/main/factories/cache/.htaccess [new file with mode: 0644]
inc/classes/main/factories/cache/class_CacheFactory.php [new file with mode: 0644]
inc/classes/main/factories/class_BaseFactory.php
inc/classes/main/factories/objects/class_ObjectFactory.php
inc/classes/main/factories/web/class_WebNewsFactory.php
inc/classes/middleware/compressor/class_CompressorChannel.php
inc/classes/middleware/io/class_FileIoHandler.php
inc/config.php
inc/file_io.php

index 5ed3f3209ee975c386519a054338d0c223b0ee7b..9a680d23de7cdce3ce7c35fbcbd74870c35f4e8a 100644 (file)
@@ -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
index b925ebdff6c92f83ce317baef7a3880d9f0f68d9..6a94ebad143e1dacfa49d7943a7f1039cee40ed8 100644 (file)
@@ -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);
index 0b41b4fe913e08c03e92cdba110eb77144756479..86b95269e349f59599f06bcadd91a114e98e167a 100644 (file)
@@ -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;
index 304d7cd8c2b03bac833648ebcaeb4d78b3737159..34f0ed3c36d3dea146563c2219552519d83564e8 100644 (file)
@@ -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 <u>%s</u> konnte nicht gefunden werden!", $class);
+               $message = sprintf("[%s:%d] Internal class <u>%s</u> 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 (file)
index 0000000..3a42882
--- /dev/null
@@ -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 (file)
index 0000000..c6fa237
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+/**
+ * An interface for classes which are "able" to cache
+ *
+ * @author             Roland Haeder <webmaster@mxchange.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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 (file)
index 0000000..3a42882
--- /dev/null
@@ -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 (file)
index 0000000..8dd2537
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+/**
+ * A simple memory cache (similar to a registry)
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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]
+?>
index c7a35e7d32ef4bb0901b5c7a829c0cc8aa59d473..84434608897e137df38a865b9ffcf6d7fe0d5fe2 100644 (file)
@@ -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("");
index a2bf81158444abaea1aef399342006ba2683eee4..409ded2b9eba7a60ee8893b1818947fce167013b 100644 (file)
@@ -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');
        }
 
        /**
index 18aabb060cbd68f9a2268901a3ddf33e60d0d4f5..f9caae5220caacc2d5007db0171d901cfc43fdff 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * A database client for the User class
+ * A database wrapper for the User class
  *
  * @see                        DatabaseFrontendInterface - An interface for database frontends (front-end to the application)
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 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 (file)
index 0000000..3a42882
--- /dev/null
@@ -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 (file)
index 0000000..087b942
--- /dev/null
@@ -0,0 +1,80 @@
+<?php
+/**
+ * A cache factory
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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]
+?>
index db2611c28913aa784557c8d4b945eda19865cf59..68a856d3958bec8d70fd3fe63291e88447919f43 100644 (file)
@@ -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();
index 5dda5fb904b8e177b3f7a70286a2d693ee1dee04..3f8ac7fa61b116b5dd93c4d5b9735f8bec1fc0de 100644 (file)
@@ -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]
index 5a604a77981b4af97d7efde20dbc83634bf9c85b..946f96b406f50866916d41553bd67b2c3b13175e 100644 (file)
@@ -38,6 +38,9 @@ class WebNewsFactory extends BaseFactory {
 
                // Set part description
                $this->setObjectDescription("Factory for webpages with news");
+
+               // Create unique ID number
+               $this->createUniqueID();
        }
 
        /**
index d1c80ca5e4bf70da3dd11be7ea61718159b21779..e48e091a3784c2d201cc389c83953514896dada6 100644 (file)
@@ -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
index 16c5b87dae69d7d87ddb8d4b3d272f50944ebd3e..a7322fb23ae3b31ce508ccbdd7a4bcfc2318dae7 100644 (file)
@@ -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;
index 52bed7d1c53eb62f6948ecddf37b70fb66015790..e918a66b5d1822fab8ba25bc7c2d9a9e13027fa6 100644 (file)
@@ -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]
 ?>
index 5e4ba6a38292fb633f55954385e810c4bcdbdb53..e9472bbb4573367e8f9bdaed7f3609df762e3ba9 100644 (file)
@@ -25,7 +25,7 @@
  */
 
 // Get the instance
-$io = FileIoHandler::createFileIoHandler();
+$io = ObjectFactory::createObjectByConfiguredName('file_io_handler');
 
 // [EOF]
 ?>