3 namespace Org\Mxchange\CoreFramework\Chain\Filter;
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;
14 * A filter chain for pre and post filters
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
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.
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.
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/>.
35 class FilterChain extends BaseFrameworkSystem implements Registerable {
37 * Protected constructor
41 private function __construct () {
42 // Call parent constructor
43 parent::__construct(__CLASS__);
47 * Creates an instance of this class
49 * @return $chainInstance An instance of this class
51 public static final function createFilterChain () {
53 $chainInstance = new FilterChain();
55 // Return the prepared instance
56 return $chainInstance;
62 * @param $filerInstance An instance of a filter class
65 public final function addFilter (Filterable $filterInstance) {
66 $this->pushValueToGenericArrayKey('filters', 'generic', 'dummy', $filterInstance);
70 * Add a new post-filter
72 * @param $filerInstance An instance of a post-filter class
75 public final function addPostFilter (Filterable $filterInstance) {
76 $this->pushValueToGenericArrayKey('filters', 'post', 'dummy', $filterInstance);
80 * "Getter" for filters array
82 * @return $filters The filters array holding all filter instances
84 protected function getFilters () {
85 // Default is nothing found
88 // Are some filters set?
89 if ($this->isValidGenericArrayKey('filters', 'generic', 'dummy')) {
91 $filters = $this->getGenericArrayKey('filters', 'generic', 'dummy');
99 * "Getter" for post-filters array
101 * @return $filters The filters array holding all post-filter instances
103 protected function getPostFilters () {
104 // Default is nothing found
107 // Are some filters set?
108 if ($this->isValidGenericArrayKey('filters', 'post', 'dummy')) {
110 $filters = $this->getGenericArrayKey('filters', 'post', 'dummy');
118 * Process all added filters. Please note that filters must throw
119 * FilterChainException if they need to interrupt the filter chain.
121 * @param $requestInstance An instance of a Requestable class
122 * @param $responseInstance An instance of a Responseable class
125 public function processFilters (Requestable $requestInstance, Responseable $responseInstance) {
127 //* DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('COUNT=' . $this->countGenericArray('filters'));
128 foreach ($this->getFilters() as $filterInstance) {
129 // Try to execute this filter
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());