3 namespace Org\Mxchange\CoreFramework\Resolver\Action;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Request\Requestable;
9 use \InvalidArgumentException;
12 * A action resolver for local (non-hubbed) actions
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class HtmlActionResolver extends BaseActionResolver implements ActionResolver {
35 * Last successfull resolved action
37 private $lastActionInstance = NULL;
40 * Protected constructor
44 protected function __construct () {
45 // Call parent constructor
46 parent::__construct(__CLASS__);
48 // Set prefix to 'html'
49 $this->setClassPrefix('html');
53 * Creates an instance of a Html action resolver with a given default action
55 * @param $actionName The default action we shall execute
56 * @return $resolverInstance The prepared action resolver instance
57 * @throws InvalidArgumentException Thrown if default action is not set
58 * @throws InvalidActionException Thrown if default action is invalid
60 public static final function createHtmlActionResolver ($actionName) {
61 // Create the new instance
62 $resolverInstance = new HtmlActionResolver();
64 // Is the variable $actionName set and the action is valid?
65 if (empty($actionName)) {
66 // Then thrown an exception here
67 throw new InvalidArgumentException('Parameter "actionName" is empty');
68 } elseif ($resolverInstance->isActionValid($actionName) === false) {
69 // Invalid action found
70 throw new InvalidActionException(array($resolverInstance, $actionName), self::EXCEPTION_INVALID_ACTION);
73 // Return the prepared instance
74 return $resolverInstance;
78 * Returns an action instance for a given request class or null if
81 * @param $requestInstance An instance of a Requestable class
82 * @return $actionInstance An instance of the resolved action
83 * @throws InvalidActionException Thrown if $actionName is
85 * @throws InvalidActionInstanceException Thrown if $actionInstance
86 * is an invalid instance
88 public function resolveActionByRequest (Requestable $requestInstance) {
91 $actionInstance = NULL;
93 // This goes fine so let's resolve the action
94 $actionName = $requestInstance->getRequestElement('action');
96 // Is the action empty? Then fall back to default action
97 if (empty($actionName)) {
98 $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
101 // Check if action is valid
102 if ($this->isActionValid($actionName) === false) {
103 // This action is invalid!
104 throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
108 $actionInstance = $this->loadAction();
111 if ((!is_object($actionInstance)) || (!$actionInstance instanceof Actionable)) {
112 // This action has an invalid instance!
113 throw new InvalidActionInstanceException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
117 $this->setResolvedInstance($actionInstance);
119 // Return the resolved action instance
120 return $actionInstance;
124 * Resolves the action by its direct name and returns an instance of its class
126 * @return $actionInstance An instance of the action class
127 * @throws InvalidActionException Thrown if $actionName is invalid
129 public function resolveAction () {
130 // Initiate the instance variable
131 $actionInstance = NULL;
134 $actionName = $this->getActionName();
136 // Is the action empty? Then fall back to default action
137 if (empty($actionName)) {
138 $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
141 // Check if action is valid
142 if ($this->isActionValid($actionName) === false) {
143 // This action is invalid!
144 throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
148 $actionInstance = $this->loadAction();
150 // Return the instance
151 return $actionInstance;