Use getResponseTypeFromSystem() to get 'html' or 'console', what is really needed.
[core.git] / inc / classes / main / resolver / command / class_BaseCommandResolver.php
index d767d2906080bcdaa4a05d472c2933f84be129f7..c750238d5e36ca67bf34a160906b7eb7dcd85109 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * A generic command resolver class
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
+ * @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
+ * @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
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class BaseCommandResolver extends BaseResolver {
-       /**
-        * Validated command name
-        */
-       private $commandName = '';
-
        /**
         * Protected constructor
         *
@@ -39,22 +34,109 @@ class BaseCommandResolver extends BaseResolver {
        }
 
        /**
-        * Setter for command name
+        * "Loads" a given command and instances it if not yet cached
         *
-        * @param       $commandName    Last validated command name
-        * @return      void
+        * @param       $commandName                            A command name we shall look for
+        * @return      $commandInstance                        A loaded command instance
+        * @throws      InvalidCommandException         Thrown if even the default
+        *                                                                              command class is missing (bad!)
         */
-       protected final function setCommandName ($commandName) {
-               $this->commandName = $commandName;
+       protected function loadCommand ($commandName) {
+               // Init command instance
+               $commandInstance = NULL;
+
+               // Create class name
+               $className = $this->getCapitalizedClassPrefix() . $this->convertToClassName($commandName) . 'Command';
+
+               // Create command class name
+               $this->setClassName($className);
+
+               // Is this class loaded?
+               if (!class_exists($this->getClassName())) {
+                       // Class not found, so throw an exception
+                       throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
+               } // END - if
+
+               // Initiate the command
+               $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
+
+               // Return the result
+               return $commandInstance;
        }
 
        /**
-        * Getter for command name
+        * Returns an command instance for a given request class or null if
+        * it was not found
         *
-        * @return      $commandName    Last validated command name
+        * @param       $requestInstance        An instance of a request class
+        * @return      $commandInstance        An instance of the resolved command
+        * @throws      InvalidCommandException                         Thrown if $commandName is
+        *                                                                                              invalid
+        * @throws      InvalidCommandInstanceException         Thrown if $commandInstance
+        *                                                                                              is an invalid instance
         */
-       protected final function getCommandName () {
-               return $this->commandName;
+       public function resolveCommandByRequest (Requestable $requestInstance) {
+               // Init variables
+               $commandName = '';
+               $commandInstance = NULL;
+
+               // This goes fine so let's resolve the command
+               $commandName = $requestInstance->getRequestElement('command');
+
+               // Is the command empty? Then fall back to default command
+               if (empty($commandName)) {
+                       $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
+               } // END - if
+
+               // Check if command is valid
+               if ($this->isCommandValid($commandName) === FALSE) {
+                       // This command is invalid!
+                       throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
+               } // END - if
+
+               // Get the command
+               $commandInstance = $this->loadCommand($commandName);
+
+               // And validate it
+               if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
+                       // This command has an invalid instance!
+                       throw new InvalidCommandInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
+               } // END - if
+
+               // Set last command
+               $this->setResolvedInstance($commandInstance);
+
+               // Return the resolved command instance
+               return $commandInstance;
+       }
+
+       /**
+        * Resolves the command by its direct name and returns an instance of its class
+        *
+        * @param       $commandName            The direct command name we shall resolve
+        * @return      $commandInstance        An instance of the command class
+        * @throws      InvalidCommandException         Thrown if $commandName is invalid
+        */
+       public function resolveCommand ($commandName) {
+               // Initiate the instance variable
+               $commandInstance = NULL;
+
+               // Is the command empty? Then fall back to default command
+               if (empty($commandName)) {
+                       $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
+               } // END - if
+
+               // Check if command is valid
+               if ($this->isCommandValid($commandName) === FALSE) {
+                       // This command is invalid!
+                       throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
+               } // END - if
+
+               // Get the command
+               $commandInstance = $this->loadCommand($commandName);
+
+               // Return the instance
+               return $commandInstance;
        }
 
        /**
@@ -66,7 +148,7 @@ class BaseCommandResolver extends BaseResolver {
         */
        public function isCommandValid ($commandName) {
                // By default nothing shall be valid
-               $isValid = false;
+               $isValid = FALSE;
 
                // Is a command set?
                if (empty($commandName)) {
@@ -75,7 +157,7 @@ class BaseCommandResolver extends BaseResolver {
                } // END - if
 
                // Create the full class name
-               $className = $this->getClassPrefix() . $this->convertToClassName($commandName) . 'Command';
+               $className = $this->getCapitalizedClassPrefix() . $this->convertToClassName($commandName) . 'Command';
 
                // Now, let us create the full name of the command class
                $this->setClassName($className);
@@ -83,7 +165,7 @@ class BaseCommandResolver extends BaseResolver {
                // Is this class already loaded?
                if (class_exists($this->getClassName())) {
                        // This class does exist. :-)
-                       $isValid = true;
+                       $isValid = TRUE;
                } // END - if
 
                // Set command name