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