Continued with renaming-season:
[core.git] / framework / main / classes / resolver / action / html / class_HtmlActionResolver.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Resolver\Action;
4
5 // Import framework stuff
6 use CoreFramework\Generic\EmptyVariableException;
7 use CoreFramework\Manager\ManageableApplication;
8 use CoreFramework\Request\Requestable;
9
10 /**
11  * A action resolver for local (non-hubbed) actions
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
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
18  *
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.
23  *
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.
28  *
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/>.
31  */
32 class HtmlActionResolver extends BaseActionResolver implements ActionResolver {
33         /**
34          * Last successfull resolved action
35          */
36         private $lastActionInstance = NULL;
37
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46
47                 // Set prefix to 'html'
48                 $this->setClassPrefix('html');
49         }
50
51         /**
52          * Creates an instance of a Html action resolver with a given default action
53          *
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
59          */
60         public static final function createHtmlActionResolver ($actionName, ManageableApplication $applicationInstance) {
61                 // Create the new instance
62                 $resolverInstance = new HtmlActionResolver();
63
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);
71                 }
72
73                 // Set the application instance
74                 $resolverInstance->setApplicationInstance($applicationInstance);
75
76                 // Return the prepared instance
77                 return $resolverInstance;
78         }
79
80         /**
81          * Returns an action instance for a given request class or null if
82          * it was not found
83          *
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
87          *                                                                                              invalid
88          * @throws      InvalidActionInstanceException          Thrown if $actionInstance
89          *                                                                                              is an invalid instance
90          */
91         public function resolveActionByRequest (Requestable $requestInstance) {
92                 // Init variables
93                 $actionName = '';
94                 $actionInstance = NULL;
95
96                 // This goes fine so let's resolve the action
97                 $actionName = $requestInstance->getRequestElement('action');
98
99                 // Is the action empty? Then fall back to default action
100                 if (empty($actionName)) {
101                         $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
102                 } // END - if
103
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);
108                 } // END - if
109
110                 // Get the action
111                 $actionInstance = $this->loadAction();
112
113                 // And validate it
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);
117                 } // END - if
118
119                 // Set last action
120                 $this->setResolvedInstance($actionInstance);
121
122                 // Return the resolved action instance
123                 return $actionInstance;
124         }
125
126         /**
127          * Resolves the action by its direct name and returns an instance of its class
128          *
129          * @return      $actionInstance         An instance of the action class
130          * @throws      InvalidActionException  Thrown if $actionName is invalid
131          */
132         public function resolveAction () {
133                 // Initiate the instance variable
134                 $actionInstance = NULL;
135
136                 // Get action name
137                 $actionName = $this->getActionName();
138
139                 // Is the action empty? Then fall back to default action
140                 if (empty($actionName)) {
141                         $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
142                 } // END - if
143
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);
148                 } // END - if
149
150                 // Get the action
151                 $actionInstance = $this->loadAction();
152
153                 // Return the instance
154                 return $actionInstance;
155         }
156
157 }