Code merge from latest ship-simu code
[mailer.git] / inc / classes / main / resolver / web / class_WebCommandResolver.php
index 30b31b5240dd13284fd9235720fb0b5ae4cecaaf..47baef47bfc4cceabea389d6e49aa602e24413cd 100644 (file)
@@ -28,7 +28,7 @@ class WebCommandResolver extends BaseResolver implements CommandResolver {
        private $lastCommandInstance = "";
 
        /**
-        * Private constructor
+        * Protected constructor
         *
         * @return      void
         */
@@ -37,15 +37,11 @@ class WebCommandResolver extends BaseResolver implements CommandResolver {
                parent::__construct(__CLASS__);
 
                // Set part description
-               $this->setObjectDescription("Lokaler Kommandoauflöser");
+               $this->setObjectDescription("Resolver for local web commands");
 
                // Create unique ID number
                $this->createUniqueID();
 
-               // Clean up a little
-               $this->removeNumberFormaters();
-               $this->removeSystemArray();
-
                // Set prefix to "Web"
                $this->setCommandPrefix("Web");
        }
@@ -53,23 +49,23 @@ class WebCommandResolver extends BaseResolver implements CommandResolver {
        /**
         * Creates an instance of a Web command resolver with a given default command
         *
-        * @param       $defaultCommand                         The default command we shall execute
+        * @param       $commandName                            The default command we shall execute
         * @param       $appInstance                            An instance of a manageable application helper class
         * @return      $resolverInstance                       The prepared command resolver instance
         * @throws      EmptyVariableException          Thrown if the default command is not set
         * @throws      InvalidCommandException         Thrown if the default command is invalid
         */
-       public final static function createWebCommandResolver ($defaultCommand, ManageableApplication $appInstance) {
+       public final static function createWebCommandResolver ($commandName, ManageableApplication $appInstance) {
                // Create the new instance
                $resolverInstance = new WebCommandResolver();
 
-               // Is the variable $defaultCommand set and the command is valid?
-               if (empty($defaultCommand)) {
+               // Is the variable $commandName set and the command is valid?
+               if (empty($commandName)) {
                        // Then thrown an exception here
                        throw new EmptyVariableException(array($resolverInstance, 'defaultCommand'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
-               } elseif (!$resolverInstance->isCommandValid($defaultCommand)) {
+               } elseif (!$resolverInstance->isCommandValid($commandName)) {
                        // Invalid command found
-                       throw new InvalidCommandException(array($resolverInstance, $defaultCommand), self::EXCEPTION_INVALID_COMMAND);
+                       throw new InvalidCommandException(array($resolverInstance, $commandName), self::EXCEPTION_INVALID_COMMAND);
                }
 
                // Set the application instance
@@ -98,10 +94,10 @@ class WebCommandResolver extends BaseResolver implements CommandResolver {
                // Test if the required parameter is set
                try {
                        // This goes fine so let's resolv the command
-                       $commandName = $requestInstance->getRequestElement($this->getConfigInstance()->readConfig("command_parameter"));
+                       $commandName = $requestInstance->getRequestElement($this->getConfigInstance()->readConfig('command_parameter'));
 
                        // Is the command empty? Then fall back to default command
-                       if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig("default_command");
+                       if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig('default_command');
 
                        // Check if the command is valid
                        if (!$this->isCommandValid($commandName)) {
@@ -128,6 +124,33 @@ class WebCommandResolver extends BaseResolver implements CommandResolver {
                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()->readConfig('default_command');
+
+               // Check if the command is valid
+               if (!$this->isCommandValid($commandName)) {
+                       // This command is invalid!
+                       throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
+               }
+
+               // Get the command
+               $commandInstance = $this->loadCommand($commandName);
+
+               // Return the instance
+               return $commandInstance;
+       }
+
        /**
         * "Loads" a given command and instances it if not yet cached
         *
@@ -138,18 +161,18 @@ class WebCommandResolver extends BaseResolver implements CommandResolver {
         */
        private function loadCommand ($commandName) {
                // Cache default command
-               $defaultCommand = $this->getConfigInstance()->readConfig("default_command");
+               $defaultCommand = $this->getConfigInstance()->readConfig('default_command');
 
                // Init command instance
                $commandInstance = null;
 
                // Create command class name
-               $class = sprintf("Web%sCommand",
-                       ucfirst(strtolower($commandName))
+               $className = sprintf("Web%sCommand",
+                       $this->convertToClassName($commandName)
                );
 
                // Is this class loaded?
-               if (!class_exists($class)) {
+               if (!class_exists($className)) {
                        // Class not found, so try the default one or throw exception
                        if ($commandName != $defaultCommand) {
                                // Try the default command
@@ -162,8 +185,8 @@ class WebCommandResolver extends BaseResolver implements CommandResolver {
 
                // Initiate the command
                $eval = sprintf("\$commandInstance = %s::create%s(\$this);",
-                       $class,
-                       $class
+                       $className,
+                       $className
                );
 
                // Run the command