3 namespace Org\Mxchange\CoreFramework\Chain\Filter;
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;
13 * A filter chain for pre and post filters
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
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.
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.
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/>.
34 class FilterChain extends BaseFrameworkSystem implements Registerable {
36 * Protected constructor
40 protected function __construct () {
41 // Call parent constructor
42 parent::__construct(__CLASS__);
46 * Creates an instance of this class
48 * @return $chainInstance An instance of this class
50 public static final function createFilterChain () {
52 $chainInstance = new FilterChain();
54 // Return the prepared instance
55 return $chainInstance;
61 * @param $filerInstance An instance of a filter class
64 public final function addFilter (Filterable $filterInstance) {
65 $this->pushValueToGenericArrayKey('filters', 'generic', 'dummy', $filterInstance);
69 * Add a new post-filter
71 * @param $filerInstance An instance of a post-filter class
74 public final function addPostFilter (Filterable $filterInstance) {
75 $this->pushValueToGenericArrayKey('filters', 'post', 'dummy', $filterInstance);
79 * "Getter" for filters array
81 * @return $filters The filters array holding all filter instances
83 protected function getFilters () {
84 // Default is nothing found
87 // Are some filters set?
88 if ($this->isValidGenericArrayKey('filters', 'generic', 'dummy')) {
90 $filters = $this->getGenericArrayKey('filters', 'generic', 'dummy');
98 * "Getter" for post-filters array
100 * @return $filters The filters array holding all post-filter instances
102 protected function getPostFilters () {
103 // Default is nothing found
106 // Are some filters set?
107 if ($this->isValidGenericArrayKey('filters', 'post', 'dummy')) {
109 $filters = $this->getGenericArrayKey('filters', 'post', 'dummy');
117 * Process all added filters. Please note that filters must throw
118 * FilterChainException if they need to interrupt the filter chain.
120 * @param $requestInstance An instance of a Requestable class
121 * @param $responseInstance An instance of a Responseable class
124 public function processFilters (Requestable $requestInstance, Responseable $responseInstance) {
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);
131 // Try to execute this filter
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());