* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team * @license GNU GPL 3.0 or any newer version * @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 * 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 . */ class HtmlActionResolver extends BaseActionResolver implements ActionResolver { /** * Last successfull resolved action */ private $lastActionInstance = NULL; /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); // Set prefix to 'html' $this->setClassPrefix('html'); } /** * Creates an instance of a Html action resolver with a given default action * * @param $actionName The default action we shall execute * @param $applicationInstance An instance of a manageable application helper class * @return $resolverInstance The prepared action resolver instance * @throws InvalidArgumentException Thrown if default action is not set * @throws InvalidActionException Thrown if default action is invalid */ public static final function createHtmlActionResolver ($actionName, ManageableApplication $applicationInstance) { // Create the new instance $resolverInstance = new HtmlActionResolver(); // Is the variable $actionName set and the action is valid? if (empty($actionName)) { // Then thrown an exception here throw new InvalidArgumentException('Parameter "actionName" is empty'); } elseif ($resolverInstance->isActionValid($actionName) === false) { // Invalid action found throw new InvalidActionException(array($resolverInstance, $actionName), self::EXCEPTION_INVALID_ACTION); } // Set the application instance $resolverInstance->setApplicationInstance($applicationInstance); // 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 Requestable 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 resolve the action $actionName = $requestInstance->getRequestElement('action'); // Is the action empty? Then fall back to default action if (empty($actionName)) { $actionName = $this->getConfigInstance()->getConfigEntry('default_action'); } // END - if // Check if action is valid if ($this->isActionValid($actionName) === false) { // 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->setResolvedInstance($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()->getConfigEntry('default_action'); } // END - if // Check if action is valid if ($this->isActionValid($actionName) === false) { // This action is invalid! throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION); } // END - if // Get the action $actionInstance = $this->loadAction(); // Return the instance return $actionInstance; } }