3 namespace CoreFramework\Resolver\Action;
5 // Import framework stuff
6 use CoreFramework\Manager\ManageableApplication;
7 use CoreFramework\Request\Requestable;
10 * A action resolver for local (non-hubbed) actions
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class HtmlActionResolver extends BaseActionResolver implements ActionResolver {
33 * Last successfull resolved action
35 private $lastActionInstance = NULL;
38 * Protected constructor
42 protected function __construct () {
43 // Call parent constructor
44 parent::__construct(__CLASS__);
46 // Set prefix to 'html'
47 $this->setClassPrefix('html');
51 * Creates an instance of a Html action resolver with a given default action
53 * @param $actionName The default action we shall execute
54 * @param $applicationInstance An instance of a manageable application helper class
55 * @return $resolverInstance The prepared action resolver instance
56 * @throws EmptyVariableException Thrown if default action is not set
57 * @throws InvalidActionException Thrown if default action is invalid
59 public static final function createHtmlActionResolver ($actionName, ManageableApplication $applicationInstance) {
60 // Create the new instance
61 $resolverInstance = new HtmlActionResolver();
63 // Is the variable $actionName set and the action is valid?
64 if (empty($actionName)) {
65 // Then thrown an exception here
66 throw new EmptyVariableException(array($resolverInstance, 'defaultAction'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
67 } elseif ($resolverInstance->isActionValid($actionName) === FALSE) {
68 // Invalid action found
69 throw new InvalidActionException(array($resolverInstance, $actionName), self::EXCEPTION_INVALID_ACTION);
72 // Set the application instance
73 $resolverInstance->setApplicationInstance($applicationInstance);
75 // Return the prepared instance
76 return $resolverInstance;
80 * Returns an action instance for a given request class or null if
83 * @param $requestInstance An instance of a Requestable class
84 * @return $actionInstance An instance of the resolved action
85 * @throws InvalidActionException Thrown if $actionName is
87 * @throws InvalidActionInstanceException Thrown if $actionInstance
88 * is an invalid instance
90 public function resolveActionByRequest (Requestable $requestInstance) {
93 $actionInstance = NULL;
95 // This goes fine so let's resolve the action
96 $actionName = $requestInstance->getRequestElement('action');
98 // Is the action empty? Then fall back to default action
99 if (empty($actionName)) {
100 $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
103 // Check if action is valid
104 if ($this->isActionValid($actionName) === FALSE) {
105 // This action is invalid!
106 throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
110 $actionInstance = $this->loadAction();
113 if ((!is_object($actionInstance)) || (!$actionInstance instanceof Actionable)) {
114 // This action has an invalid instance!
115 throw new InvalidActionInstanceException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
119 $this->setResolvedInstance($actionInstance);
121 // Return the resolved action instance
122 return $actionInstance;
126 * Resolves the action by its direct name and returns an instance of its class
128 * @return $actionInstance An instance of the action class
129 * @throws InvalidActionException Thrown if $actionName is invalid
131 public function resolveAction () {
132 // Initiate the instance variable
133 $actionInstance = NULL;
136 $actionName = $this->getActionName();
138 // Is the action empty? Then fall back to default action
139 if (empty($actionName)) {
140 $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
143 // Check if action is valid
144 if ($this->isActionValid($actionName) === FALSE) {
145 // This action is invalid!
146 throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
150 $actionInstance = $this->loadAction();
152 // Return the instance
153 return $actionInstance;