Continued:
[core.git] / framework / main / classes / resolver / action / html / class_HtmlActionResolver.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Resolver\Action;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Request\Requestable;
9
10 // Import SPL stuff
11 use \InvalidArgumentException;
12
13 /**
14  * A action resolver for local (non-hubbed) actions
15  *
16  * @author              Roland Haeder <webmaster@shipsimu.org>
17  * @version             0.0.0
18  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
19  * @license             GNU GPL 3.0 or any newer version
20  * @link                http://www.shipsimu.org
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program. If not, see <http://www.gnu.org/licenses/>.
34  */
35 class HtmlActionResolver extends BaseActionResolver implements ActionResolver {
36         /**
37          * Last successfull resolved action
38          */
39         private $lastActionInstance = NULL;
40
41         /**
42          * Protected constructor
43          *
44          * @return      void
45          */
46         private function __construct () {
47                 // Call parent constructor
48                 parent::__construct(__CLASS__);
49
50                 // Set prefix to 'html'
51                 $this->setClassPrefix('html');
52         }
53
54         /**
55          * Creates an instance of a Html action resolver with a given default action
56          *
57          * @param       $actionName                             The default action we shall execute
58          * @return      $resolverInstance               The prepared action resolver instance
59          * @throws      InvalidArgumentException        Thrown if default action is not set
60          * @throws      InvalidActionException  Thrown if default action is invalid
61          */
62         public static final function createHtmlActionResolver ($actionName) {
63                 // Create the new instance
64                 $resolverInstance = new HtmlActionResolver();
65
66                 // Is the variable $actionName set and the action is valid?
67                 if (empty($actionName)) {
68                         // Then thrown an exception here
69                         throw new InvalidArgumentException('Parameter "actionName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
70                 } elseif ($resolverInstance->isActionValid($actionName) === false) {
71                         // Invalid action found
72                         throw new InvalidActionException(array($resolverInstance, $actionName), self::EXCEPTION_INVALID_ACTION);
73                 }
74
75                 // Return the prepared instance
76                 return $resolverInstance;
77         }
78
79         /**
80          * Returns an action instance for a given request class or null if
81          * it was not found
82          *
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
86          *                                                                                              invalid
87          * @throws      InvalidActionInstanceException          Thrown if $actionInstance
88          *                                                                                              is an invalid instance
89          */
90         public function resolveActionByRequest (Requestable $requestInstance) {
91                 // Init variables
92                 $actionName = '';
93                 $actionInstance = NULL;
94
95                 // This goes fine so let's resolve the action
96                 $actionName = $requestInstance->getRequestElement('action');
97
98                 // Is the action empty? Then fall back to default action
99                 if (empty($actionName)) {
100                         $actionName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_action');
101                 }
102
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);
107                 }
108
109                 // Get the action
110                 $actionInstance = $this->loadAction();
111
112                 // And validate it
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);
116                 }
117
118                 // Set last action
119                 $this->setResolvedInstance($actionInstance);
120
121                 // Return the resolved action instance
122                 return $actionInstance;
123         }
124
125         /**
126          * Resolves the action by its direct name and returns an instance of its class
127          *
128          * @return      $actionInstance         An instance of the action class
129          * @throws      InvalidActionException  Thrown if $actionName is invalid
130          */
131         public function resolveAction () {
132                 // Initiate the instance variable
133                 $actionInstance = NULL;
134
135                 // Get action name
136                 $actionName = $this->getActionName();
137
138                 // Is the action empty? Then fall back to default action
139                 if (empty($actionName)) {
140                         $actionName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_action');
141                 }
142
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);
147                 }
148
149                 // Get the action
150                 $actionInstance = $this->loadAction();
151
152                 // Return the instance
153                 return $actionInstance;
154         }
155
156 }