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