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