3 namespace CoreFramework\Controller;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Object\BaseFrameworkSystem;
8 use CoreFramework\Registry\Registry;
9 use CoreFramework\Registry\Registerable;
10 use CoreFramework\Request\Requestable;
11 use CoreFramework\Response\Responseable;
14 * A generic controller class. You should extend this base class if you want to
15 * write your own controller. You get the advantage that you can use the pre and
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class BaseController extends BaseFrameworkSystem implements Registerable {
38 // Exception constants
39 const EXCEPTION_FILTER_CHAIN_INVALID = 0xf10;
41 // Names of controller's own filter chains
42 const FILTER_CHAIN_PRE_COMMAND = 'controller_pre_command';
43 const FILTER_CHAIN_POST_COMMAND = 'controller_post_command';
46 * Generic filter chains
48 private $filterChains = array();
51 * Protected constructor
53 * @param $className Name of the class
56 protected function __construct ($className) {
57 // Call parent constructor
58 parent::__construct($className);
60 // Initialize both filter chains
61 $this->initFilterChain(self::FILTER_CHAIN_PRE_COMMAND);
62 $this->initFilterChain(self::FILTER_CHAIN_POST_COMMAND);
64 // Add this controller to the registry
65 Registry::getRegistry()->addInstance('controller', $this);
69 * Executes a command with pre and post filters
71 * @param $requestInstance A Requestable class
72 * @param $responseInstance A Responseable class
75 public function executeGenericPrePostCommand (Requestable $requestInstance, Responseable $responseInstance) {
76 // Get the command instance from the resolver by sending a request instance to the resolver
77 $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance);
79 // Add more filters by the command
80 $commandInstance->addExtraFilters($this, $requestInstance);
82 // Run the pre filters
83 $this->executePreFilters($requestInstance, $responseInstance);
85 // This request was valid! :-D
86 $requestInstance->requestIsValid();
88 // Execute the command
89 $commandInstance->execute($requestInstance, $responseInstance);
91 // Run the post filters
92 $this->executePostFilters($requestInstance, $responseInstance);
94 // Flush the response out
95 $responseInstance->flushBuffer();
99 * Handles the given request and response, redirects to login_failed if
100 * UserAuthorizationException is thrown.
102 * @param $requestInstance An instance of a Requestable class
103 * @param $responseInstance An instance of a Responseable class
106 public function genericHanleRequestLoginFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) {
107 // Get the "form action"
108 $formAction = $requestInstance->getRequestElement('form');
110 // Get command instance from resolver
111 $commandInstance = $this->getResolverInstance()->resolveCommand($formAction);
113 // Add more filters by the command
114 $commandInstance->addExtraFilters($this, $requestInstance);
116 // Try to run the pre filters, if auth exceptions come through redirect here
118 // Run the pre filters
119 $this->executePreFilters($requestInstance, $responseInstance);
120 } catch (UserAuthorizationException $e) {
121 // Redirect to main page
122 $responseInstance->redirectToConfiguredUrl('login_failed');
129 * Is the request still valid? Post filters shall only be executed of
130 * the request is valid
132 if ($requestInstance->isRequestValid()) {
133 // Execute the command
134 $commandInstance->execute($requestInstance, $responseInstance);
136 // Execute *very* generic ppost filters
137 $this->executePostFilters($requestInstance, $responseInstance);
140 // Flush the buffer out
141 $responseInstance->flushBuffer();
145 * Generic execute of the command: pre and post filters with redirect
146 * but request becomes valid after pre-filters run.
148 * @param $requestInstance An instance of a Requestable class
149 * @param $responseInstance An instance of a Responseable class
152 public function genericHanleRequestLoginAreaFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) {
153 // Get the command instance from the resolver by sending a request instance to the resolver
154 $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance);
156 // Add more filters by the command
157 $commandInstance->addExtraFilters($this, $requestInstance);
159 // Try to run the pre filters, if auth exceptions come through redirect here
161 // Run the pre filters
162 $this->executePreFilters($requestInstance, $responseInstance);
163 } catch (UserAuthorizationException $e) {
164 // Redirect to main page
165 $responseInstance->redirectToConfiguredUrl('login_failed');
171 // This request was valid! :-D
172 $requestInstance->requestIsValid();
174 // Execute the command
175 $commandInstance->execute($requestInstance, $responseInstance);
177 // Run the pre filters
178 $this->executePostFilters($requestInstance, $responseInstance);
180 // Flush the response out
181 $responseInstance->flushBuffer();
185 * Private method to initialize a given filter chain
187 * @param $filterChain Name of the filter chain
190 protected function initFilterChain ($filterChain) {
191 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: START');
192 $this->filterChains[$filterChain] = ObjectFactory::createObjectByConfiguredName('filter_chain_class');
193 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: FINISHED');
197 * Adds a filter to a given filter chain
199 * @param $filterChain Chain of the filter
200 * @param $filterInstance An instance of a filter
202 * @throws InvalidFilterChainException If the filter chain is invalid
204 protected function addFilter ($filterChain, Filterable $filterInstance) {
205 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: START');
207 // Test if the filter is there
208 if (!isset($this->filterChains[$filterChain])) {
209 // Throw an exception here
210 throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
214 $this->filterChains[$filterChain]->addFilter($filterInstance);
215 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: FINISH');
219 * Adds a filter to the pre filter chain
221 * @param $filterInstance An instance of a filter
224 public function addPreFilter (Filterable $filterInstance) {
225 // Add the pre filter
226 $this->addFilter(self::FILTER_CHAIN_PRE_COMMAND, $filterInstance);
230 * Adds a filter to the post filter chain
232 * @param $filterInstance An instance of a filter
235 public function addPostFilter (Filterable $filterInstance) {
236 // Add the post filter
237 $this->addFilter(self::FILTER_CHAIN_POST_COMMAND, $filterInstance);
241 * Add a shutdown filter
243 * @param $filterInstance A Filterable class
246 public function addShutdownFilter (Filterable $filterInstance) {
247 $this->addFilter('shutdown', $filterInstance);
251 * Executes given filter chain chain
253 * @param $filterChain Chain of the filter to execute
254 * @param $requestInstance An instance of a Requestable class
255 * @param $responseInstance An instance of a Responseable class
257 * @throws InvalidFilterChainException If the filter chain is invalid
259 protected function executeFilters ($filterChain, Requestable $requestInstance, Responseable $responseInstance) {
260 // Test if the filter is there
261 if (!isset($this->filterChains[$filterChain])) {
262 // Throw an exception here
263 throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
267 $this->filterChains[$filterChain]->processFilters($requestInstance, $responseInstance);
271 * Executes all pre filters
273 * @param $requestInstance An instance of a Requestable class
274 * @param $responseInstance An instance of a Responseable class
277 protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) {
278 // Execute all pre filters
279 $this->executeFilters(self::FILTER_CHAIN_PRE_COMMAND, $requestInstance, $responseInstance);
283 * Executes all post filters
285 * @param $requestInstance An instance of a Requestable class
286 * @param $responseInstance An instance of a Responseable class
289 protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) {
290 // Execute all post filters
291 $this->executeFilters(self::FILTER_CHAIN_POST_COMMAND, $requestInstance, $responseInstance);
295 * Executes all shutdown filters
297 * @param $requestInstance A Requestable class
298 * @param $responseInstance A Responseable class
301 public function executeShutdownFilters (Requestable $requestInstance, Responseable $responseInstance) {
302 $this->executeFilters('shutdown', $requestInstance, $responseInstance);