]> git.mxchange.org Git - hub.git/blobdiff - inc/classes/main/resolver/action/web/class_WebActionResolver.php
Code syncronized with shipsimu code base
[hub.git] / inc / classes / main / resolver / action / web / class_WebActionResolver.php
diff --git a/inc/classes/main/resolver/action/web/class_WebActionResolver.php b/inc/classes/main/resolver/action/web/class_WebActionResolver.php
new file mode 100644 (file)
index 0000000..83f5ddd
--- /dev/null
@@ -0,0 +1,147 @@
+<?php
+/**
+ * A action resolver for local (non-hubbed) actions
+ *
+ * @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 WebActionResolver extends BaseActionResolver implements ActionResolver {
+       /**
+        * Last successfull resolved action
+        */
+       private $lastActionInstance = "";
+
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+
+               // Set prefix to "Web"
+               $this->setActionPrefix("Web");
+       }
+
+       /**
+        * Creates an instance of a Web action resolver with a given default action
+        *
+        * @param       $actionName                             The default action we shall execute
+        * @param       $appInstance                    An instance of a manageable application helper class
+        * @return      $resolverInstance               The prepared action resolver instance
+        * @throws      EmptyVariableException  Thrown if the default action is not set
+        * @throws      InvalidActionException  Thrown if the default action is invalid
+        */
+       public final static function createWebActionResolver ($actionName, ManageableApplication $appInstance) {
+               // Create the new instance
+               $resolverInstance = new WebActionResolver();
+
+               // Is the variable $actionName set and the action is valid?
+               if (empty($actionName)) {
+                       // Then thrown an exception here
+                       throw new EmptyVariableException(array($resolverInstance, 'defaultAction'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
+               } elseif (!$resolverInstance->isActionValid($actionName)) {
+                       // Invalid action found
+                       throw new InvalidActionException(array($resolverInstance, $actionName), self::EXCEPTION_INVALID_ACTION);
+               }
+
+               // Set the application instance
+               $resolverInstance->setApplicationInstance($appInstance);
+
+               // Return the prepared instance
+               return $resolverInstance;
+       }
+
+       /**
+        * Returns an action instance for a given request class or null if
+        * it was not found
+        *
+        * @param       $requestInstance        An instance of a request class
+        * @return      $actionInstance An instance of the resolved action
+        * @throws      InvalidActionException                          Thrown if $actionName is
+        *                                                                                              invalid
+        * @throws      InvalidActionInstanceException          Thrown if $actionInstance
+        *                                                                                              is an invalid instance
+        */
+       public function resolveActionByRequest (Requestable $requestInstance) {
+               // Init variables
+               $actionName = "";
+               $actionInstance = null;
+
+               // This goes fine so let's resolv the action
+               $actionName = $requestInstance->getRequestElement('action');
+
+               // Is the action empty? Then fall back to default action
+               if (empty($actionName)) $actionName = $this->getConfigInstance()->readConfig('default_action');
+
+               // Check if the action is valid
+               if (!$this->isActionValid($actionName)) {
+                       // This action is invalid!
+                       throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
+               } // END - if
+
+               // Get the action
+               $actionInstance = $this->loadAction();
+
+               // And validate it
+               if ((!is_object($actionInstance)) || (!$actionInstance instanceof Actionable)) {
+                       // This action has an invalid instance!
+                       throw new InvalidActionInstanceException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
+               } // END - if
+
+               // Set last action
+               $this->lastActionInstance = $actionInstance;
+
+               // Return the resolved action instance
+               return $actionInstance;
+       }
+
+       /**
+        * Resolves the action by its direct name and returns an instance of its class
+        *
+        * @return      $actionInstance         An instance of the action class
+        * @throws      InvalidActionException  Thrown if $actionName is invalid
+        */
+       public function resolveAction () {
+               // Initiate the instance variable
+               $actionInstance = null;
+
+               // Get action name
+               $actionName = $this->getActionName();
+
+               // Is the action empty? Then fall back to default action
+               if (empty($actionName)) $actionName = $this->getConfigInstance()->readConfig('default_action');
+
+               // Check if the action is valid
+               if (!$this->isActionValid($actionName)) {
+                       // This action is invalid!
+                       throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
+               }
+
+               // Get the action
+               $actionInstance = $this->loadAction();
+
+               // Return the instance
+               return $actionInstance;
+       }
+}
+
+// [EOF]
+?>