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 <<<<<<< HEAD:framework/main/classes/filter/class_FilterChain.php
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
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
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.
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.
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/>.
38 class FilterChain extends BaseFrameworkSystem implements Registerable {
40 * Protected constructor
44 protected function __construct () {
45 // Call parent constructor
46 parent::__construct(__CLASS__);
50 * Creates an instance of this class
52 * @return $chainInstance An instance of this class
54 public static final function createFilterChain () {
56 $chainInstance = new FilterChain();
58 // Return the prepared instance
59 return $chainInstance;
65 * @param $filerInstance An instance of a filter class
68 public final function addFilter (Filterable $filterInstance) {
69 $this->pushValueToGenericArrayKey('filters', 'generic', 'dummy', $filterInstance);
73 * Add a new post-filter
75 * @param $filerInstance An instance of a post-filter class
78 public final function addPostFilter (Filterable $filterInstance) {
79 $this->pushValueToGenericArrayKey('filters', 'post', 'dummy', $filterInstance);
83 * "Getter" for filters array
85 * @return $filters The filters array holding all filter instances
87 protected function getFilters () {
88 // Default is nothing found
91 // Are some filters set?
92 if ($this->isValidGenericArrayKey('filters', 'generic', 'dummy')) {
94 $filters = $this->getGenericArrayKey('filters', 'generic', 'dummy');
102 * "Getter" for post-filters array
104 * @return $filters The filters array holding all post-filter instances
106 protected function getPostFilters () {
107 // Default is nothing found
110 // Are some filters set?
111 if ($this->isValidGenericArrayKey('filters', 'post', 'dummy')) {
113 $filters = $this->getGenericArrayKey('filters', 'post', 'dummy');
121 * Process all added filters. Please note that filters must throw
122 * FilterChainException if they need to interrupt the filter chain.
124 * @param $requestInstance An instance of a Requestable class
125 * @param $responseInstance An instance of a Responseable class
128 public function processFilters (Requestable $requestInstance, Responseable $responseInstance) {
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);
135 // Try to execute this filter
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());