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