1f35cb05bd3a2fa000a3770850b6566ca6079c00
[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 - 2009 Roland Haeder, this is free software
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         /**
28          * Generic filter chains
29          */
30         private $filterChains = array();
31
32         /**
33          * Protected constructor
34          *
35          * @param       $className      Name of the class
36          * @return      void
37          */
38         protected function __construct ($className) {
39                 // Call parent constructor
40                 parent::__construct($className);
41
42                 // Clean up a little
43                 $this->removeNumberFormaters();
44                 $this->removeSystemArray();
45
46                 // Initialize both filter chains
47                 $this->initFilterChain('pre');
48                 $this->initFilterChain('post');
49
50                 // Add this controller to the registry
51                 Registry::getRegistry()->addInstance('controller', $this);
52         }
53
54         /**
55          * Private method to initialize a given filter chain
56          *
57          * @param       $filterChain    Name of the filter chain
58          * @return      void
59          */
60         private function initFilterChain ($filterChain) {
61                 $this->filterChains[$filterChain] = ObjectFactory::createObjectByConfiguredName('filter_chain_class');
62         }
63
64         /**
65          * Adds a filter to the pre filter chain
66          *
67          * @param       $filterInstance         An instance of a filter
68          * @return      void
69          */
70         public function addPreFilter (Filterable $filterInstance) {
71                 // Add the pre filter
72                 $this->filterChains['pre']->addFilter($filterInstance);
73         }
74
75         /**
76          * Adds a filter to the post filter chain
77          *
78          * @param       $filterInstance         An instance of a filter
79          * @return      void
80          */
81         public function addPostFilter (Filterable $filterInstance) {
82                 // Add the post filter
83                 $this->filterChains['post']->addFilter($filterInstance);
84         }
85
86         /**
87          * Executes all pre filters
88          *
89          * @param       $requestInstance        An instance of a request class
90          * @param       $responseInstance       An instance of a response class
91          * @return      void
92          */
93         protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) {
94                 // Execute all pre filters
95                 $this->filterChains['pre']->processFilters($requestInstance, $responseInstance);
96         }
97
98         /**
99          * Executes all post filters
100          *
101          * @param       $requestInstance        An instance of a request class
102          * @param       $responseInstance       An instance of a response class
103          * @return      void
104          */
105         protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) {
106                 // Execute all post filters
107                 $this->filterChains['post']->processFilters($requestInstance, $responseInstance);
108         }
109 }
110
111 // [EOF]
112 ?>