Renamed Registry -> GenericRegistry to make it clear that this registry does
[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  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 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         protected 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          * @param       $applicationInstance    An instance of a manageable application helper class
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, ManageableApplication $applicationInstance) {
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');
70                 } elseif ($resolverInstance->isActionValid($actionName) === false) {
71                         // Invalid action found
72                         throw new InvalidActionException(array($resolverInstance, $actionName), self::EXCEPTION_INVALID_ACTION);
73                 }
74
75                 // Set the application instance
76                 $resolverInstance->setApplicationInstance($applicationInstance);
77
78                 // Return the prepared instance
79                 return $resolverInstance;
80         }
81
82         /**
83          * Returns an action instance for a given request class or null if
84          * it was not found
85          *
86          * @param       $requestInstance        An instance of a Requestable class
87          * @return      $actionInstance An instance of the resolved action
88          * @throws      InvalidActionException                          Thrown if $actionName is
89          *                                                                                              invalid
90          * @throws      InvalidActionInstanceException          Thrown if $actionInstance
91          *                                                                                              is an invalid instance
92          */
93         public function resolveActionByRequest (Requestable $requestInstance) {
94                 // Init variables
95                 $actionName = '';
96                 $actionInstance = NULL;
97
98                 // This goes fine so let's resolve the action
99                 $actionName = $requestInstance->getRequestElement('action');
100
101                 // Is the action empty? Then fall back to default action
102                 if (empty($actionName)) {
103                         $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
104                 } // END - if
105
106                 // Check if action is valid
107                 if ($this->isActionValid($actionName) === false) {
108                         // This action is invalid!
109                         throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
110                 } // END - if
111
112                 // Get the action
113                 $actionInstance = $this->loadAction();
114
115                 // And validate it
116                 if ((!is_object($actionInstance)) || (!$actionInstance instanceof Actionable)) {
117                         // This action has an invalid instance!
118                         throw new InvalidActionInstanceException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
119                 } // END - if
120
121                 // Set last action
122                 $this->setResolvedInstance($actionInstance);
123
124                 // Return the resolved action instance
125                 return $actionInstance;
126         }
127
128         /**
129          * Resolves the action by its direct name and returns an instance of its class
130          *
131          * @return      $actionInstance         An instance of the action class
132          * @throws      InvalidActionException  Thrown if $actionName is invalid
133          */
134         public function resolveAction () {
135                 // Initiate the instance variable
136                 $actionInstance = NULL;
137
138                 // Get action name
139                 $actionName = $this->getActionName();
140
141                 // Is the action empty? Then fall back to default action
142                 if (empty($actionName)) {
143                         $actionName = $this->getConfigInstance()->getConfigEntry('default_action');
144                 } // END - if
145
146                 // Check if action is valid
147                 if ($this->isActionValid($actionName) === false) {
148                         // This action is invalid!
149                         throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION);
150                 } // END - if
151
152                 // Get the action
153                 $actionInstance = $this->loadAction();
154
155                 // Return the instance
156                 return $actionInstance;
157         }
158
159 }