3 namespace CoreFramework\Resolver\Action;
5 // Import framework stuff
6 use CoreFramework\Generic\EmptyVariableException;
7 use CoreFramework\Manager\ManageableApplication;
8 use CoreFramework\Request\Requestable;
11 * A action resolver for local (non-hubbed) actions
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class HtmlActionResolver extends BaseActionResolver implements ActionResolver {
34 * Last successfull resolved action
36 private $lastActionInstance = NULL;
39 * Protected constructor
43 protected function __construct () {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
47 // Set prefix to 'html'
48 $this->setClassPrefix('html');
52 * Creates an instance of a Html action resolver with a given default action
54 * @param $actionName The default action we shall execute
55 * @param $applicationInstance An instance of a manageable application helper class
56 * @return $resolverInstance The prepared action resolver instance
57 * @throws EmptyVariableException Thrown if default action is not set
58 * @throws InvalidActionException Thrown if default action is invalid
60 public static final function createHtmlActionResolver ($actionName, ManageableApplication $applicationInstance) {
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 EmptyVariableException(array($resolverInstance, 'defaultAction'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
68 } elseif ($resolverInstance->isActionValid($actionName) === FALSE) {
69 // Invalid action found
70 throw new InvalidActionException(array($resolverInstance, $actionName), self::EXCEPTION_INVALID_ACTION);
73 // Set the application instance
74 $resolverInstance->setApplicationInstance($applicationInstance);
76 // Return the prepared instance
77 return $resolverInstance;
81 * Returns an action instance for a given request class or null if
84 * @param $requestInstance An instance of a Requestable class
85 * @return $actionInstance An instance of the resolved action
86 * @throws InvalidActionException Thrown if $actionName is
88 * @throws InvalidActionInstanceException Thrown if $actionInstance
89 * is an invalid instance
91 public function resolveActionByRequest (Requestable $requestInstance) {
94 $actionInstance = NULL;
96 // This goes fine so let's resolve the action
97 $actionName = $requestInstance->getRequestElement('action');
99 // Is the action empty? Then fall back to default action
100 if (empty($actionName)) {
101 $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
104 // Check if action is valid
105 if ($this->isActionValid($actionName) === FALSE) {
106 // This action is invalid!
107 throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
111 $actionInstance = $this->loadAction();
114 if ((!is_object($actionInstance)) || (!$actionInstance instanceof Actionable)) {
115 // This action has an invalid instance!
116 throw new InvalidActionInstanceException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
120 $this->setResolvedInstance($actionInstance);
122 // Return the resolved action instance
123 return $actionInstance;
127 * Resolves the action by its direct name and returns an instance of its class
129 * @return $actionInstance An instance of the action class
130 * @throws InvalidActionException Thrown if $actionName is invalid
132 public function resolveAction () {
133 // Initiate the instance variable
134 $actionInstance = NULL;
137 $actionName = $this->getActionName();
139 // Is the action empty? Then fall back to default action
140 if (empty($actionName)) {
141 $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
144 // Check if action is valid
145 if ($this->isActionValid($actionName) === FALSE) {
146 // This action is invalid!
147 throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
151 $actionInstance = $this->loadAction();
153 // Return the instance
154 return $actionInstance;