3 * A generic controller class. You should extend this base class if you want to
4 * write your own controller. You get the advantage that you can use the pre and
7 * @author Roland Haeder <webmaster@ship-simu.org>
9 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
10 * @license GNU GPL 3.0 or any newer version
11 * @link http://www.ship-simu.org
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 class BaseController extends BaseFrameworkSystem implements Registerable {
28 * Generic filter chains
30 private $filterChains = array();
32 // Exception constants
33 const EXCEPTION_FILTER_CHAIN_INVALID = 0xf10;
36 * Protected constructor
38 * @param $className Name of the class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
45 // Initialize both filter chains
46 $this->initFilterChain('pre');
47 $this->initFilterChain('post');
49 // Add this controller to the registry
50 Registry::getRegistry()->addInstance('controller', $this);
54 * Private method to initialize a given filter chain
56 * @param $filterChain Name of the filter chain
59 protected function initFilterChain ($filterChain) {
60 //* DEBUG: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ' init: START');
61 $this->filterChains[$filterChain] = ObjectFactory::createObjectByConfiguredName('filter_chain_class');
62 //* DEBUG: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ' init: FINISHED');
66 * Adds a filter to a given filter chain
68 * @param $filterChain Chain of the filter
69 * @param $filterInstance An instance of a filter
71 * @throws InvalidFilterChainException If the filter chain is invalid
73 protected function addFilter ($filterChain, Filterable $filterInstance) {
74 //* DEBUG: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: START');
76 // Test if the filter is there
77 if (!isset($this->filterChains[$filterChain])) {
78 // Throw an exception here
79 throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
83 $this->filterChains[$filterChain]->addFilter($filterInstance);
84 //* DEBUG: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: FINISH');
88 * Adds a filter to the pre filter chain
90 * @param $filterInstance An instance of a filter
93 public function addPreFilter (Filterable $filterInstance) {
95 $this->addFilter('pre', $filterInstance);
99 * Adds a filter to the post filter chain
101 * @param $filterInstance An instance of a filter
104 public function addPostFilter (Filterable $filterInstance) {
105 // Add the post filter
106 $this->addFilter('post', $filterInstance);
110 * Executes given filter chain chain
112 * @param $filterChain Chain of the filter to execute
113 * @param $requestInstance An instance of a request class
114 * @param $responseInstance An instance of a response class
116 * @throws InvalidFilterChainException If the filter chain is invalid
118 protected function executeFilters ($filterChain, Requestable $requestInstance, Responseable $responseInstance) {
119 // Test if the filter is there
120 if (!isset($this->filterChains[$filterChain])) {
121 // Throw an exception here
122 throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
126 $this->filterChains[$filterChain]->processFilters($requestInstance, $responseInstance);
130 * Executes all pre filters
132 * @param $requestInstance An instance of a request class
133 * @param $responseInstance An instance of a response class
136 protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) {
137 // Execute all pre filters
138 $this->executeFilters('pre', $requestInstance, $responseInstance);
142 * Executes all post filters
144 * @param $requestInstance An instance of a request class
145 * @param $responseInstance An instance of a response class
148 protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) {
149 // Execute all post filters
150 $this->executeFilters('post', $requestInstance, $responseInstance);