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