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