Continued:
[core.git] / framework / main / classes / controller / class_BaseController.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Controller;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Chain\Filter\InvalidFilterChainException;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Filter\Filterable;
9 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
10 use Org\Mxchange\CoreFramework\Registry\Registry;
11 use Org\Mxchange\CoreFramework\Registry\Registerable;
12 use Org\Mxchange\CoreFramework\Request\Requestable;
13 use Org\Mxchange\CoreFramework\Response\Responseable;
14
15 /**
16  * A generic controller class. You should extend this base class if you want to
17  * write your own controller. You get the advantage that you can use the pre and
18  * post filters.
19  *
20  * @author              Roland Haeder <webmaster@shipsimu.org>
21  * @version             0.0.0
22  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
23  * @license             GNU GPL 3.0 or any newer version
24  * @link                http://www.shipsimu.org
25  *
26  * This program is free software: you can redistribute it and/or modify
27  * it under the terms of the GNU General Public License as published by
28  * the Free Software Foundation, either version 3 of the License, or
29  * (at your option) any later version.
30  *
31  * This program is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  * GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License
37  * along with this program. If not, see <http://www.gnu.org/licenses/>.
38  */
39 class BaseController extends BaseFrameworkSystem implements Registerable {
40         // Exception constants
41         const EXCEPTION_FILTER_CHAIN_INVALID = 0xf10;
42
43         // Names of controller's own filter chains
44         const FILTER_CHAIN_PRE_COMMAND  = 'controller_pre_command';
45         const FILTER_CHAIN_POST_COMMAND = 'controller_post_command';
46
47         /**
48          * Generic filter chains
49          */
50         private $filterChains = array();
51
52         /**
53          * Protected constructor
54          *
55          * @param       $className      Name of the class
56          * @return      void
57          */
58         protected function __construct ($className) {
59                 // Call parent constructor
60                 parent::__construct($className);
61
62                 // Initialize both filter chains
63                 $this->initFilterChain(self::FILTER_CHAIN_PRE_COMMAND);
64                 $this->initFilterChain(self::FILTER_CHAIN_POST_COMMAND);
65
66                 // Add this controller to the registry
67                 Registry::getRegistry()->addInstance('controller', $this);
68         }
69
70         /**
71          * Executes a command with pre and post filters
72          *
73          * @param       $requestInstance        A Requestable class
74          * @param       $responseInstance       A Responseable class
75          * @return      void
76          */
77         public function executeGenericPrePostCommand (Requestable $requestInstance, Responseable $responseInstance) {
78                 // Get the command instance from the resolver by sending a request instance to the resolver
79                 $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance);
80
81                 // Add more filters by the command
82                 $commandInstance->addExtraFilters($this, $requestInstance);
83
84                 // Run the pre filters
85                 $this->executePreFilters($requestInstance, $responseInstance);
86
87                 // This request was valid! :-D
88                 $requestInstance->requestIsValid();
89
90                 // Execute the command
91                 $commandInstance->execute($requestInstance, $responseInstance);
92
93                 // Run the post filters
94                 $this->executePostFilters($requestInstance, $responseInstance);
95
96                 // Flush the response out
97                 $responseInstance->flushBuffer();
98         }
99
100         /**
101          * Handles the given request and response, redirects to login_failed if
102          * UserAuthorizationException is thrown.
103          *
104          * @param       $requestInstance        An instance of a Requestable class
105          * @param       $responseInstance       An instance of a Responseable class
106          * @return      void
107          */
108         public function genericHanleRequestLoginFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) {
109                 // Get the "form action"
110                 $formAction = $requestInstance->getRequestElement('form');
111
112                 // Get command instance from resolver
113                 $commandInstance = $this->getResolverInstance()->resolveCommand($formAction);
114
115                 // Add more filters by the command
116                 $commandInstance->addExtraFilters($this, $requestInstance);
117
118                 // Try to run the pre filters, if auth exceptions come through redirect here
119                 try {
120                         // Run the pre filters
121                         $this->executePreFilters($requestInstance, $responseInstance);
122                 } catch (UserAuthorizationException $e) {
123                         // Redirect to main page
124                         $responseInstance->redirectToConfiguredUrl('login_failed');
125
126                         // Exit here
127                         exit();
128                 }
129
130                 /*
131                  * Is the request still valid? Post filters shall only be executed of
132                  * the request is valid
133                  */
134                 if ($requestInstance->isRequestValid()) {
135                         // Execute the command
136                         $commandInstance->execute($requestInstance, $responseInstance);
137
138                         // Execute *very* generic post filters
139                         $this->executePostFilters($requestInstance, $responseInstance);
140                 } // END - if
141
142                 // Flush the buffer out
143                 $responseInstance->flushBuffer();
144         }
145
146         /**
147          * Generic execute of the command: pre and post filters with redirect
148          * but request becomes valid after pre-filters run.
149          *
150          * @param       $requestInstance        An instance of a Requestable class
151          * @param       $responseInstance       An instance of a Responseable class
152          * @return      void
153          */
154         public function genericHanleRequestLoginAreaFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) {
155                 // Get the command instance from the resolver by sending a request instance to the resolver
156                 $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance);
157
158                 // Add more filters by the command
159                 $commandInstance->addExtraFilters($this, $requestInstance);
160
161                 // Try to run the pre filters, if auth exceptions come through redirect here
162                 try {
163                         // Run the pre filters
164                         $this->executePreFilters($requestInstance, $responseInstance);
165                 } catch (UserAuthorizationException $e) {
166                         // Redirect to main page
167                         $responseInstance->redirectToConfiguredUrl('login_failed');
168
169                         // Exit here
170                         exit();
171                 }
172
173                 // This request was valid! :-D
174                 $requestInstance->requestIsValid();
175
176                 // Execute the command
177                 $commandInstance->execute($requestInstance, $responseInstance);
178
179                 // Run the post filters
180                 $this->executePostFilters($requestInstance, $responseInstance);
181
182                 // Flush the response out
183                 $responseInstance->flushBuffer();
184         }
185
186         /**
187          * Private method to initialize a given filter chain
188          *
189          * @param       $filterChain    Name of the filter chain
190          * @return      void
191          */
192         protected function initFilterChain ($filterChain) {
193                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: START');
194                 $this->filterChains[$filterChain] = ObjectFactory::createObjectByConfiguredName('filter_chain_class');
195                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: FINISHED');
196         }
197
198         /**
199          * Adds a filter to a given filter chain
200          *
201          * @param       $filterChain    Chain of the filter
202          * @param       $filterInstance         An instance of a filter
203          * @return      void
204          * @throws      InvalidFilterChainException     If the filter chain is invalid
205          */
206         protected function addFilter ($filterChain, Filterable $filterInstance) {
207                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: START');
208
209                 // Test if the filter is there
210                 if (!isset($this->filterChains[$filterChain])) {
211                         // Throw an exception here
212                         throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
213                 } // END - if
214
215                 // Add the filter
216                 $this->filterChains[$filterChain]->addFilter($filterInstance);
217                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: FINISH');
218         }
219
220         /**
221          * Adds a filter to the pre filter chain
222          *
223          * @param       $filterInstance         An instance of a filter
224          * @return      void
225          */
226         public function addPreFilter (Filterable $filterInstance) {
227                 // Add the pre filter
228                 $this->addFilter(self::FILTER_CHAIN_PRE_COMMAND, $filterInstance);
229         }
230
231         /**
232          * Adds a filter to the post filter chain
233          *
234          * @param       $filterInstance         An instance of a filter
235          * @return      void
236          */
237         public function addPostFilter (Filterable $filterInstance) {
238                 // Add the post filter
239                 $this->addFilter(self::FILTER_CHAIN_POST_COMMAND, $filterInstance);
240         }
241
242         /**
243          * Add a shutdown filter
244          *
245          * @param       $filterInstance         A Filterable class
246          * @return      void
247          */
248         public function addShutdownFilter (Filterable $filterInstance) {
249                 $this->addFilter('shutdown', $filterInstance);
250         }
251
252         /**
253          * Executes given filter chain chain
254          *
255          * @param       $filterChain            Chain of the filter to execute
256          * @param       $requestInstance        An instance of a Requestable class
257          * @param       $responseInstance       An instance of a Responseable class
258          * @return      void
259          * @throws      InvalidFilterChainException     If the filter chain is invalid
260          */
261         protected function executeFilters ($filterChain, Requestable $requestInstance, Responseable $responseInstance) {
262                 // Test if the filter is there
263                 if (!isset($this->filterChains[$filterChain])) {
264                         // Throw an exception here
265                         throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
266                 } // END - if
267
268                 // Run all filters
269                 $this->filterChains[$filterChain]->processFilters($requestInstance, $responseInstance);
270         }
271
272         /**
273          * Executes all pre filters
274          *
275          * @param       $requestInstance        An instance of a Requestable class
276          * @param       $responseInstance       An instance of a Responseable class
277          * @return      void
278          */
279         protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) {
280                 // Execute all pre filters
281                 $this->executeFilters(self::FILTER_CHAIN_PRE_COMMAND, $requestInstance, $responseInstance);
282         }
283
284         /**
285          * Executes all post filters
286          *
287          * @param       $requestInstance        An instance of a Requestable class
288          * @param       $responseInstance       An instance of a Responseable class
289          * @return      void
290          */
291         protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) {
292                 // Execute all post filters
293                 $this->executeFilters(self::FILTER_CHAIN_POST_COMMAND, $requestInstance, $responseInstance);
294         }
295
296         /**
297          * Executes all shutdown filters
298          *
299          * @param       $requestInstance        A Requestable class
300          * @param       $responseInstance       A Responseable class
301          * @return      void
302          */
303         public function executeShutdownFilters (Requestable $requestInstance, Responseable $responseInstance) {
304                 $this->executeFilters('shutdown', $requestInstance, $responseInstance);
305         }
306
307 }