3 namespace CoreFramework\Resolver\Action;
5 // Import framework stuff
6 use CoreFramework\Manager\ManageableApplication;
9 * A action resolver for local (non-hubbed) actions
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class HtmlActionResolver extends BaseActionResolver implements ActionResolver {
32 * Last successfull resolved action
34 private $lastActionInstance = NULL;
37 * Protected constructor
41 protected function __construct () {
42 // Call parent constructor
43 parent::__construct(__CLASS__);
45 // Set prefix to 'html'
46 $this->setClassPrefix('html');
50 * Creates an instance of a Html action resolver with a given default action
52 * @param $actionName The default action we shall execute
53 * @param $applicationInstance An instance of a manageable application helper class
54 * @return $resolverInstance The prepared action resolver instance
55 * @throws EmptyVariableException Thrown if default action is not set
56 * @throws InvalidActionException Thrown if default action is invalid
58 public static final function createHtmlActionResolver ($actionName, ManageableApplication $applicationInstance) {
59 // Create the new instance
60 $resolverInstance = new HtmlActionResolver();
62 // Is the variable $actionName set and the action is valid?
63 if (empty($actionName)) {
64 // Then thrown an exception here
65 throw new EmptyVariableException(array($resolverInstance, 'defaultAction'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
66 } elseif ($resolverInstance->isActionValid($actionName) === FALSE) {
67 // Invalid action found
68 throw new InvalidActionException(array($resolverInstance, $actionName), self::EXCEPTION_INVALID_ACTION);
71 // Set the application instance
72 $resolverInstance->setApplicationInstance($applicationInstance);
74 // Return the prepared instance
75 return $resolverInstance;
79 * Returns an action instance for a given request class or null if
82 * @param $requestInstance An instance of a request class
83 * @return $actionInstance An instance of the resolved action
84 * @throws InvalidActionException Thrown if $actionName is
86 * @throws InvalidActionInstanceException Thrown if $actionInstance
87 * is an invalid instance
89 public function resolveActionByRequest (Requestable $requestInstance) {
92 $actionInstance = NULL;
94 // This goes fine so let's resolve the action
95 $actionName = $requestInstance->getRequestElement('action');
97 // Is the action empty? Then fall back to default action
98 if (empty($actionName)) {
99 $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
102 // Check if action is valid
103 if ($this->isActionValid($actionName) === FALSE) {
104 // This action is invalid!
105 throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
109 $actionInstance = $this->loadAction();
112 if ((!is_object($actionInstance)) || (!$actionInstance instanceof Actionable)) {
113 // This action has an invalid instance!
114 throw new InvalidActionInstanceException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
118 $this->setResolvedInstance($actionInstance);
120 // Return the resolved action instance
121 return $actionInstance;
125 * Resolves the action by its direct name and returns an instance of its class
127 * @return $actionInstance An instance of the action class
128 * @throws InvalidActionException Thrown if $actionName is invalid
130 public function resolveAction () {
131 // Initiate the instance variable
132 $actionInstance = NULL;
135 $actionName = $this->getActionName();
137 // Is the action empty? Then fall back to default action
138 if (empty($actionName)) {
139 $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
142 // Check if action is valid
143 if ($this->isActionValid($actionName) === FALSE) {
144 // This action is invalid!
145 throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
149 $actionInstance = $this->loadAction();
151 // Return the instance
152 return $actionInstance;