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