3 namespace CoreFramework\Controller;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Object\BaseFrameworkSystem;
8 use CoreFramework\Registry\Generic\Registry;
11 * A generic controller class. You should extend this base class if you want to
12 * write your own controller. You get the advantage that you can use the pre and
15 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
34 class BaseController extends BaseFrameworkSystem implements Registerable {
35 // Exception constants
36 const EXCEPTION_FILTER_CHAIN_INVALID = 0xf10;
38 // Names of controller's own filter chains
39 const FILTER_CHAIN_PRE_COMMAND = 'controller_pre_command';
40 const FILTER_CHAIN_POST_COMMAND = 'controller_post_command';
43 * Generic filter chains
45 private $filterChains = array();
48 * Protected constructor
50 * @param $className Name of the class
53 protected function __construct ($className) {
54 // Call parent constructor
55 parent::__construct($className);
57 // Initialize both filter chains
58 $this->initFilterChain(self::FILTER_CHAIN_PRE_COMMAND);
59 $this->initFilterChain(self::FILTER_CHAIN_POST_COMMAND);
61 // Add this controller to the registry
62 Registry::getRegistry()->addInstance('controller', $this);
66 * Executes a command with pre and post filters
68 * @param $requestInstance A Requestable class
69 * @param $responseInstance A Responseable class
72 public function executeGenericPrePostCommand (Requestable $requestInstance, Responseable $responseInstance) {
73 // Get the command instance from the resolver by sending a request instance to the resolver
74 $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance);
76 // Add more filters by the command
77 $commandInstance->addExtraFilters($this, $requestInstance);
79 // Run the pre filters
80 $this->executePreFilters($requestInstance, $responseInstance);
82 // This request was valid! :-D
83 $requestInstance->requestIsValid();
85 // Execute the command
86 $commandInstance->execute($requestInstance, $responseInstance);
88 // Run the post filters
89 $this->executePostFilters($requestInstance, $responseInstance);
91 // Flush the response out
92 $responseInstance->flushBuffer();
96 * Handles the given request and response, redirects to login_failed if
97 * UserAuthorizationException is thrown.
99 * @param $requestInstance An instance of a request class
100 * @param $responseInstance An instance of a response class
103 public function genericHanleRequestLoginFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) {
104 // Get the "form action"
105 $formAction = $requestInstance->getRequestElement('form');
107 // Get command instance from resolver
108 $commandInstance = $this->getResolverInstance()->resolveCommand($formAction);
110 // Add more filters by the command
111 $commandInstance->addExtraFilters($this, $requestInstance);
113 // Try to run the pre filters, if auth exceptions come through redirect here
115 // Run the pre filters
116 $this->executePreFilters($requestInstance, $responseInstance);
117 } catch (UserAuthorizationException $e) {
118 // Redirect to main page
119 $responseInstance->redirectToConfiguredUrl('login_failed');
126 * Is the request still valid? Post filters shall only be executed of
127 * the request is valid
129 if ($requestInstance->isRequestValid()) {
130 // Execute the command
131 $commandInstance->execute($requestInstance, $responseInstance);
133 // Execute *very* generic ppost filters
134 $this->executePostFilters($requestInstance, $responseInstance);
137 // Flush the buffer out
138 $responseInstance->flushBuffer();
142 * Generic execute of the command: pre and post filters with redirect
143 * but request becomes valid after pre-filters run.
145 * @param $requestInstance An instance of a request class
146 * @param $responseInstance An instance of a response class
149 public function genericHanleRequestLoginAreaFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) {
150 // Get the command instance from the resolver by sending a request instance to the resolver
151 $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance);
153 // Add more filters by the command
154 $commandInstance->addExtraFilters($this, $requestInstance);
156 // Try to run the pre filters, if auth exceptions come through redirect here
158 // Run the pre filters
159 $this->executePreFilters($requestInstance, $responseInstance);
160 } catch (UserAuthorizationException $e) {
161 // Redirect to main page
162 $responseInstance->redirectToConfiguredUrl('login_failed');
168 // This request was valid! :-D
169 $requestInstance->requestIsValid();
171 // Execute the command
172 $commandInstance->execute($requestInstance, $responseInstance);
174 // Run the pre filters
175 $this->executePostFilters($requestInstance, $responseInstance);
177 // Flush the response out
178 $responseInstance->flushBuffer();
182 * Private method to initialize a given filter chain
184 * @param $filterChain Name of the filter chain
187 protected function initFilterChain ($filterChain) {
188 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: START');
189 $this->filterChains[$filterChain] = ObjectFactory::createObjectByConfiguredName('filter_chain_class');
190 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: FINISHED');
194 * Adds a filter to a given filter chain
196 * @param $filterChain Chain of the filter
197 * @param $filterInstance An instance of a filter
199 * @throws InvalidFilterChainException If the filter chain is invalid
201 protected function addFilter ($filterChain, Filterable $filterInstance) {
202 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: START');
204 // Test if the filter is there
205 if (!isset($this->filterChains[$filterChain])) {
206 // Throw an exception here
207 throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
211 $this->filterChains[$filterChain]->addFilter($filterInstance);
212 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: FINISH');
216 * Adds a filter to the pre filter chain
218 * @param $filterInstance An instance of a filter
221 public function addPreFilter (Filterable $filterInstance) {
222 // Add the pre filter
223 $this->addFilter(self::FILTER_CHAIN_PRE_COMMAND, $filterInstance);
227 * Adds a filter to the post filter chain
229 * @param $filterInstance An instance of a filter
232 public function addPostFilter (Filterable $filterInstance) {
233 // Add the post filter
234 $this->addFilter(self::FILTER_CHAIN_POST_COMMAND, $filterInstance);
238 * Add a shutdown filter
240 * @param $filterInstance A Filterable class
243 public function addShutdownFilter (Filterable $filterInstance) {
244 $this->addFilter('shutdown', $filterInstance);
248 * Executes given filter chain chain
250 * @param $filterChain Chain of the filter to execute
251 * @param $requestInstance An instance of a request class
252 * @param $responseInstance An instance of a response class
254 * @throws InvalidFilterChainException If the filter chain is invalid
256 protected function executeFilters ($filterChain, Requestable $requestInstance, Responseable $responseInstance) {
257 // Test if the filter is there
258 if (!isset($this->filterChains[$filterChain])) {
259 // Throw an exception here
260 throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
264 $this->filterChains[$filterChain]->processFilters($requestInstance, $responseInstance);
268 * Executes all pre filters
270 * @param $requestInstance An instance of a request class
271 * @param $responseInstance An instance of a response class
274 protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) {
275 // Execute all pre filters
276 $this->executeFilters(self::FILTER_CHAIN_PRE_COMMAND, $requestInstance, $responseInstance);
280 * Executes all post filters
282 * @param $requestInstance An instance of a request class
283 * @param $responseInstance An instance of a response class
286 protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) {
287 // Execute all post filters
288 $this->executeFilters(self::FILTER_CHAIN_POST_COMMAND, $requestInstance, $responseInstance);
292 * Executes all shutdown filters
294 * @param $requestInstance A Requestable class
295 * @param $responseInstance A Responseable class
298 public function executeShutdownFilters (Requestable $requestInstance, Responseable $responseInstance) {
299 $this->executeFilters('shutdown', $requestInstance, $responseInstance);