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 5ed3f32..9a680d2 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 b925ebd..6a94eba 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 0b41b4f..86b9526 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 304d7cd..34f0ed3 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 c7a35e7..8443460 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 a2bf811..409ded2 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 18aabb0..f9caae5 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 db2611c..68a856d 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 5dda5fb..3f8ac7f 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 5a604a7..946f96b 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 d1c80ca..e48e091 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 16c5b87..a7322fb 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 52bed7d..e918a66 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 5e4ba6a..e9472bb 100644 (file)
@@ -25,7 +25,7 @@
  */
 
 // Get the instance
-$io = FileIoHandler::createFileIoHandler();
+$io = ObjectFactory::createObjectByConfiguredName('file_io_handler');
 
 // [EOF]
 ?>