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