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