Renamed getInstance() to getSelfInstance(), conflicts BaseRegistry versus several...
[shipsimu.git] / application / ship-simu / class_ApplicationHelper.php
index e141d04f08647918ff953edfd43df1fcf3836d41..9d40b3d915ee3e04433e0dbd30e956f2401b6a3a 100644 (file)
@@ -22,9 +22,9 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @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
@@ -37,9 +37,9 @@
  * 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/>.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplication {
+class ApplicationHelper extends BaseApplication implements ManageableApplication, Registerable {
        /**
         * The version number of this application
         */
@@ -55,28 +55,24 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica
         */
        private $shortName = "";
 
+       /**
+        * An instance of a controller
+        */
+       private $controllerInstance = null;
+
        /**
         * An instance of this class
         */
        private static $thisInstance = null;
 
        /**
-        * Private constructor
+        * Protected constructor
         *
         * @return      void
         */
-       private function __construct () {
+       protected function __construct () {
                // Call parent constructor
-               parent::constructor(__CLASS__);
-
-               // Set description
-               $this->setPartDescr("Application-Helper");
-
-               // Create an unique ID
-               $this->createUniqueID();
-
-               // Tidy up a little
-               $this->removeSystemArray();
+               parent::__construct(__CLASS__);
        }
 
        /**
@@ -84,11 +80,11 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica
         *
         * @return      $thisInstance           An instance of this class
         */
-       public final static function getInstance () {
+       public static final function getInstance () {
                // Is the instance there?
                if (is_null(self::$thisInstance)) {
                        self::$thisInstance = new ApplicationHelper();
-               }
+               } // END - if
 
                // Return the instance
                return self::$thisInstance;
@@ -97,7 +93,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica
        /**
         * Getter for the version number
         *
-        * @return      $appVersion     The application's version number
+        * @return      $appVersion             The application's version number
         */
        public final function getAppVersion () {
                return $this->appVersion;
@@ -106,7 +102,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica
        /**
         * Setter for the version number
         *
-        * @param               $appVersion     The application's version number
+        * @param       $appVersion             The application's version number
         * @return      void
         */
        public final function setAppVersion ($appVersion) {
@@ -127,7 +123,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica
        /**
         * Setter for human-readable name
         *
-        * @param               $appName        The application's human-readable name
+        * @param       $appName        The application's human-readable name
         * @return      void
         */
        public final function setAppName ($appName) {
@@ -148,7 +144,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica
        /**
         * Setter for short uni*-like name
         *
-        * @param               $shortName      The application's short uni*-like name
+        * @param       $shortName      The application's short uni*-like name
         * @return      void
         */
        public final function setAppShortName ($shortName) {
@@ -158,21 +154,107 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica
        }
 
        /**
-        * Launches the ship-simulator game
+        * Builds the master template's name
+        *
+        * @return      $masterTemplateName             Name of the master template
+        */
+       public function buildMasterTemplateName () {
+               // Get short name and add suffix
+               $masterTemplateName = str_replace('-', '', $this->getAppShortName()) . '_main';
+
+               // Return it
+               return $masterTemplateName;
+       }
+
+       /**
+        * Launches the admin area
         *
         * @return      void
         */
        public final function entryPoint () {
-               // Handle the request
+               // Set this application in registry
+               Registry::getRegistry()->addInstance('app', $this);
+
+               // Analyze the environment for type of response/request
+               $type = ConsoleTools::analyzeEnvironmentForType();
+               $classType = ConsoleTools::analyzeEnvironmentForClassType();
+
+               // Create a new request object
+               $requestInstance = ObjectFactory::createObjectByName(ucfirst($type) . 'Request');
+
+               // Remember request instance here
+               $this->setRequestInstance($requestInstance);
+
+               // Default response is HTTP (HTML page) and type is 'Web'
+               $response = $type;
+               $responseType = $classType;
+
+               // Do we have another response?
+               if ($requestInstance->isRequestElementSet('request')) {
+                       // Then use it
+                       $response = strtolower($requestInstance->getRequestElement('request'));
+                       $responseType = $response;
+               } // END - if
+
+               // ... and a new response object
+               $responseClass = sprintf("%sResponse", $this->convertToClassName($response));
+               $responseInstance = ObjectFactory::createObjectByName($responseClass, array($this));
 
-               // Prepare the template engine
-               $tplEngine = $this->prepareTemplateEngine($this);
+               // Remember response instance here
+               $this->setResponseInstance($responseInstance);
 
-               // Load the main template
-               $tplEngine->loadCodeTemplate("shipsimu_main");
+               // Get the parameter from the request
+               $commandName = $requestInstance->getRequestElement('page');
 
-               // Raise an error here
-               trigger_error(__METHOD__.": Unfinished work!");
+               // If it is null then get default command
+               if (is_null($commandName)) {
+                       // Get the default command
+                       $commandName = $responseInstance->getDefaultCommand();
+
+                       // Set it in request
+                       $requestInstance->setRequestElement('page', $commandName);
+               } // END - if
+
+               // Get a resolver
+               $resolverClass = sprintf("%sControllerResolver", $this->convertToClassName($responseType));
+               $resolverInstance = ObjectFactory::createObjectByName($resolverClass, array($commandName, $this));
+
+               // Get a controller instance as well
+               $this->controllerInstance = $resolverInstance->resolveController();
+
+               // Get a web output class
+               $outputInstance = ObjectFactory::createObjectByConfiguredName('output_class', array($this));
+
+               // Set it in this application
+               $this->setWebOutputInstance($outputInstance);
+
+               // Handle the request
+               $this->controllerInstance->handleRequest($requestInstance, $responseInstance);
+       }
+
+       /**
+        * Handle the indexed array of fatal messages and puts them out in an
+        * acceptable fasion
+        *
+        * @param       $messageList    An array of fatal messages
+        * @return      void
+        */
+       public function handleFatalMessages (array $messageList) {
+               // Walk through all messages
+               foreach ($messageList as $message) {
+                       print("MSG:".$message."<br />\n");
+               } // END - if
+       }
+
+       /**
+        * Assigns application-depending data
+        *
+        * @param       $templateInstance       An instance of a template engine
+        * @return      void
+        */
+       public function assignExtraTemplateData (CompileableTemplate $templateInstance) {
+               // Assign charset
+               $templateInstance->assignConfigVariable('header_charset');
        }
 }