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