3 namespace Org\Mxchange\CoreFramework\Resolver\Action;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
7 use Org\Mxchange\CoreFramework\Request\Requestable;
10 use \InvalidArgumentException;
13 * A action resolver for local (non-hubbed) actions
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 <<<<<<< HEAD:framework/main/classes/resolver/action/html/class_HtmlActionResolver.php
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
21 >>>>>>> Some updates::inc/main/classes/resolver/action/html/class_HtmlActionResolver.php
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class HtmlActionResolver extends BaseActionResolver implements ActionResolver {
40 * Last successfull resolved action
42 private $lastActionInstance = NULL;
45 * Protected constructor
49 protected function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
53 // Set prefix to 'html'
54 $this->setClassPrefix('html');
58 * Creates an instance of a Html action resolver with a given default action
60 * @param $actionName The default action we shall execute
61 * @param $applicationInstance An instance of a manageable application helper class
62 * @return $resolverInstance The prepared action resolver instance
63 * @throws InvalidArgumentException Thrown if default action is not set
64 * @throws InvalidActionException Thrown if default action is invalid
66 public static final function createHtmlActionResolver ($actionName, ManageableApplication $applicationInstance) {
67 // Create the new instance
68 $resolverInstance = new HtmlActionResolver();
70 // Is the variable $actionName set and the action is valid?
71 if (empty($actionName)) {
72 // Then thrown an exception here
73 throw new InvalidArgumentException('Parameter "actionName" is empty');
74 } elseif ($resolverInstance->isActionValid($actionName) === false) {
75 // Invalid action found
76 throw new InvalidActionException(array($resolverInstance, $actionName), self::EXCEPTION_INVALID_ACTION);
79 // Set the application instance
80 $resolverInstance->setApplicationInstance($applicationInstance);
82 // Return the prepared instance
83 return $resolverInstance;
87 * Returns an action instance for a given request class or null if
90 * @param $requestInstance An instance of a Requestable class
91 * @return $actionInstance An instance of the resolved action
92 * @throws InvalidActionException Thrown if $actionName is
94 * @throws InvalidActionInstanceException Thrown if $actionInstance
95 * is an invalid instance
97 public function resolveActionByRequest (Requestable $requestInstance) {
100 $actionInstance = NULL;
102 // This goes fine so let's resolve the action
103 $actionName = $requestInstance->getRequestElement('action');
105 // Is the action empty? Then fall back to default action
106 if (empty($actionName)) {
107 $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
110 // Check if action is valid
111 if ($this->isActionValid($actionName) === false) {
112 // This action is invalid!
113 throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
117 $actionInstance = $this->loadAction();
120 if ((!is_object($actionInstance)) || (!$actionInstance instanceof Actionable)) {
121 // This action has an invalid instance!
122 throw new InvalidActionInstanceException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
126 $this->setResolvedInstance($actionInstance);
128 // Return the resolved action instance
129 return $actionInstance;
133 * Resolves the action by its direct name and returns an instance of its class
135 * @return $actionInstance An instance of the action class
136 * @throws InvalidActionException Thrown if $actionName is invalid
138 public function resolveAction () {
139 // Initiate the instance variable
140 $actionInstance = NULL;
143 $actionName = $this->getActionName();
145 // Is the action empty? Then fall back to default action
146 if (empty($actionName)) {
147 $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
150 // Check if action is valid
151 if ($this->isActionValid($actionName) === false) {
152 // This action is invalid!
153 throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
157 $actionInstance = $this->loadAction();
159 // Return the instance
160 return $actionInstance;