3 namespace CoreFramework\Controller;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7 use CoreFramework\Registry\Generic\Registry;
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
14 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
33 class BaseController extends BaseFrameworkSystem implements Registerable {
34 // Exception constants
35 const EXCEPTION_FILTER_CHAIN_INVALID = 0xf10;
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';
42 * Generic filter chains
44 private $filterChains = array();
47 * Protected constructor
49 * @param $className Name of the class
52 protected function __construct ($className) {
53 // Call parent constructor
54 parent::__construct($className);
56 // Initialize both filter chains
57 $this->initFilterChain(self::FILTER_CHAIN_PRE_COMMAND);
58 $this->initFilterChain(self::FILTER_CHAIN_POST_COMMAND);
60 // Add this controller to the registry
61 Registry::getRegistry()->addInstance('controller', $this);
65 * Executes a command with pre and post filters
67 * @param $requestInstance A Requestable class
68 * @param $responseInstance A Responseable class
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);
75 // Add more filters by the command
76 $commandInstance->addExtraFilters($this, $requestInstance);
78 // Run the pre filters
79 $this->executePreFilters($requestInstance, $responseInstance);
81 // This request was valid! :-D
82 $requestInstance->requestIsValid();
84 // Execute the command
85 $commandInstance->execute($requestInstance, $responseInstance);
87 // Run the post filters
88 $this->executePostFilters($requestInstance, $responseInstance);
90 // Flush the response out
91 $responseInstance->flushBuffer();
95 * Handles the given request and response, redirects to login_failed if
96 * UserAuthorizationException is thrown.
98 * @param $requestInstance An instance of a request class
99 * @param $responseInstance An instance of a response class
102 public function genericHanleRequestLoginFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) {
103 // Get the "form action"
104 $formAction = $requestInstance->getRequestElement('form');
106 // Get command instance from resolver
107 $commandInstance = $this->getResolverInstance()->resolveCommand($formAction);
109 // Add more filters by the command
110 $commandInstance->addExtraFilters($this, $requestInstance);
112 // Try to run the pre filters, if auth exceptions come through redirect here
114 // Run the pre filters
115 $this->executePreFilters($requestInstance, $responseInstance);
116 } catch (UserAuthorizationException $e) {
117 // Redirect to main page
118 $responseInstance->redirectToConfiguredUrl('login_failed');
125 * Is the request still valid? Post filters shall only be executed of
126 * the request is valid
128 if ($requestInstance->isRequestValid()) {
129 // Execute the command
130 $commandInstance->execute($requestInstance, $responseInstance);
132 // Execute *very* generic ppost filters
133 $this->executePostFilters($requestInstance, $responseInstance);
136 // Flush the buffer out
137 $responseInstance->flushBuffer();
141 * Generic execute of the command: pre and post filters with redirect
142 * but request becomes valid after pre-filters run.
144 * @param $requestInstance An instance of a request class
145 * @param $responseInstance An instance of a response class
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);
152 // Add more filters by the command
153 $commandInstance->addExtraFilters($this, $requestInstance);
155 // Try to run the pre filters, if auth exceptions come through redirect here
157 // Run the pre filters
158 $this->executePreFilters($requestInstance, $responseInstance);
159 } catch (UserAuthorizationException $e) {
160 // Redirect to main page
161 $responseInstance->redirectToConfiguredUrl('login_failed');
167 // This request was valid! :-D
168 $requestInstance->requestIsValid();
170 // Execute the command
171 $commandInstance->execute($requestInstance, $responseInstance);
173 // Run the pre filters
174 $this->executePostFilters($requestInstance, $responseInstance);
176 // Flush the response out
177 $responseInstance->flushBuffer();
181 * Private method to initialize a given filter chain
183 * @param $filterChain Name of the filter chain
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');
193 * Adds a filter to a given filter chain
195 * @param $filterChain Chain of the filter
196 * @param $filterInstance An instance of a filter
198 * @throws InvalidFilterChainException If the filter chain is invalid
200 protected function addFilter ($filterChain, Filterable $filterInstance) {
201 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: START');
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);
210 $this->filterChains[$filterChain]->addFilter($filterInstance);
211 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: FINISH');
215 * Adds a filter to the pre filter chain
217 * @param $filterInstance An instance of a filter
220 public function addPreFilter (Filterable $filterInstance) {
221 // Add the pre filter
222 $this->addFilter(self::FILTER_CHAIN_PRE_COMMAND, $filterInstance);
226 * Adds a filter to the post filter chain
228 * @param $filterInstance An instance of a filter
231 public function addPostFilter (Filterable $filterInstance) {
232 // Add the post filter
233 $this->addFilter(self::FILTER_CHAIN_POST_COMMAND, $filterInstance);
237 * Add a shutdown filter
239 * @param $filterInstance A Filterable class
242 public function addShutdownFilter (Filterable $filterInstance) {
243 $this->addFilter('shutdown', $filterInstance);
247 * Executes given filter chain chain
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
253 * @throws InvalidFilterChainException If the filter chain is invalid
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);
263 $this->filterChains[$filterChain]->processFilters($requestInstance, $responseInstance);
267 * Executes all pre filters
269 * @param $requestInstance An instance of a request class
270 * @param $responseInstance An instance of a response class
273 protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) {
274 // Execute all pre filters
275 $this->executeFilters(self::FILTER_CHAIN_PRE_COMMAND, $requestInstance, $responseInstance);
279 * Executes all post filters
281 * @param $requestInstance An instance of a request class
282 * @param $responseInstance An instance of a response class
285 protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) {
286 // Execute all post filters
287 $this->executeFilters(self::FILTER_CHAIN_POST_COMMAND, $requestInstance, $responseInstance);
291 * Executes all shutdown filters
293 * @param $requestInstance A Requestable class
294 * @param $responseInstance A Responseable class
297 public function executeShutdownFilters (Requestable $requestInstance, Responseable $responseInstance) {
298 $this->executeFilters('shutdown', $requestInstance, $responseInstance);