2a1c50f97c0b5a18267c4bd067f8b1d9287941df
[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 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         /**
28          * Generic filter chains
29          */
30         private $filterChains = array();
31
32         // Exception constants
33         const EXCEPTION_FILTER_CHAIN_INVALID = 0xf10;
34
35         /**
36          * Protected constructor
37          *
38          * @param       $className      Name of the class
39          * @return      void
40          */
41         protected function __construct ($className) {
42                 // Call parent constructor
43                 parent::__construct($className);
44
45                 // Clean up a little
46                 $this->removeNumberFormaters();
47                 $this->removeSystemArray();
48
49                 // Initialize both filter chains
50                 $this->initFilterChain('pre');
51                 $this->initFilterChain('post');
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                 $this->filterChains[$filterChain] = ObjectFactory::createObjectByConfiguredName('filter_chain_class');
65         }
66
67         /**
68          * Adds a filter to a given filter group
69          *
70          * @param       $filterGroup    Group of the filter
71          * @param       $filterInstance         An instance of a filter
72          * @return      void
73          */
74         protected function addFilter ($filterGroup, Filterable $filterInstance) {
75                 $this->filterChains[$filterGroup]->addFilter($filterInstance);
76         }
77
78         /**
79          * Adds a filter to the pre filter chain
80          *
81          * @param       $filterInstance         An instance of a filter
82          * @return      void
83          */
84         public function addPreFilter (Filterable $filterInstance) {
85                 // Add the pre filter
86                 $this->addFilter('pre', $filterInstance);
87         }
88
89         /**
90          * Adds a filter to the post filter chain
91          *
92          * @param       $filterInstance         An instance of a filter
93          * @return      void
94          */
95         public function addPostFilter (Filterable $filterInstance) {
96                 // Add the post filter
97                 $this->addFilter('post', $filterInstance);
98         }
99
100         /**
101          * Executes given filter chain group
102          *
103          * @param       $filterGroup    Group of the filter to execute
104          * @param       $requestInstance        An instance of a request class
105          * @param       $responseInstance       An instance of a response class
106          * @return      void
107          * @throws      InvalidFilterChainException     If the filter chain is invalid
108          */
109         protected function executeFilters ($filterGroup, Requestable $requestInstance, Responseable $responseInstance) {
110                 // Test if the filter is there
111                 if (!isset($this->filterChains[$filterGroup])) {
112                         // Throw an exception here
113                         throw new InvalidFilterChainException(array($this, $filterGroup), self::EXCEPTION_FILTER_CHAIN_INVALID);
114                 } // END - if
115
116                 // Run all filters
117                 $this->filterChains[$filterGroup]->processFilters($requestInstance, $responseInstance);
118         }
119
120         /**
121          * Executes all pre filters
122          *
123          * @param       $requestInstance        An instance of a request class
124          * @param       $responseInstance       An instance of a response class
125          * @return      void
126          */
127         protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) {
128                 // Execute all pre filters
129                 $this->executeFilters('pre', $requestInstance, $responseInstance);
130         }
131
132         /**
133          * Executes all post filters
134          *
135          * @param       $requestInstance        An instance of a request class
136          * @param       $responseInstance       An instance of a response class
137          * @return      void
138          */
139         protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) {
140                 // Execute all post filters
141                 $this->executeFilters('post', $requestInstance, $responseInstance);
142         }
143 }
144
145 // [EOF]
146 ?>