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