More conventions than code added:
[shipsimu.git] / inc / classes / main / filter / class_FilterChain.php
1 <?php
2 /**
3  * A filter chain for pre and post filters
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class FilterChain extends BaseFrameworkSystem {
25         /**
26          * All filters together
27          */
28         private $filters = array();
29
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38
39                 // Set part description
40                 $this->setObjectDescription("A filter chain class");
41
42                 // Create unique ID number
43                 $this->generateUniqueId();
44
45                 // Clean up a little
46                 $this->removeNumberFormaters();
47                 $this->removeSystemArray();
48         }
49
50         /**
51          * Creates an instance of this class
52          *
53          * @return      $chainInstance  An instance of this class
54          */
55         public final static function createFilterChain () {
56                 // Get a new instance
57                 $chainInstance = new FilterChain();
58
59                 // Return the prepared instance
60                 return $chainInstance;
61         }
62
63         /**
64          * Add a new filter
65          *
66          * @param       $filerInstance  An instance of a filter class
67          * @return      void
68          */
69         public final function addFilter (Filterable $filterInstance) {
70                 $this->filters[] = $filterInstance;
71         }
72
73         /**
74          * Process all added filters
75          *
76          * @param       $requestInstance        An instance of a request class
77          * @param       $responseInstance       An instance of a response class
78          * @return      void
79          */
80         public function processFilters (Requestable $requestInstance, Responseable $responseInstance) {
81                 // Run all filters
82                 //* DEBUG */ echo "COUNT=".count($this->filters)."<br />\n";
83                 foreach ($this->filters as $filterInstance) {
84                         // Try to execute this filter
85                         try {
86                                 //* DEBUG */ echo "FILTER: ".$filterInstance->__toString().": Processing started.<br />\n";
87                                 $filterInstance->execute($requestInstance, $responseInstance);
88                                 //* DEBUG */ echo "FILTER: ".$filterInstance->__toString().": Processing ended.<br />\n";
89                         } catch (FilterChainException $e) {
90                                 // This exception can be thrown to just skip any further processing
91                                 break;
92                         }
93                 } // END - foreach
94         }
95 }
96
97 // [EOF]
98 ?>