3 namespace Org\Mxchange\CoreFramework\Controller;
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\GenericRegistry;
11 use Org\Mxchange\CoreFramework\Registry\Registerable;
12 use Org\Mxchange\CoreFramework\Request\Requestable;
13 use Org\Mxchange\CoreFramework\Resolver\Resolver;
14 use Org\Mxchange\CoreFramework\Response\Responseable;
17 * A generic controller class. You should extend this base class if you want to
18 * write your own controller. You get the advantage that you can use the pre and
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 abstract class BaseController extends BaseFrameworkSystem implements Registerable {
41 // Exception constants
42 const EXCEPTION_FILTER_CHAIN_INVALID = 0xf10;
44 // Names of controller's own filter chains
45 const FILTER_CHAIN_PRE_COMMAND = 'controller_pre_command';
46 const FILTER_CHAIN_POST_COMMAND = 'controller_post_command';
51 private $resolverInstance = NULL;
54 * Generic filter chains
56 private $filterChains = [];
59 * Protected constructor
61 * @param $className Name of the class
64 protected function __construct (string $className) {
65 // Call parent constructor
66 parent::__construct($className);
68 // Initialize both filter chains
69 $this->initFilterChain(self::FILTER_CHAIN_PRE_COMMAND);
70 $this->initFilterChain(self::FILTER_CHAIN_POST_COMMAND);
72 // Add this controller to the registry
73 GenericRegistry::getRegistry()->addInstance('controller', $this);
77 * Setter for resolver instance
79 * @param $resolverInstance Instance of a command resolver class
82 protected final function setResolverInstance (Resolver $resolverInstance) {
83 $this->resolverInstance = $resolverInstance;
87 * Getter for resolver instance
89 * @return $resolverInstance Instance of a command resolver class
91 protected final function getResolverInstance () {
92 return $this->resolverInstance;
96 * Executes a command with pre and post filters
98 * @param $requestInstance A Requestable class
99 * @param $responseInstance A Responseable class
102 public function executeGenericPrePostCommand (Requestable $requestInstance, Responseable $responseInstance) {
103 // Get the command instance from the resolver by sending a request instance to the resolver
104 $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance);
106 // Add more filters by the command
107 $commandInstance->addExtraFilters($this, $requestInstance);
109 // Run the pre filters
110 $this->executePreFilters($requestInstance, $responseInstance);
112 // This request was valid! :-D
113 $requestInstance->requestIsValid();
115 // Execute the command
116 $commandInstance->execute($requestInstance, $responseInstance);
118 // Run the post filters
119 $this->executePostFilters($requestInstance, $responseInstance);
121 // Flush the response out
122 $responseInstance->flushBuffer();
126 * Handles the given request and response, redirects to login_failed if
127 * UserAuthorizationException is thrown.
129 * @param $requestInstance An instance of a Requestable class
130 * @param $responseInstance An instance of a Responseable class
133 public function genericHanleRequestLoginFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) {
134 // Get the "form action"
135 $formAction = $requestInstance->getRequestElement('form');
137 // Get command instance from resolver
138 $commandInstance = $this->getResolverInstance()->resolveCommand('Org\Mxchange\CoreFramework\Command\Failed', $formAction);
140 // Add more filters by the command
141 $commandInstance->addExtraFilters($this, $requestInstance);
143 // Try to run the pre filters, if auth exceptions come through redirect here
145 // Run the pre filters
146 $this->executePreFilters($requestInstance, $responseInstance);
147 } catch (UserAuthorizationException $e) {
148 // Redirect to main page
149 $responseInstance->redirectToConfiguredUrl('login_failed');
156 * Is the request still valid? Post filters shall only be executed of
157 * the request is valid
159 if ($requestInstance->isRequestValid()) {
160 // Execute the command
161 $commandInstance->execute($requestInstance, $responseInstance);
163 // Execute *very* generic post filters
164 $this->executePostFilters($requestInstance, $responseInstance);
167 // Flush the buffer out
168 $responseInstance->flushBuffer();
172 * Generic execute of the command: pre and post filters with redirect
173 * but request becomes valid after pre-filters run.
175 * @param $requestInstance An instance of a Requestable class
176 * @param $responseInstance An instance of a Responseable class
179 public function genericHanleRequestLoginAreaFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) {
180 // Get the command instance from the resolver by sending a request instance to the resolver
181 $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance);
183 // Add more filters by the command
184 $commandInstance->addExtraFilters($this, $requestInstance);
186 // Try to run the pre filters, if auth exceptions come through redirect here
188 // Run the pre filters
189 $this->executePreFilters($requestInstance, $responseInstance);
190 } catch (UserAuthorizationException $e) {
191 // Redirect to main page
192 $responseInstance->redirectToConfiguredUrl('login_failed');
198 // This request was valid! :-D
199 $requestInstance->requestIsValid();
201 // Execute the command
202 $commandInstance->execute($requestInstance, $responseInstance);
204 // Run the post filters
205 $this->executePostFilters($requestInstance, $responseInstance);
207 // Flush the response out
208 $responseInstance->flushBuffer();
212 * Private method to initialize a given filter chain
214 * @param $filterChain Name of the filter chain
217 protected function initFilterChain ($filterChain) {
218 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: START');
219 $this->filterChains[$filterChain] = ObjectFactory::createObjectByConfiguredName('filter_chain_class');
220 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: FINISHED');
224 * Adds a filter to a given filter chain
226 * @param $filterChain Chain of the filter
227 * @param $filterInstance An instance of a filter
229 * @throws InvalidFilterChainException If the filter chain is invalid
231 protected function addFilter ($filterChain, Filterable $filterInstance) {
232 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: START');
234 // Test if the filter is there
235 if (!isset($this->filterChains[$filterChain])) {
236 // Throw an exception here
237 throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
241 $this->filterChains[$filterChain]->addFilter($filterInstance);
242 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: FINISH');
246 * Adds a filter to the pre filter chain
248 * @param $filterInstance An instance of a filter
251 public function addPreFilter (Filterable $filterInstance) {
252 // Add the pre filter
253 $this->addFilter(self::FILTER_CHAIN_PRE_COMMAND, $filterInstance);
257 * Adds a filter to the post filter chain
259 * @param $filterInstance An instance of a filter
262 public function addPostFilter (Filterable $filterInstance) {
263 // Add the post filter
264 $this->addFilter(self::FILTER_CHAIN_POST_COMMAND, $filterInstance);
268 * Add a shutdown filter
270 * @param $filterInstance A Filterable class
273 public function addShutdownFilter (Filterable $filterInstance) {
274 $this->addFilter('shutdown', $filterInstance);
278 * Executes given filter chain chain
280 * @param $filterChain Chain of the filter to execute
281 * @param $requestInstance An instance of a Requestable class
282 * @param $responseInstance An instance of a Responseable class
284 * @throws InvalidFilterChainException If the filter chain is invalid
286 protected function executeFilters ($filterChain, Requestable $requestInstance, Responseable $responseInstance) {
287 // Test if the filter is there
288 if (!isset($this->filterChains[$filterChain])) {
289 // Throw an exception here
290 throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
294 $this->filterChains[$filterChain]->processFilters($requestInstance, $responseInstance);
298 * Executes all pre filters
300 * @param $requestInstance An instance of a Requestable class
301 * @param $responseInstance An instance of a Responseable class
304 protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) {
305 // Execute all pre filters
306 $this->executeFilters(self::FILTER_CHAIN_PRE_COMMAND, $requestInstance, $responseInstance);
310 * Executes all post filters
312 * @param $requestInstance An instance of a Requestable class
313 * @param $responseInstance An instance of a Responseable class
316 protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) {
317 // Execute all post filters
318 $this->executeFilters(self::FILTER_CHAIN_POST_COMMAND, $requestInstance, $responseInstance);
322 * Executes all shutdown filters
324 * @param $requestInstance A Requestable class
325 * @param $responseInstance A Responseable class
328 public function executeShutdownFilters (Requestable $requestInstance, Responseable $responseInstance) {
329 $this->executeFilters('shutdown', $requestInstance, $responseInstance);