1d88e02021476b86be2f3d30ea11b67b5903da7b
[core.git] / inc / main / classes / filter / class_FilterChain.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Filter\Chain;
4
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7 use CoreFramework\Registry\Registerable;
8 use CoreFramework\Request\Requestable;
9
10 /**
11  * A filter chain for pre and post filters
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.shipsimu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 class FilterChain extends BaseFrameworkSystem implements Registerable {
33         /**
34          * Protected constructor
35          *
36          * @return      void
37          */
38         protected function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41         }
42
43         /**
44          * Creates an instance of this class
45          *
46          * @return      $chainInstance  An instance of this class
47          */
48         public static final function createFilterChain () {
49                 // Get a new instance
50                 $chainInstance = new FilterChain();
51
52                 // Return the prepared instance
53                 return $chainInstance;
54         }
55
56         /**
57          * Add a new filter
58          *
59          * @param       $filerInstance  An instance of a filter class
60          * @return      void
61          */
62         public final function addFilter (Filterable $filterInstance) {
63                 $this->pushValueToGenericArrayKey('filters', 'generic', 'dummy', $filterInstance);
64         }
65
66         /**
67          * Add a new post-filter
68          *
69          * @param       $filerInstance  An instance of a post-filter class
70          * @return      void
71          */
72         public final function addPostFilter (Filterable $filterInstance) {
73                 $this->pushValueToGenericArrayKey('filters', 'post', 'dummy', $filterInstance);
74         }
75
76         /**
77          * "Getter" for filters array
78          *
79          * @return      $filters        The filters array holding all filter instances
80          */
81         protected function getFilters () {
82                 // Default is nothing found
83                 $filters = array();
84
85                 // Are some filters set?
86                 if ($this->isValidGenericArrayKey('filters', 'generic', 'dummy')) {
87                         // Then get them
88                         $filters = $this->getGenericArrayKey('filters', 'generic', 'dummy');
89                 } // END - if
90
91                 // Return it
92                 return $filters;
93         }
94
95         /**
96          * "Getter" for post-filters array
97          *
98          * @return      $filters        The filters array holding all post-filter instances
99          */
100         protected function getPostFilters () {
101                 // Default is nothing found
102                 $filters = array();
103
104                 // Are some filters set?
105                 if ($this->isValidGenericArrayKey('filters', 'post', 'dummy')) {
106                         // Then get them
107                         $filters = $this->getGenericArrayKey('filters', 'post', 'dummy');
108                 } // END - if
109
110                 // Return it
111                 return $filters;
112         }
113
114         /**
115          * Process all added filters. Please note that filters must throw
116          * FilterChainException if they need to interrupt the filter chain.
117          *
118          * @param       $requestInstance        An instance of a Requestable class
119          * @param       $responseInstance       An instance of a Responseable class
120          * @return      void
121          */
122         public function processFilters (Requestable $requestInstance, Responseable $responseInstance) {
123                 // Run all filters
124                 //* DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('COUNT=' . $this->countGenericArray('filters'));
125                 foreach ($this->getFilters() as $filterInstance) {
126                         // Must be an instance of Filterable
127                         assert($filterInstance instanceof Filterable);
128
129                         // Try to execute this filter
130                         try {
131                                 //* DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILTER: ' . $filterInstance->__toString() . ': Processing started.');
132                                 $filterInstance->execute($requestInstance, $responseInstance);
133                                 //* DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILTER: ' . $filterInstance->__toString() . ': Processing ended.');
134                         } catch (FilterChainException $e) {
135                                 // This exception can be thrown to just skip any further processing
136                                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Failed to execute lase filter ' . $filterInstance->__toString() . ': ' . $e->getMessage());
137                                 break;
138                         }
139                 } // END - foreach
140         }
141
142 }