Copyright updated
[core.git] / inc / classes / main / controller / class_BaseController.php
1 <?php
2 /**
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
5  * post filters.
6  *
7  * @author              Roland Haeder <webmaster@ship-simu.org>
8  * @version             0.0.0
9  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
10  * @license             GNU GPL 3.0 or any newer version
11  * @link                http://www.ship-simu.org
12  *
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.
17  *
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.
22  *
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/>.
25  */
26 class BaseController extends BaseFrameworkSystem implements Registerable {
27         // Exception constants
28         const EXCEPTION_FILTER_CHAIN_INVALID = 0xf10;
29
30         // Names of controller's own filter chains
31         const FILTER_CHAIN_PRE_COMMAND  = 'controller_pre_command';
32         const FILTER_CHAIN_POST_COMMAND = 'controller_post_command';
33
34         /**
35          * Generic filter chains
36          */
37         private $filterChains = array();
38
39         /**
40          * Protected constructor
41          *
42          * @param       $className      Name of the class
43          * @return      void
44          */
45         protected function __construct ($className) {
46                 // Call parent constructor
47                 parent::__construct($className);
48
49                 // Initialize both filter chains
50                 $this->initFilterChain(self::FILTER_CHAIN_PRE_COMMAND);
51                 $this->initFilterChain(self::FILTER_CHAIN_POST_COMMAND);
52
53                 // Add this controller to the registry
54                 Registry::getRegistry()->addInstance('controller', $this);
55         }
56
57         /**
58          * Private method to initialize a given filter chain
59          *
60          * @param       $filterChain    Name of the filter chain
61          * @return      void
62          */
63         protected function initFilterChain ($filterChain) {
64                 //* DEBUG: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ' init: START');
65                 $this->filterChains[$filterChain] = ObjectFactory::createObjectByConfiguredName('filter_chain_class');
66                 //* DEBUG: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ' init: FINISHED');
67         }
68
69         /**
70          * Adds a filter to a given filter chain
71          *
72          * @param       $filterChain    Chain of the filter
73          * @param       $filterInstance         An instance of a filter
74          * @return      void
75          * @throws      InvalidFilterChainException     If the filter chain is invalid
76          */
77         protected function addFilter ($filterChain, Filterable $filterInstance) {
78                 //* DEBUG: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: START');
79
80                 // Test if the filter is there
81                 if (!isset($this->filterChains[$filterChain])) {
82                         // Throw an exception here
83                         throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
84                 } // END - if
85
86                 // Add the filter
87                 $this->filterChains[$filterChain]->addFilter($filterInstance);
88                 //* DEBUG: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: FINISH');
89         }
90
91         /**
92          * Adds a filter to the pre filter chain
93          *
94          * @param       $filterInstance         An instance of a filter
95          * @return      void
96          */
97         public function addPreFilter (Filterable $filterInstance) {
98                 // Add the pre filter
99                 $this->addFilter(self::FILTER_CHAIN_PRE_COMMAND, $filterInstance);
100         }
101
102         /**
103          * Adds a filter to the post filter chain
104          *
105          * @param       $filterInstance         An instance of a filter
106          * @return      void
107          */
108         public function addPostFilter (Filterable $filterInstance) {
109                 // Add the post filter
110                 $this->addFilter(self::FILTER_CHAIN_POST_COMMAND, $filterInstance);
111         }
112
113         /**
114          * Executes given filter chain chain
115          *
116          * @param       $filterChain            Chain of the filter to execute
117          * @param       $requestInstance        An instance of a request class
118          * @param       $responseInstance       An instance of a response class
119          * @return      void
120          * @throws      InvalidFilterChainException     If the filter chain is invalid
121          */
122         protected function executeFilters ($filterChain, Requestable $requestInstance, Responseable $responseInstance) {
123                 // Test if the filter is there
124                 if (!isset($this->filterChains[$filterChain])) {
125                         // Throw an exception here
126                         throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
127                 } // END - if
128
129                 // Run all filters
130                 $this->filterChains[$filterChain]->processFilters($requestInstance, $responseInstance);
131         }
132
133         /**
134          * Executes all pre filters
135          *
136          * @param       $requestInstance        An instance of a request class
137          * @param       $responseInstance       An instance of a response class
138          * @return      void
139          */
140         protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) {
141                 // Execute all pre filters
142                 $this->executeFilters(self::FILTER_CHAIN_PRE_COMMAND, $requestInstance, $responseInstance);
143         }
144
145         /**
146          * Executes all post filters
147          *
148          * @param       $requestInstance        An instance of a request class
149          * @param       $responseInstance       An instance of a response class
150          * @return      void
151          */
152         protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) {
153                 // Execute all post filters
154                 $this->executeFilters(self::FILTER_CHAIN_POST_COMMAND, $requestInstance, $responseInstance);
155         }
156 }
157
158 // [EOF]
159 ?>